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
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
|
# File 'lib/cfoundry/v2/model_magic/client_extensions.rb', line 5
def add_client_methods(klass)
singular = klass.object_name
plural = klass.plural_object_name
define_base_client_methods do
define_method(singular) do |guid, *args|
get("v2", plural, guid,
:accept => :json,
:params => ModelMagic.params_from(args)
)
end
define_method(plural) do |*args|
all_pages(
get("v2", plural,
:accept => :json,
:params => ModelMagic.params_from(args)
)
)
end
define_method(:"#{plural}_first_page") do |*args|
get("v2", plural,
:accept => :json,
:params => ModelMagic.params_from(args)
)
end
end
define_client_methods do
define_method(singular) do |*args|
guid, partial, _ = args
x = klass.new(guid, self, nil, partial)
unless guid
if klass.scoped_organization && current_organization
x.send(:"#{klass.scoped_organization}=", current_organization)
end
if klass.scoped_space && current_space
x.send(:"#{klass.scoped_space}=", current_space)
end
end
x
end
define_method(plural) do |*args|
if klass.scoped_space && current_space
current_space.send(plural, *args)
elsif klass.scoped_organization && current_organization
current_organization.send(plural, *args)
else
@base.send(plural, *args).collect do |json|
send(:"make_#{singular}", json)
end
end
end
define_method(:"#{plural}_first_page") do |*args|
response = @base.send(:"#{plural}_first_page", *args)
results = response[:resources].collect do |json|
send(:"make_#{singular}", json)
end
{
:next_page => !!response[:next_url],
:results => results
}
end
define_method(:"#{singular}_from") do |path, *args|
send(
:"make_#{singular}",
@base.get(
path,
:accept => :json,
:params => ModelMagic.params_from(args)))
end
define_method(:"#{plural}_from") do |path, *args|
objs = @base.all_pages(
@base.get(
path,
:accept => :json,
:params => ModelMagic.params_from(args)))
objs.collect do |json|
send(:"make_#{singular}", json)
end
end
define_method(:"make_#{singular}") do |json|
klass.new(
json[:metadata][:guid],
self,
json)
end
end
end
|