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
- #define_base_client_methods(&blk) ⇒ Object
- #define_client_methods(&blk) ⇒ Object
- #has_summary(actions = {}) ⇒ Object
- #inherited(klass) ⇒ Object
- #object_name ⇒ Object
- #plural_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.
18 19 20 |
# File 'lib/cfoundry/v2/model_magic.rb', line 18 def scoped_organization @scoped_organization end |
#scoped_space ⇒ Object (readonly)
Returns the value of attribute scoped_space.
18 19 20 |
# File 'lib/cfoundry/v2/model_magic.rb', line 18 def scoped_space @scoped_space end |
Class Method Details
.params_from(args) ⇒ Object
431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 |
# File 'lib/cfoundry/v2/model_magic.rb', line 431 def self.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 |
Instance Method Details
#attribute(name, type, opts = {}) ⇒ Object
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 |
# File 'lib/cfoundry/v2/model_magic.rb', line 150 def attribute(name, type, opts = {}) attributes[name] = opts json_name = opts[:at] || name 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] = if manifest[:entity].key?(json_name) manifest[:entity][json_name] else default end end define_method(:"#{name}=") do |val| unless has_default && val == default CFoundry::Validator.validate_type(val, type) end @cache[name] = val @manifest ||= {} @manifest[:entity] ||= {} old = @manifest[:entity][json_name] @changes[name] = [old, val] if old != val @manifest[:entity][json_name] = val @diff[json_name] = val end end |
#attributes ⇒ Object
43 44 45 |
# File 'lib/cfoundry/v2/model_magic.rb', line 43 def attributes @attributes ||= {} end |
#defaults ⇒ Object
39 40 41 |
# File 'lib/cfoundry/v2/model_magic.rb', line 39 def defaults @defaults ||= {} end |
#define_base_client_methods(&blk) ⇒ Object
35 36 37 |
# File 'lib/cfoundry/v2/model_magic.rb', line 35 def define_base_client_methods(&blk) BaseClientMethods.module_eval(&blk) end |
#define_client_methods(&blk) ⇒ Object
31 32 33 |
# File 'lib/cfoundry/v2/model_magic.rb', line 31 def define_client_methods(&blk) ClientMethods.module_eval(&blk) end |
#has_summary(actions = {}) ⇒ Object
361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 |
# File 'lib/cfoundry/v2/model_magic.rb', line 361 def has_summary(actions = {}) define_method(:summary) do @client.base.get("v2", plural_object_name, @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
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 135 136 137 138 139 140 141 142 143 144 145 146 147 148 |
# File 'lib/cfoundry/v2/model_magic.rb', line 55 def inherited(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(:"create_#{singular}") do |payload| post("v2", plural, :content => :json, :accept => :json, :payload => payload) end define_method(:"delete_#{singular}") do |guid| delete("v2", plural, guid) true end define_method(:"update_#{singular}") do |guid, payload| put("v2", plural, guid, :content => :json, :accept => :json, :payload => payload) end define_method(plural) do |*args| all_pages( 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) # 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.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 has_summary end |
#object_name ⇒ Object
20 21 22 23 24 25 |
# File 'lib/cfoundry/v2/model_magic.rb', line 20 def object_name @object_name ||= name.split("::").last.gsub( /([a-z])([A-Z])/, '\1_\2').downcase.to_sym end |
#plural_object_name ⇒ Object
27 28 29 |
# File 'lib/cfoundry/v2/model_magic.rb', line 27 def plural_object_name :"#{object_name}s" end |
#queryable_by(*names) ⇒ Object
401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 |
# File 'lib/cfoundry/v2/model_magic.rb', line 401 def queryable_by(*names) klass = self singular = object_name plural = plural_object_name 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
189 190 191 |
# File 'lib/cfoundry/v2/model_magic.rb', line 189 def scoped_to_organization(relation = :organization) @scoped_organization = relation end |
#scoped_to_space(relation = :space) ⇒ Object
193 194 195 |
# File 'lib/cfoundry/v2/model_magic.rb', line 193 def scoped_to_space(relation = :space) @scoped_space = relation end |
#to_many(plural, opts = {}) ⇒ Object
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 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 |
# File 'lib/cfoundry/v2/model_magic.rb', line 253 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 kls = object.to_s.capitalize.gsub(/(.)_(.)/) do $1 + $2.upcase end define_method(plural) do |*args| klass = CFoundry::V2.const_get(kls) 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( :"#{klass.plural_object_name}_from", "/v2/#{plural_object_name}/#@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| klass = self.class.objects[object] CFoundry::Validator.validate_type(x, klass) if cache = @cache[plural] cache << x unless cache.include?(x) end @client.base.put("v2", plural_object_name, @guid, plural, x.guid, :accept => :json) end define_method(:"remove_#{singular}") do |x| klass = self.class.objects[object] CFoundry::Validator.validate_type(x, klass) if cache = @cache[plural] cache.delete(x) end @client.base.delete("v2", plural_object_name, @guid, plural, x.guid, :accept => :json) end define_method(:"#{plural}=") do |xs| klass = self.class.objects[object] CFoundry::Validator.validate_type(xs, [klass]) @manifest ||= {} @manifest[:entity] ||= {} old = @manifest[:entity][:"#{singular}_guids"] if old != xs.collect(&:guid) old_objs = @cache[plural] || 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[plural] = [old_objs, xs] end @cache[plural] = xs @manifest[:entity][:"#{singular}_guids"] = @diff[:"#{singular}_guids"] = xs.collect(&:guid) end end |
#to_many_relations ⇒ Object
51 52 53 |
# File 'lib/cfoundry/v2/model_magic.rb', line 51 def to_many_relations @to_many_relations ||= {} end |
#to_one(name, opts = {}) ⇒ Object
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 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 |
# File 'lib/cfoundry/v2/model_magic.rb', line 197 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 = self.class.objects[obj] unless has_default && val == default CFoundry::Validator.validate_type(val, klass) end @manifest ||= {} @manifest[:entity] ||= {} old = @manifest[:entity][:"#{name}_guid"] if old != (val && val.guid) old_obj = @cache[name] || klass.new(@client, old, @manifest[:entity][name]) @changes[name] = [old_obj, val] end @cache[name] = val @manifest[:entity][:"#{name}_guid"] = @diff[:"#{name}_guid"] = val && val.guid end end |
#to_one_relations ⇒ Object
47 48 49 |
# File 'lib/cfoundry/v2/model_magic.rb', line 47 def to_one_relations @to_one_relations ||= {} end |