Module: Chemlab::Attributable

Defined in:
lib/chemlab/attributable.rb

Overview

Attributable module

Examples:

class MyClass
  include Attributable
  attribute :url
  attribute :id
  attribute :name do
    'test'
  end

Class Method Summary collapse

Class Method Details

.included(base) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/chemlab/attributable.rb', line 14

def self.included(base)
  base.class_eval do
    def self.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
  end
end