7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
# File 'lib/xero/associations/has_many.rb', line 7
def has_many(association_name, options = {})
define_method association_name do
var = instance_variable_get(:"@#{association_name}")
if var.blank?
var = HasManyProxy.new
instance_variable_set(:"@#{association_name}", var)
end
var
end
define_method "#{association_name}=" do |value|
klass = options[:class_name] || association_name.to_s.singularize
klass = "Xero::Models::#{klass.to_s.classify}".constantize
if value.is_a?(Hash) && value[value.keys.first].is_a?(Array)
value[value.keys.first].each do |item|
self.send(association_name).send :<<, klass.new(item)
end
elsif value.is_a?(Array) && value.all? { |a| a.is_a?(klass) }
instance_variable_set(
:"@#{association_name}", HasManyProxy.new(value)
)
end
self.send(association_name)
end
end
|