Module: Stove::Mixin::Optionable::ClassMethods

Defined in:
lib/stove/mixins/optionable.rb

Instance Method Summary collapse

Instance Method Details

#option(name, initial = UNSET_VALUE) ⇒ Object

This is a magical method. It does three things:

  1. Defines a class method getter and setter for the given option

  2. Defines an instance method that delegates to the class method

  3. (Optionally) sets the initial value



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/stove/mixins/optionable.rb', line 24

def option(name, initial = UNSET_VALUE)
  define_singleton_method(name) do |value = UNSET_VALUE|
    if value == UNSET_VALUE
      instance_variable_get("@#{name}")
    else
      instance_variable_set("@#{name}", value)
    end
  end

  define_method(name) { self.class.send(name) }

  unless initial == UNSET_VALUE
    send(name, initial)
  end
end