Class: CFoundry::V2::Model
- Inherits:
-
Object
- Object
- CFoundry::V2::Model
show all
- Extended by:
- ModelMagic
- Includes:
- ActiveModel::Validations
- Defined in:
- lib/cfoundry/v2/model.rb
Direct Known Subclasses
App, AppEvent, Domain, Organization, QuotaDefinition, Route, Service, ServiceAuthToken, ServiceBinding, ServiceInstance, ServicePlan, Space, Stack, User
Constant Summary
collapse
- @@objects =
{}
Instance Attribute Summary collapse
Attributes included from ModelMagic
#scoped_organization, #scoped_space
Class Method Summary
collapse
Instance Method Summary
collapse
Methods included from ModelMagic
attributes, defaults, inherited, params_from, scoped_to_organization, scoped_to_space, to_many_relations, to_one_relations
#queryable_by
#to_many
#to_one
#attribute
#has_summary
#add_client_methods
Constructor Details
#initialize(guid, client, manifest = nil, partial = false) ⇒ Model
Returns a new instance of Model.
27
28
29
30
31
32
33
34
35
|
# File 'lib/cfoundry/v2/model.rb', line 27
def initialize(guid, client, manifest = nil, partial = false)
@guid = guid
@client = client
@manifest = manifest
@partial = partial
@cache = {}
@diff = {}
@changes = {}
end
|
Instance Attribute Details
#cache ⇒ Object
Returns the value of attribute cache.
23
24
25
|
# File 'lib/cfoundry/v2/model.rb', line 23
def cache
@cache
end
|
#changes ⇒ Object
Returns the value of attribute changes.
23
24
25
|
# File 'lib/cfoundry/v2/model.rb', line 23
def changes
@changes
end
|
#created_at ⇒ Object
Returns the value of attribute created_at.
24
25
26
|
# File 'lib/cfoundry/v2/model.rb', line 24
def created_at
@created_at
end
|
#diff ⇒ Object
Returns the value of attribute diff.
25
26
27
|
# File 'lib/cfoundry/v2/model.rb', line 25
def diff
@diff
end
|
#guid ⇒ Object
Returns the value of attribute guid.
23
24
25
|
# File 'lib/cfoundry/v2/model.rb', line 23
def guid
@guid
end
|
#updated_at ⇒ Object
Returns the value of attribute updated_at.
24
25
26
|
# File 'lib/cfoundry/v2/model.rb', line 24
def updated_at
@updated_at
end
|
Class Method Details
.inherited(klass) ⇒ Object
17
18
19
20
|
# File 'lib/cfoundry/v2/model.rb', line 17
def inherited(klass)
@@objects[klass.object_name] = klass
super
end
|
.objects ⇒ Object
13
14
15
|
# File 'lib/cfoundry/v2/model.rb', line 13
def objects
@@objects
end
|
Instance Method Details
#attribute_for_error(error) ⇒ Object
91
92
93
|
# File 'lib/cfoundry/v2/model.rb', line 91
def attribute_for_error(error)
:base
end
|
#changed? ⇒ Boolean
55
56
57
|
# File 'lib/cfoundry/v2/model.rb', line 55
def changed?
!@changes.empty?
end
|
#create ⇒ Object
79
80
81
82
83
84
85
86
87
88
89
|
# File 'lib/cfoundry/v2/model.rb', line 79
def create
create!
true
rescue CFoundry::APIError => e
if e.instance_of? CFoundry::APIError
errors.add(:base, :cc_client)
else
errors.add(attribute_for_error(e), e.message)
end
false
end
|
#create! ⇒ Object
this does a bit of extra processing to allow for ‘delete!’ followed by ‘create!’
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
|
# File 'lib/cfoundry/v2/model.rb', line 97
def create!
payload = {}
@manifest ||= {}
@manifest[:entity] ||= {}
@manifest[:entity].each do |k, v|
if v.is_a?(Hash) && v.key?(:metadata)
elsif v.is_a?(Array) && !v.empty? && v.all? { |x|
x.is_a?(Hash) && x.key?(:metadata)
}
singular = k.to_s.sub(/s$/, "")
payload[:"#{singular}_guids"] = v.collect do |x|
if x.is_a?(Hash) && x.key?(:metadata)
x[:metadata][:guid]
else
x
end
end
elsif k.to_s.end_with?("_url")
else
payload[k] = v
end
end
@manifest = @client.base.post("v2", plural_object_name,
:content => :json,
:accept => :json,
:payload => payload
)
@guid = @manifest[:metadata][:guid]
@diff.clear
true
end
|
#delete(options = {}) ⇒ Object
149
150
151
152
153
154
155
156
157
158
|
# File 'lib/cfoundry/v2/model.rb', line 149
def delete(options = {})
delete!(options)
rescue CFoundry::APIError => e
if e.instance_of? CFoundry::APIError
errors.add(:base, :cc_client)
else
errors.add(attribute_for_error(e), e.message)
end
false
end
|
#delete!(options = {}) ⇒ Object
160
161
162
163
164
165
166
167
168
169
170
171
172
|
# File 'lib/cfoundry/v2/model.rb', line 160
def delete!(options = {})
@client.base.delete("v2", plural_object_name, guid, :params => options)
@deleted = true
@diff.clear
if @manifest
@manifest.delete :metadata
end
true
end
|
#eql?(other) ⇒ Boolean
Also known as:
==
198
199
200
|
# File 'lib/cfoundry/v2/model.rb', line 198
def eql?(other)
other.is_a?(self.class) && @guid == other.guid
end
|
#exists? ⇒ Boolean
186
187
188
189
190
191
192
|
# File 'lib/cfoundry/v2/model.rb', line 186
def exists?
invalidate!
manifest
true
rescue CFoundry::NotFound
false
end
|
#hash ⇒ Object
204
205
206
|
# File 'lib/cfoundry/v2/model.rb', line 204
def hash
@guid.hash
end
|
#inspect ⇒ Object
59
60
61
|
# File 'lib/cfoundry/v2/model.rb', line 59
def inspect
"\#<#{self.class.name} '#@guid'>"
end
|
#invalidate! ⇒ Object
71
72
73
74
75
76
77
|
# File 'lib/cfoundry/v2/model.rb', line 71
def invalidate!
@manifest = nil
@partial = false
@cache = {}
@diff = {}
@changes = {}
end
|
#manifest ⇒ Object
47
48
49
|
# File 'lib/cfoundry/v2/model.rb', line 47
def manifest
@manifest ||= @client.base.send(object_name, @guid)
end
|
#object_name ⇒ Object
63
64
65
|
# File 'lib/cfoundry/v2/model.rb', line 63
def object_name
@object_name ||= self.class.object_name
end
|
#partial? ⇒ Boolean
51
52
53
|
# File 'lib/cfoundry/v2/model.rb', line 51
def partial?
@partial
end
|
#persisted? ⇒ Boolean
182
183
184
|
# File 'lib/cfoundry/v2/model.rb', line 182
def persisted?
@guid && !@deleted
end
|
#plural_object_name ⇒ Object
67
68
69
|
# File 'lib/cfoundry/v2/model.rb', line 67
def plural_object_name
@plural_object_name ||= self.class.plural_object_name
end
|
#query_target(klass) ⇒ Object
194
195
196
|
# File 'lib/cfoundry/v2/model.rb', line 194
def query_target(klass)
self
end
|
#to_key ⇒ Object
178
179
180
|
# File 'lib/cfoundry/v2/model.rb', line 178
def to_key
persisted? ? [@guid] : nil
end
|
#to_param ⇒ Object
174
175
176
|
# File 'lib/cfoundry/v2/model.rb', line 174
def to_param
persisted? ? @guid.to_s : nil
end
|
#update! ⇒ Object
137
138
139
140
141
142
143
144
145
146
147
|
# File 'lib/cfoundry/v2/model.rb', line 137
def update!
@manifest = @client.base.put("v2", plural_object_name, guid,
:content => :json,
:accept => :json,
:payload => @diff
)
@diff.clear
true
end
|