42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
|
# File 'lib/scout/entity/property.rb', line 42
def property(name_and_type, &block)
name, type = case name_and_type
when Symbol, String
[name_and_type.to_sym, DEFAULT_PROPERTY_TYPE]
else
name_and_type.collect.first
end
real_method = case type
when :single, :single2array
Entity::Property.single_method(name)
when :array, :array2single
Entity::Property.array_method(name)
when :multiple
Entity::Property.multi_method(name)
when :both
name
else
raise "Type of property unknown #{type}"
end
properties[name] = block.parameters
entity_class = self
if type == :multiple
self.define_method(real_method) do |*args,**kwargs|
if entity_class.persisted_methods && entity_class.persisted_methods[name]
type, options = entity_class.persisted_methods[name]
else
type, options = nil, {persist: false}
end
missing = []
responses = {}
self.each do |item|
begin
responses[item] = Entity::Property.persist(name, item, type, options.merge(other: {args: args, kwargs: kwargs})) do
raise MultipleEntityProperty
end
rescue MultipleEntityProperty
missing << item
end
end
if missing.any?
self.annotate(missing)
new_responses = missing.instance_exec(*args, **kwargs, &block)
missing.each do |item|
responses[item] = Entity::Property.persist(name, item, type, options.merge(other: {args: args, kwargs: kwargs})) do
Array === new_responses ? new_responses[item.container_index] : new_responses[item]
end
end
end
responses.values_at(*self)
end
else
self.define_method(real_method) do |*args,**kwargs|
if entity_class.persisted_methods && entity_class.persisted_methods[name]
type, options = entity_class.persisted_methods[name]
else
type, options = nil, {persist: false}
end
Entity::Property.persist(name, self, type, options.merge(other: {args: args, kwargs: kwargs})) do
self.instance_exec(*args, **kwargs, &block)
end
end
end
return if type == :both
self.define_method(name) do |*args,**kwargs|
method_type = %w(single_method array_method multi_method).select do |method_type|
self.methods.include?(Entity::Property.send(method_type, name))
end.first
real_method = Entity::Property.send(method_type, name)
if Array === self
case method_type
when 'single_method'
self.collect{|item| item.send(real_method, *args, **kwargs) }
when 'array_method', 'multi_method'
self.send(real_method, *args, **kwargs)
end
else
case method_type
when 'single_method'
self.send(real_method, *args, **kwargs)
when 'array_method', 'multi_method'
if AnnotatedArray.is_contained?(self)
cache_code = Misc.digest({:name => name, :args => args})
res = (self.container._ary_property_cache[cache_code] ||= self.container.send(real_method, *args, **kwargs))
Array === res ? res[self.container_index] : res[self]
else
res = self.make_array.send(real_method, *args, **kwargs)
Array === res ? res[0] : res[self]
end
end
end
end
end
|