8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
# File 'lib/minimapper/entity/serialized_association.rb', line 8
def serialized_association(name, entity_class_or_proc = nil)
attribute_name = "#{name}_attributes"
attribute attribute_name
define_method(name) do
attributes = public_send(attribute_name) || {}
entity = instance_variable_get("@#{name}")
if entity_class_or_proc.is_a?(Proc)
entity ||= instance_exec(attributes, &entity_class_or_proc)
else
entity_class = entity_class_or_proc ||
name.to_s.camelcase.constantize
entity ||= entity_class.new(attributes)
end
instance_variable_set("@#{name}", entity)
end
end
|