3
4
5
6
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
|
# File 'lib/cfoundry/v2/model_magic/has_summary.rb', line 3
def has_summary(actions = {})
define_method(:summary) do
@client.base.get("v2", plural_object_name, @guid, "summary", :accept => :json)
end
define_method(:summarize!) do |*args|
body, _ = args
body ||= summary
body.each do |key, val|
if act = actions[key]
instance_exec(val, &act)
elsif self.class.attributes[key]
self.send(:"#{key}=", val)
elsif self.class.to_many_relations[key]
singular = key.to_s.sub(/s$/, "").to_sym
vals = val.collect do |sub|
obj = @client.send(singular, sub[:guid], true)
obj.summarize! sub
obj
end
self.send(:"#{key}=", vals)
elsif self.class.to_one_relations[key]
obj = @client.send(key, val[:guid], true)
obj.summarize! val
self.send(:"#{key}=", obj)
end
end
nil
end
end
|