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

Methods

[]   []=   default   dsl   each_option   each_pair   get   inspect   merge!   method_missing   new   o   option   pretty_print   set_value   sub   to_hash   trigger  

Public Class methods

Public Instance methods

Retrieve only the :value from the value hash if found via keys.

Assign new :value to the value hash on the current instance.

TODO: allow arbitrary assignments

To avoid lookup on the parent, we can set a default to the internal Hash. Parameters as in {Options#o}, but without the key.

Shortcut for instance_eval

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.

o(doc, key, value, other = {}, &block)

Alias for option

Store an option in the Options instance.

@param [to_s] doc describing the purpose of this option @param [to_sym] key used to access @param [Object] value may be anything @param [Hash] other optional Hash containing meta-data

                       :doc, :value keys will be ignored

@param [Array] keys @param [Object] value

Create a new Options instance with name and pass block on to its dsl. Assigns the new instance to the name Symbol on current instance.

Add a block that will be called when a new value is set.

[Validate]