Class | Innate::Options |
In: |
lib/innate/options/dsl.rb
|
Parent: | Object |
Provides a minimal DSL to describe options with defaults and metadata.
The example below should demonstrate the major features, note that key lookup wanders up the hierarchy until there is a match found or the parent of the Options class is itself, in which case nil will be returned.
Usage:
class Calculator @options = Options.new(:foo) def self.options; @options; end options.dsl do o "Which method to use", :method, :plus o "Default arguments", :args, [1, 2] sub(:minus){ o("Default arguments", :args, [4, 3]) } end def self.calculate(method = nil, *args) method ||= options[:method] args = args.empty? ? options[method, :args] : args self.send(method, *args) end def self.plus(n1, n2) n1 + n2 end def self.minus(n1, n2) n1 - n2 end end Calculator.calculate # => 3 Calculator.options[:method] = :minus # => :minus Calculator.calculate # => 1 Calculator.calculate(:plus, 4, 5) # => 9
Assign new :value to the value hash on the current instance.
TODO: allow arbitrary assignments
Try to retrieve the corresponding Hash for the passed keys, will try to retrieve the key from a parent if no match is found on the current instance. If multiple keys are passed it will try to find a matching child and pass the request on to it.