Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Add an idea about handling config at some point.

git-svn-id: http://svn.macosforge.org/repository/ruby/DietRB/trunk@4863 23306eb0-4c56-4727-a40e-e92c0eb68959
  • Loading branch information...
commit 363fe3162ff66ae7ed913e8121c09f04484bf09f 1 parent 8be2cbf
@alloy alloy authored
Showing with 92 additions and 0 deletions.
  1. +6 −0 TODO
  2. +86 −0 config_prototype.rb
View
6 TODO
@@ -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
View
86 config_prototype.rb
@@ -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
Please sign in to comment.
Something went wrong with that request. Please try again.