Class: Famili::Attributes

Inherits:
Object
  • Object
show all
Defined in:
lib/famili/attributes.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes) ⇒ Attributes

Returns a new instance of Attributes.



5
6
7
8
# File 'lib/famili/attributes.rb', line 5

def initialize(attributes)
  @attributes = attributes
  @unresolved_names = attributes.keys
end

Instance Attribute Details

#unresolved_namesObject (readonly)

Returns the value of attribute unresolved_names.



3
4
5
# File 'lib/famili/attributes.rb', line 3

def unresolved_names
  @unresolved_names
end

Instance Method Details

#[](name) ⇒ Object



10
11
12
# File 'lib/famili/attributes.rb', line 10

def [](name)
  (@cached_attributes ||= {})[name] ||= resolve(nil, name)
end

#resolve(model, name) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/famili/attributes.rb', line 14

def resolve(model, name)
  if unresolved_names.delete(name)
    attribute_value = @attributes[name]
    if attribute_value.is_a?(::Proc)
      attribute_value = model.instance_exec(&attribute_value)
    elsif attribute_value.respond_to?(:call)
      attribute_value = attribute_value.call
    end
    attribute_value = attribute_value.build if attribute_value.is_a?(::Famili::Father)
    attribute_value
  end
end