Module: CFoundry::V2::ModelMagic
- Included in:
- Model
- Defined in:
- lib/cfoundry/v2/model_magic.rb
Instance 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.
Class Method Summary collapse
Instance Method Summary collapse
- #attribute(name, type, opts = {}) ⇒ Object
- #attributes ⇒ Object
- #defaults ⇒ Object
- #has_summary(actions = {}) ⇒ Object
- #inherited(klass) ⇒ Object
- #object_name ⇒ Object
- #queryable_by(*names) ⇒ 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
Instance Attribute Details
#scoped_organization ⇒ Object (readonly)
Returns the value of attribute scoped_organization.
16 17 18 |
# File 'lib/cfoundry/v2/model_magic.rb', line 16 def scoped_organization @scoped_organization end |
#scoped_space ⇒ Object (readonly)
Returns the value of attribute scoped_space.
16 17 18 |
# File 'lib/cfoundry/v2/model_magic.rb', line 16 def scoped_space @scoped_space end |
Class Method Details
.params_from(args) ⇒ Object
447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 |
# File 'lib/cfoundry/v2/model_magic.rb', line 447 def params_from(args) , _ = args ||= {} [:depth] ||= 1 params = {} .each do |k, v| case k when :depth params[:"inline-relations-depth"] = v when :query params[:q] = v.join(":") end end params end |
.validate_type(val, type) ⇒ Object
441 442 443 444 445 |
# File 'lib/cfoundry/v2/model_magic.rb', line 441 def validate_type(val, type) unless value_matches?(val, type) raise CFoundry::Mismatch.new(type, val) end end |
.value_matches?(val, type) ⇒ Boolean
415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 |
# File 'lib/cfoundry/v2/model_magic.rb', line 415 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
#attribute(name, type, opts = {}) ⇒ Object
136 137 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 |
# File 'lib/cfoundry/v2/model_magic.rb', line 136 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 ModelMagic.validate_type(val, type) end @cache[name] = val @manifest ||= {} @manifest[:entity] ||= {} old = @manifest[:entity][name] @changes[name] = [old, val] if old != val @manifest[:entity][name] = val @diff[name] = val end end |
#attributes ⇒ Object
29 30 31 |
# File 'lib/cfoundry/v2/model_magic.rb', line 29 def attributes @attributes ||= {} end |
#defaults ⇒ Object
25 26 27 |
# File 'lib/cfoundry/v2/model_magic.rb', line 25 def defaults @defaults ||= {} end |
#has_summary(actions = {}) ⇒ Object
341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 |
# File 'lib/cfoundry/v2/model_magic.rb', line 341 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 |
#inherited(klass) ⇒ Object
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.rb', line 41 def inherited(klass) singular = klass.object_name plural = :"#{singular}s" BaseClientMethods.module_eval do define_method(singular) do |guid, *args| get("v2", plural, guid, :accept => :json, :params => ModelMagic.params_from(args)) end define_method(:"create_#{singular}") do |payload| post(payload, "v2", plural, :content => :json, :accept => :json) end define_method(:"delete_#{singular}") do |guid| delete("v2", plural, guid) true end define_method(:"update_#{singular}") do |guid, payload| put(payload, "v2", plural, guid, :content => :json, :accept => :json) end define_method(plural) do |*args| all_pages( get("v2", plural, :accept => :json, :params => ModelMagic.params_from(args))) end end ClientMethods.module_eval do define_method(singular) do |*args| guid, partial, _ = args x = klass.new(guid, self, nil, partial) # when creating an object, automatically set the org/space 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| # use current org/space 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(:"#{singular}_from") do |path, *args| send( :"make_#{singular}", @base.request_path( Net::HTTP::Get, path, :accept => :json, :params => ModelMagic.params_from(args))) end define_method(:"#{plural}_from") do |path, *args| objs = @base.all_pages( @base.request_path( Net::HTTP::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 |
#object_name ⇒ Object
18 19 20 21 22 23 |
# File 'lib/cfoundry/v2/model_magic.rb', line 18 def object_name @object_name ||= name.split("::").last.gsub( /([a-z])([A-Z])/, '\1_\2').downcase.to_sym end |
#queryable_by(*names) ⇒ Object
384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 |
# File 'lib/cfoundry/v2/model_magic.rb', line 384 def queryable_by(*names) klass = self singular = object_name plural = :"#{singular}s" query = QUERIES[singular] query.module_eval do names.each do |name| define_method(:"#{singular}_by_#{name}") do |*args| send(:"#{plural}_by_#{name}", *args).first end define_method(:"#{plural}_by_#{name}") do |val, *args| , _ = args ||= {} [:query] = [name, val] query_target(klass).send(plural, ) end end end const_set(:Queries, query) ClientMethods.module_eval do include query end end |
#scoped_to_organization(relation = :organization) ⇒ Object
169 170 171 |
# File 'lib/cfoundry/v2/model_magic.rb', line 169 def scoped_to_organization(relation = :organization) @scoped_organization = relation end |
#scoped_to_space(relation = :space) ⇒ Object
173 174 175 |
# File 'lib/cfoundry/v2/model_magic.rb', line 173 def scoped_to_space(relation = :space) @scoped_space = relation end |
#to_many(plural, opts = {}) ⇒ Object
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 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 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 |
# File 'lib/cfoundry/v2/model_magic.rb', line 233 def to_many(plural, opts = {}) to_many_relations[plural] = opts singular = plural.to_s.sub(/s$/, "").to_sym include QUERIES[singular] object = opts[:as] || singular plural_object = :"#{object}s" kls = object.to_s.capitalize.gsub(/(.)_(.)/) do $1 + $2.upcase end define_method(plural) do |*args| opts, _ = args opts ||= {} if opts.empty? && cache = @cache[plural] return cache end if @manifest && @manifest[:entity].key?(plural) && opts.empty? objs = @manifest[:entity][plural] if query = opts[:query] find_by, find_val = query 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}", opts) end if opts.empty? @cache[plural] = res end res end define_method(:"#{plural}_url") do manifest[:entity][:"#{plural}_url"] end define_method(:"add_#{singular}") do |x| ModelMagic.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| ModelMagic.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| klass = CFoundry::V2.const_get(kls) ModelMagic.validate_type(xs, [klass]) @manifest ||= {} @manifest[:entity] ||= {} old = @manifest[:entity][:"#{singular}_guids"] if old != xs.collect(&:guid) old_objs = if all = @manifest[:entity][plural] all.collect do |m| klass.new(@client, m[:metadata][:guid], m) end elsif old old.collect { |id| klass.new(@client, id) } end @changes[name] = [old_objs, val] end @cache[plural] = xs @manifest[:entity][:"#{singular}_guids"] = @diff[:"#{singular}_guids"] = xs.collect(&:guid) end end |
#to_many_relations ⇒ Object
37 38 39 |
# File 'lib/cfoundry/v2/model_magic.rb', line 37 def to_many_relations @to_many_relations ||= {} end |
#to_one(name, opts = {}) ⇒ Object
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 227 228 229 230 231 |
# File 'lib/cfoundry/v2/model_magic.rb', line 177 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) else default end end define_method(:"#{name}_url") do manifest[:entity][:"#{name}_url"] end define_method(:"#{name}=") do |val| klass = CFoundry::V2.const_get(kls) unless has_default && val == default ModelMagic.validate_type(val, klass) end @manifest ||= {} @manifest[:entity] ||= {} old = @manifest[:entity][:"#{name}_guid"] if old != val.guid old_obj = klass.new(@client, old, @manifest[:entity][name]) @changes[name] = [old_obj, val] end @cache[name] = val @manifest[:entity][name] = val.manifest @manifest[:entity][:"#{name}_guid"] = @diff[:"#{name}_guid"] = val && val.guid end end |
#to_one_relations ⇒ Object
33 34 35 |
# File 'lib/cfoundry/v2/model_magic.rb', line 33 def to_one_relations @to_one_relations ||= {} end |