Class: Chemlab::Component::DSL

Inherits:
Object
  • Object
show all
Defined in:
lib/chemlab/component.rb

Instance Method Summary collapse

Constructor Details

#initialize(base) ⇒ DSL

Returns a new instance of DSL.



51
52
53
# File 'lib/chemlab/component.rb', line 51

def initialize(base)
  @base = base
end

Instance Method Details

#attribute(name) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/chemlab/component.rb', line 55

def attribute(name)
  @base.module_eval do
    attr_writer(name)

    default_value = block_given? ? yield : nil

    define_singleton_method(name) do |value = nil|
      instance_variable_get("@#{name}") ||
        instance_variable_set(
          "@#{name}",
          value || default_value
        )
    end
  end
end