Class: CFoundry::V2::Model
- Inherits:
-
Object
- Object
- CFoundry::V2::Model
- Defined in:
- lib/cfoundry/v2/model.rb
Direct Known Subclasses
App, AppSpace, Domain, Framework, Organization, Runtime, Service, ServiceBinding, ServiceInstance, ServicePlan, User
Instance Attribute Summary collapse
-
#id ⇒ Object
readonly
Returns the value of attribute id.
Class Method Summary collapse
- .attribute(name, opts = {}) ⇒ Object
- .defaults ⇒ Object
- .to_many(plural, opts = {}) ⇒ Object
- .to_one(name, opts = {}) ⇒ Object
Instance Method Summary collapse
- #==(other) ⇒ Object
- #create! ⇒ Object
- #delete! ⇒ Object
- #exists? ⇒ Boolean
-
#initialize(id, client, manifest = nil) ⇒ Model
constructor
A new instance of Model.
- #inspect ⇒ Object
- #manifest ⇒ Object
- #object_name ⇒ Object
- #update!(diff = @diff) ⇒ Object
Constructor Details
#initialize(id, client, manifest = nil) ⇒ Model
Returns a new instance of Model.
99 100 101 102 103 104 |
# File 'lib/cfoundry/v2/model.rb', line 99 def initialize(id, client, manifest = nil) @id = id @client = client @manifest = manifest @diff = {} end |
Instance Attribute Details
#id ⇒ Object (readonly)
Returns the value of attribute id.
97 98 99 |
# File 'lib/cfoundry/v2/model.rb', line 97 def id @id end |
Class Method Details
.attribute(name, opts = {}) ⇒ Object
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/cfoundry/v2/model.rb', line 8 def attribute(name, opts = {}) default = opts[:default] || nil defaults[name] = default if default define_method(name) { manifest[:entity][name] || default } define_method(:"#{name}=") { |val| @manifest ||= {} @manifest[:entity] ||= {} @manifest[:entity][name] = val @diff[name] = val } end |
.defaults ⇒ Object
4 5 6 |
# File 'lib/cfoundry/v2/model.rb', line 4 def defaults @defaults ||= {} end |
.to_many(plural, opts = {}) ⇒ Object
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 |
# File 'lib/cfoundry/v2/model.rb', line 50 def to_many(plural, opts = {}) singular = plural.to_s.sub(/s$/, "").to_sym object = opts[:as] || singular plural_object = object.to_s.sub(/s$/, "").to_sym define_method(plural) { if manifest[:entity].key? plural manifest[:entity][plural].collect do |json| @client.send(:"make_#{object}", json) end else @client.send( :"#{plural_object}_from", send("#{plural}_url"), opts[:depth] || 1) end } define_method(:"#{plural}_url") { manifest[:entity][:"#{plural}_url"] } # TODO: these are hacky define_method(:"add_#{singular}") { |x| @client.base.request_path( :put, ["v2", "#{object_name}s", @id, plural, x.id], nil => :json) } define_method(:"remove_#{singular}") { @client.base.request_path( :delete, ["v2", "#{object_name}s", @id, plural, x.id], nil => :json) } define_method(:"#{plural}=") { |xs| @manifest ||= {} @manifest[:entity] ||= {} @manifest[:entity][:"#{singular}_guids"] = @diff[:"#{singular}_guids"] = xs.collect(&:id) } end |
.to_one(name, opts = {}) ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/cfoundry/v2/model.rb', line 24 def to_one(name, opts = {}) obj = opts[:as] || name define_method(name) { if manifest[:entity].key? name @client.send(:"make_#{obj}", manifest[:entity][name]) else @client.send( :"#{obj}_from", send("#{name}_url"), opts[:depth] || 1) end } define_method(:"#{name}_url") { manifest[:entity][:"#{name}_url"] } define_method(:"#{name}=") { |x| @manifest ||= {} @manifest[:entity] ||= {} @manifest[:entity][:"#{name}_guid"] = @diff[:"#{name}_guid"] = x.id } end |
Instance Method Details
#==(other) ⇒ Object
154 155 156 |
# File 'lib/cfoundry/v2/model.rb', line 154 def ==(other) other.is_a?(self.class) && @id == other.id end |
#create! ⇒ Object
122 123 124 125 126 127 128 129 130 131 |
# File 'lib/cfoundry/v2/model.rb', line 122 def create! @manifest = @client.base.send( :"create_#{object_name}", self.class.defaults.merge(@manifest[:entity])) @id = @manifest[:metadata][:guid] true end |
#delete! ⇒ Object
139 140 141 142 143 144 145 |
# File 'lib/cfoundry/v2/model.rb', line 139 def delete! @client.base.send(:"delete_#{object_name}", @id) if @manifest @manifest.delete :metadata end end |
#exists? ⇒ Boolean
147 148 149 150 151 152 |
# File 'lib/cfoundry/v2/model.rb', line 147 def exists? @client.base.send(object_name, @id) true rescue CFoundry::APIError # TODO: NotFound would be better false end |
#inspect ⇒ Object
111 112 113 |
# File 'lib/cfoundry/v2/model.rb', line 111 def inspect "\#<#{self.class.name} '#@id'>" end |
#manifest ⇒ Object
106 107 108 109 |
# File 'lib/cfoundry/v2/model.rb', line 106 def manifest # inline depth of 2 for fewer requests @manifest ||= @client.base.send(object_name, @id, 2) end |
#object_name ⇒ Object
115 116 117 118 119 120 |
# File 'lib/cfoundry/v2/model.rb', line 115 def object_name @object_name ||= self.class.name.split("::").last.gsub( /([a-z])([A-Z])/, '\1_\2').downcase end |
#update!(diff = @diff) ⇒ Object
133 134 135 136 137 |
# File 'lib/cfoundry/v2/model.rb', line 133 def update!(diff = @diff) @client.base.send(:"update_#{object_name}", @id, diff) @manifest = nil end |