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.
26
27
28
29
30
31
32
33
34
|
# File 'lib/cfoundry/v2/model.rb', line 26
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
|
#diff ⇒ Object
Returns the value of attribute diff.
24
25
26
|
# File 'lib/cfoundry/v2/model.rb', line 24
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
|
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
80
81
82
|
# File 'lib/cfoundry/v2/model.rb', line 80
def attribute_for_error(error)
:base
end
|
#changed? ⇒ Boolean
44
45
46
|
# File 'lib/cfoundry/v2/model.rb', line 44
def changed?
!@changes.empty?
end
|
#create ⇒ Object
68
69
70
71
72
73
74
75
76
77
78
|
# File 'lib/cfoundry/v2/model.rb', line 68
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!’
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
|
# File 'lib/cfoundry/v2/model.rb', line 86
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
138
139
140
141
142
143
144
145
146
147
|
# File 'lib/cfoundry/v2/model.rb', line 138
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
149
150
151
152
153
154
155
156
157
158
159
160
161
|
# File 'lib/cfoundry/v2/model.rb', line 149
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:
==
187
188
189
|
# File 'lib/cfoundry/v2/model.rb', line 187
def eql?(other)
other.is_a?(self.class) && @guid == other.guid
end
|
#exists? ⇒ Boolean
175
176
177
178
179
180
181
|
# File 'lib/cfoundry/v2/model.rb', line 175
def exists?
invalidate!
manifest
true
rescue CFoundry::NotFound
false
end
|
#hash ⇒ Object
193
194
195
|
# File 'lib/cfoundry/v2/model.rb', line 193
def hash
@guid.hash
end
|
#inspect ⇒ Object
48
49
50
|
# File 'lib/cfoundry/v2/model.rb', line 48
def inspect
"\#<#{self.class.name} '#@guid'>"
end
|
#invalidate! ⇒ Object
60
61
62
63
64
65
66
|
# File 'lib/cfoundry/v2/model.rb', line 60
def invalidate!
@manifest = nil
@partial = false
@cache = {}
@diff = {}
@changes = {}
end
|
#manifest ⇒ Object
36
37
38
|
# File 'lib/cfoundry/v2/model.rb', line 36
def manifest
@manifest ||= @client.base.send(object_name, @guid)
end
|
#object_name ⇒ Object
52
53
54
|
# File 'lib/cfoundry/v2/model.rb', line 52
def object_name
@object_name ||= self.class.object_name
end
|
#partial? ⇒ Boolean
40
41
42
|
# File 'lib/cfoundry/v2/model.rb', line 40
def partial?
@partial
end
|
#persisted? ⇒ Boolean
171
172
173
|
# File 'lib/cfoundry/v2/model.rb', line 171
def persisted?
@guid && !@deleted
end
|
#plural_object_name ⇒ Object
56
57
58
|
# File 'lib/cfoundry/v2/model.rb', line 56
def plural_object_name
@plural_object_name ||= self.class.plural_object_name
end
|
#query_target(klass) ⇒ Object
183
184
185
|
# File 'lib/cfoundry/v2/model.rb', line 183
def query_target(klass)
self
end
|
#to_key ⇒ Object
167
168
169
|
# File 'lib/cfoundry/v2/model.rb', line 167
def to_key
persisted? ? [@guid] : nil
end
|
#to_param ⇒ Object
163
164
165
|
# File 'lib/cfoundry/v2/model.rb', line 163
def to_param
persisted? ? @guid.to_s : nil
end
|
#update! ⇒ Object
126
127
128
129
130
131
132
133
134
135
136
|
# File 'lib/cfoundry/v2/model.rb', line 126
def update!
@client.base.put("v2", plural_object_name, guid,
:content => :json,
:accept => :json,
:payload => @diff
)
@diff.clear
true
end
|