Class: CFoundry::V2::Model
- Inherits:
-
Object
- Object
- CFoundry::V2::Model
- Defined in:
- lib/cfoundry/v2/model.rb
Direct Known Subclasses
App, Domain, Framework, Organization, Route, Runtime, Service, ServiceAuthToken, ServiceBinding, ServiceInstance, ServicePlan, Space, User
Class Attribute Summary collapse
-
.scoped_organization ⇒ Object
readonly
Returns the value of attribute scoped_organization.
-
.scoped_space ⇒ Object
readonly
Returns the value of attribute scoped_space.
Instance Attribute Summary collapse
-
#cache ⇒ Object
Returns the value of attribute cache.
-
#guid ⇒ Object
Returns the value of attribute guid.
Class Method Summary collapse
- .attribute(name, type, opts = {}) ⇒ Object
- .attributes ⇒ Object
- .defaults ⇒ Object
- .has_summary(actions = {}) ⇒ Object
- .scoped_to_organization(relation = :organization) ⇒ Object
- .scoped_to_space(relation = :space) ⇒ Object
- .to_many(plural, opts = {}) ⇒ Object
- .to_many_relations ⇒ Object
- .to_one(name, opts = {}) ⇒ Object
- .to_one_relations ⇒ Object
- .validate_type(val, type) ⇒ Object
- .value_matches?(val, type) ⇒ Boolean
Instance Method Summary collapse
-
#create! ⇒ Object
this does a bit of extra processing to allow for ‘delete!’ followed by ‘create!’.
- #delete! ⇒ Object
- #eql?(other) ⇒ Boolean (also: #==)
- #exists? ⇒ Boolean
- #hash ⇒ Object
-
#initialize(guid, client, manifest = nil, partial = false) ⇒ Model
constructor
A new instance of Model.
- #inspect ⇒ Object
- #invalidate! ⇒ Object
- #manifest ⇒ Object
- #object_name ⇒ Object
- #partial? ⇒ Boolean
- #update! ⇒ Object
Constructor Details
#initialize(guid, client, manifest = nil, partial = false) ⇒ Model
Returns a new instance of Model.
274 275 276 277 278 279 280 281 |
# File 'lib/cfoundry/v2/model.rb', line 274 def initialize(guid, client, manifest = nil, partial = false) @guid = guid @client = client @manifest = manifest @partial = partial @cache = {} @diff = {} end |
Class Attribute Details
.scoped_organization ⇒ Object (readonly)
Returns the value of attribute scoped_organization.
6 7 8 |
# File 'lib/cfoundry/v2/model.rb', line 6 def scoped_organization @scoped_organization end |
.scoped_space ⇒ Object (readonly)
Returns the value of attribute scoped_space.
6 7 8 |
# File 'lib/cfoundry/v2/model.rb', line 6 def scoped_space @scoped_space end |
Instance Attribute Details
#cache ⇒ Object
Returns the value of attribute cache.
272 273 274 |
# File 'lib/cfoundry/v2/model.rb', line 272 def cache @cache end |
#guid ⇒ Object
Returns the value of attribute guid.
272 273 274 |
# File 'lib/cfoundry/v2/model.rb', line 272 def guid @guid end |
Class Method Details
.attribute(name, type, opts = {}) ⇒ Object
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 |
# File 'lib/cfoundry/v2/model.rb', line 56 def attribute(name, type, opts = {}) attributes[name] = opts default = opts[:default] if has_default = opts.key?(:default) defaults[name] = default end define_method(name) do return @cache[name] if @cache.key?(name) @cache[name] = manifest[:entity][name] || default end define_method(:"#{name}=") do |val| unless has_default && val == default Model.validate_type(val, type) end @cache[name] = val @manifest ||= {} @manifest[:entity] ||= {} @manifest[:entity][name] = val @diff[name] = val end end |
.attributes ⇒ Object
44 45 46 |
# File 'lib/cfoundry/v2/model.rb', line 44 def attributes @attributes ||= {} end |
.defaults ⇒ Object
40 41 42 |
# File 'lib/cfoundry/v2/model.rb', line 40 def defaults @defaults ||= {} end |
.has_summary(actions = {}) ⇒ Object
228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 |
# File 'lib/cfoundry/v2/model.rb', line 228 def has_summary(actions = {}) define_method(:summary) do @client.base.request_path( Net::HTTP::Get, ["v2", "#{object_name}s", @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 |
.scoped_to_organization(relation = :organization) ⇒ Object
85 86 87 |
# File 'lib/cfoundry/v2/model.rb', line 85 def scoped_to_organization(relation = :organization) @scoped_organization = relation end |
.scoped_to_space(relation = :space) ⇒ Object
89 90 91 |
# File 'lib/cfoundry/v2/model.rb', line 89 def scoped_to_space(relation = :space) @scoped_space = relation end |
.to_many(plural, opts = {}) ⇒ Object
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 |
# File 'lib/cfoundry/v2/model.rb', line 138 def to_many(plural, opts = {}) to_many_relations[plural] = opts singular = plural.to_s.sub(/s$/, "").to_sym object = opts[:as] || singular plural_object = :"#{object}s" kls = object.to_s.capitalize.gsub(/(.)_(.)/) do $1 + $2.upcase end define_method(plural) do |*args| depth, query = args if !depth && !query && cache = @cache[plural] return cache end if @manifest && @manifest[:entity].key?(plural) && !depth objs = @manifest[:entity][plural] if query find_by = query.keys.first find_val = query.values.first objs = objs.select { |o| o[:entity][find_by] == find_val } end res = objs.collect do |json| @client.send(:"make_#{object}", json) end else res = @client.send( :"#{plural_object}_from", "/v2/#{object_name}s/#@guid/#{plural}", depth || opts[:depth], query) end unless depth || query @cache[plural] = res end res end define_method(:"#{plural}_url") do manifest[:entity][:"#{plural}_url"] end define_method(:"add_#{singular}") do |x| Model.validate_type(x, CFoundry::V2.const_get(kls)) if cache = @cache[plural] cache << x unless cache.include?(x) end @client.base.request_path( Net::HTTP::Put, ["v2", "#{object_name}s", @guid, plural, x.guid], :accept => :json) end define_method(:"remove_#{singular}") do |x| Model.validate_type(x, CFoundry::V2.const_get(kls)) if cache = @cache[plural] cache.delete(x) end @client.base.request_path( Net::HTTP::Delete, ["v2", "#{object_name}s", @guid, plural, x.guid], :accept => :json) end define_method(:"#{plural}=") do |xs| Model.validate_type(xs, [CFoundry::V2.const_get(kls)]) @cache[plural] = xs @manifest ||= {} @manifest[:entity] ||= {} @manifest[:entity][:"#{singular}_guids"] = @diff[:"#{singular}_guids"] = xs.collect(&:guid) end end |
.to_many_relations ⇒ Object
52 53 54 |
# File 'lib/cfoundry/v2/model.rb', line 52 def to_many_relations @to_many_relations ||= {} end |
.to_one(name, opts = {}) ⇒ Object
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 |
# File 'lib/cfoundry/v2/model.rb', line 93 def to_one(name, opts = {}) to_one_relations[name] = opts obj = opts[:as] || name kls = obj.to_s.capitalize.gsub(/(.)_(.)/) do $1 + $2.upcase end default = opts[:default] if has_default = opts.key?(:default) defaults[:"#{name}_guid"] = default end define_method(name) do return @cache[name] if @cache.key?(name) @cache[name] = if @manifest && @manifest[:entity].key?(name) @client.send(:"make_#{obj}", @manifest[:entity][name]) elsif url = send("#{name}_url") @client.send(:"#{obj}_from", url, opts[:depth] || 1) else default end end define_method(:"#{name}_url") do manifest[:entity][:"#{name}_url"] end define_method(:"#{name}=") do |x| unless has_default && x == default Model.validate_type(x, CFoundry::V2.const_get(kls)) end @cache[name] = x @manifest ||= {} @manifest[:entity] ||= {} @manifest[:entity][:"#{name}_guid"] = @diff[:"#{name}_guid"] = x && x.guid end end |
.to_one_relations ⇒ Object
48 49 50 |
# File 'lib/cfoundry/v2/model.rb', line 48 def to_one_relations @to_one_relations ||= {} end |
.validate_type(val, type) ⇒ Object
34 35 36 37 38 |
# File 'lib/cfoundry/v2/model.rb', line 34 def validate_type(val, type) unless value_matches?(val, type) raise CFoundry::Mismatch.new(type, val) end end |
.value_matches?(val, type) ⇒ Boolean
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 |
# File 'lib/cfoundry/v2/model.rb', line 8 def value_matches?(val, type) case type when Class val.is_a?(type) when Regexp val.is_a?(String) && val =~ type when :url value_matches?(val, URI::regexp(%w(http https))) when :https_url value_matches?(val, URI::regexp("https")) when :boolean val.is_a?(TrueClass) || val.is_a?(FalseClass) when Array val.all? do |x| value_matches?(x, type.first) end when Hash val.is_a?(Hash) && type.all? { |name, subtype| val.key?(name) && value_matches?(val[name], subtype) } else val.is_a?(Object.const_get(type.to_s.capitalize)) end end |
Instance Method Details
#create! ⇒ Object
this does a bit of extra processing to allow for ‘delete!’ followed by ‘create!’
311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 |
# File 'lib/cfoundry/v2/model.rb', line 311 def create! payload = {} self.class.defaults.merge(@manifest[:entity]).each do |k, v| if v.is_a?(Hash) && v.key?(:metadata) # skip; there's a _guid attribute already elsif v.is_a?(Array) && 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?("_json") && v.is_a?(String) payload[k] = MultiJson.load(v) elsif k.to_s.end_with?("_url") else payload[k] = v end end @manifest = @client.base.send(:"create_#{object_name}", payload) @guid = @manifest[:metadata][:guid] @diff.clear true end |
#delete! ⇒ Object
354 355 356 357 358 359 360 361 362 363 364 365 366 |
# File 'lib/cfoundry/v2/model.rb', line 354 def delete! @client.base.send(:"delete_#{object_name}", @guid) @guid = nil @diff.clear if @manifest @manifest.delete :metadata end true end |
#eql?(other) ⇒ Boolean Also known as: ==
375 376 377 |
# File 'lib/cfoundry/v2/model.rb', line 375 def eql?(other) other.is_a?(self.class) && @guid == other.guid end |
#exists? ⇒ Boolean
368 369 370 371 372 373 |
# File 'lib/cfoundry/v2/model.rb', line 368 def exists? @client.base.send(object_name, @guid) true rescue CFoundry::NotFound false end |
#hash ⇒ Object
380 381 382 |
# File 'lib/cfoundry/v2/model.rb', line 380 def hash @guid.hash end |
#inspect ⇒ Object
291 292 293 |
# File 'lib/cfoundry/v2/model.rb', line 291 def inspect "\#<#{self.class.name} '#@guid'>" end |
#invalidate! ⇒ Object
302 303 304 305 306 307 |
# File 'lib/cfoundry/v2/model.rb', line 302 def invalidate! @manifest = nil @partial = false @cache = {} @diff = {} end |
#manifest ⇒ Object
283 284 285 |
# File 'lib/cfoundry/v2/model.rb', line 283 def manifest @manifest ||= @client.base.send(object_name, @guid) end |
#object_name ⇒ Object
295 296 297 298 299 300 |
# File 'lib/cfoundry/v2/model.rb', line 295 def object_name @object_name ||= self.class.name.split("::").last.gsub( /([a-z])([A-Z])/, '\1_\2').downcase end |
#partial? ⇒ Boolean
287 288 289 |
# File 'lib/cfoundry/v2/model.rb', line 287 def partial? @partial end |
#update! ⇒ Object
346 347 348 349 350 351 352 |
# File 'lib/cfoundry/v2/model.rb', line 346 def update! @manifest = @client.base.send(:"update_#{object_name}", @guid, @diff) @diff.clear true end |