[macruby-changes] [4863] DietRB/trunk
source_changes at macosforge.org
source_changes at macosforge.org
Sun Oct 31 17:10:43 PDT 2010
Revision: 4863
http://trac.macosforge.org/projects/ruby/changeset/4863
Author: eloy.de.enige at gmail.com
Date: 2010-10-31 17:10:39 -0700 (Sun, 31 Oct 2010)
Log Message:
-----------
Add an idea about handling config at some point.
Modified Paths:
--------------
DietRB/trunk/TODO
Added Paths:
-----------
DietRB/trunk/config_prototype.rb
Modified: DietRB/trunk/TODO
===================================================================
--- DietRB/trunk/TODO 2010-10-31 23:03:17 UTC (rev 4862)
+++ DietRB/trunk/TODO 2010-11-01 00:10:39 UTC (rev 4863)
@@ -6,3 +6,9 @@
* Make sure the following formatters work: hirb, and looksee
* Make sure the majority of the utils in utility_belt work
* Possibly add copy-paste support as an ext
+
+= Config
+
+Add a global config. Modules such as the formatter can register options with it.
+The modules must always check the global config, unless the option has been
+explicitely overriden on the module instance. See config_prototype.rb
Added: DietRB/trunk/config_prototype.rb
===================================================================
--- DietRB/trunk/config_prototype.rb (rev 0)
+++ DietRB/trunk/config_prototype.rb 2010-11-01 00:10:39 UTC (rev 4863)
@@ -0,0 +1,86 @@
+class Config
+ class Entry
+ attr_accessor :klass, :name, :options
+
+ def initialize(klass, name)
+ @klass, @name = klass, name
+ @options = {}
+ end
+
+ def add_option(name, default, description)
+ name = name.to_s
+ @options[name] = [default, description]
+ klass.class_eval(%{
+ attr_writer :#{name}
+ def #{name}
+ @#{name}.nil? ? Config.current.#{@name}.#{name} : @#{name}
+ end
+ })
+ end
+
+ def to_s
+ @options.keys.sort.map do |name|
+ "config.#{@name}.#{name} = #{@options[name][0]} # #{@options[name][1]}"
+ end.join("\n")
+ end
+
+ def method_missing(name, *args)
+ name = name.to_s
+ if name[-1,1] == '=' && option = @options[name[0..-1]]
+ option[0] = args[0]
+ elsif option = @options[name]
+ option[0]
+ else
+ super
+ end
+ end
+ end
+
+ def self.current
+ @current ||= new
+ end
+
+ attr_accessor :entries
+
+ def initialize
+ @entries = {}
+ end
+
+ def add_entry(klass, name)
+ name = name.to_s
+ entry = Entry.new(klass, name)
+ yield entry
+ @entries[name] = entry
+ end
+
+ def to_s
+ @entries.keys.sort.map do |name|
+ @entries[name].to_s
+ end.join("\n")
+ end
+
+ def method_missing(entry)
+ @entries[entry.to_s] || super
+ end
+end
+
+module Kernel
+ def config
+ Config.current
+ end
+end
+
+class Formatter
+ Config.current.add_entry(self, :formatter) do |entry|
+ entry.add_option(:use_inspect, true, "Call inspect on the object")
+ entry.add_option(:auto_indent, true, "Automatically indent code")
+ end
+end
+
+puts config
+
+f = Formatter.new
+p f.use_inspect
+f.use_inspect = false
+p f.use_inspect
+p f.auto_indent
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/macruby-changes/attachments/20101031/6f44d8da/attachment.html>
More information about the macruby-changes
mailing list