Module: Fathom::AttributeSystem::ClassMethods

Defined in:
lib/fathom/behaviors/attribute_system.rb

Instance Method Summary collapse

Instance Method Details

#attribute(getter_name, default = nil) ⇒ Object

Define an attribute, or property of this class. Useful for very thin, generic Ruby classes for Data models.



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/fathom/behaviors/attribute_system.rb', line 40

def attribute(getter_name, default=nil)
  
  # Define a getter on the object
  define_method(getter_name) do
    
    target = send(self.class.attributes_proxy)

    return target[getter_name] unless target.respond_to?(:has_key?)

    if target.has_key?(getter_name)
      target[getter_name]
    else
      self.class.attribute_defaults[getter_name]
    end
  end

  # Define a setter on the object
  define_method("#{getter_name}=") do |value|
    send(self.class.attributes_proxy)[getter_name] = value
  end
  
  # Define a default on the object
  attribute_defaults[getter_name] = default
  
end

#attribute_defaultsObject



75
76
77
# File 'lib/fathom/behaviors/attribute_system.rb', line 75

def attribute_defaults
  @attribute_defaults ||= {}
end

#attributes_proxyObject



71
72
73
# File 'lib/fathom/behaviors/attribute_system.rb', line 71

def attributes_proxy
  @attributes_proxy ||= :attributes
end

#set_attributes_proxy(value) ⇒ Object

Defines where the attributes are stored for the target class.



67
68
69
# File 'lib/fathom/behaviors/attribute_system.rb', line 67

def set_attributes_proxy(value)
  @attributes_proxy = value
end