Module: Chemlab::Attributable

Defined in:
lib/chemlab/attributable.rb

Overview

Attributable module

Examples:

class MyClass
  include Attributable
  attribute :url
  attribute :id
  attribute :name { 'test' }

Instance Method Summary collapse

Instance Method Details

#attribute(name) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/chemlab/attributable.rb', line 12

def attribute(name)
  default_value = nil
  default_value = yield if block_given?

  define_method(name) do
    instance_variable_get("@#{name}") ||
      instance_variable_set(
        "@#{name}",
        default_value
      )
  end
end