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
Instance Attribute Summary collapse
-
#guid ⇒ Object
readonly
Returns the value of attribute guid.
Class Method Summary collapse
- .attribute(name, type, opts = {}) ⇒ Object
- .defaults ⇒ Object
- .to_many(plural, opts = {}) ⇒ Object
- .to_one(name, opts = {}) ⇒ 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) ⇒ Model
constructor
A new instance of Model.
- #inspect ⇒ Object
- #invalidate! ⇒ Object
- #manifest ⇒ Object
- #object_name ⇒ Object
- #update!(diff = {}) ⇒ Object
Constructor Details
#initialize(guid, client, manifest = nil) ⇒ Model
Returns a new instance of Model.
166 167 168 169 170 171 |
# File 'lib/cfoundry/v2/model.rb', line 166 def initialize(guid, client, manifest = nil) @guid = guid @client = client @manifest = manifest @diff = {} end |
Instance Attribute Details
#guid ⇒ Object (readonly)
Returns the value of attribute guid.
164 165 166 |
# File 'lib/cfoundry/v2/model.rb', line 164 def guid @guid end |
Class Method Details
.attribute(name, type, opts = {}) ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/cfoundry/v2/model.rb', line 42 def attribute(name, type, opts = {}) default = opts[:default] || nil has_default = opts.key?(:default) defaults[name] = default if has_default define_method(name) { manifest[:entity][name] || default } define_method(:"#{name}=") { |val| unless has_default && val == default Model.validate_type(val, type) end @manifest ||= {} @manifest[:entity] ||= {} @manifest[:entity][name] = val @diff[name] = val } end |
.defaults ⇒ Object
38 39 40 |
# File 'lib/cfoundry/v2/model.rb', line 38 def defaults @defaults ||= {} end |
.to_many(plural, opts = {}) ⇒ Object
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 149 150 151 152 153 154 155 156 157 158 159 160 161 |
# File 'lib/cfoundry/v2/model.rb', line 95 def to_many(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) { |*args| depth, query = args 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 objs.collect do |json| @client.send(:"make_#{object}", json) end else @client.send( :"#{plural_object}_from", "/v2/#{object_name}s/#@guid/#{plural}", depth || opts[:depth], query) end } define_method(:"#{plural}_url") { manifest[:entity][:"#{plural}_url"] } define_method(:"add_#{singular}") { |x| # TODO: reflect this change in the app manifest? Model.validate_type(x, CFoundry::V2.const_get(kls)) @client.base.request_path( :put, ["v2", "#{object_name}s", @guid, plural, x.guid], nil => :json) } define_method(:"remove_#{singular}") { |x| # TODO: reflect this change in the app manifest? Model.validate_type(x, CFoundry::V2.const_get(kls)) @client.base.request_path( :delete, ["v2", "#{object_name}s", @guid, plural, x.guid], nil => :json) } define_method(:"#{plural}=") { |xs| Model.validate_type(xs, [CFoundry::V2.const_get(kls)]) @manifest ||= {} @manifest[:entity] ||= {} @manifest[:entity][:"#{singular}_guids"] = @diff[:"#{singular}_guids"] = xs.collect(&:guid) } end |
.to_one(name, opts = {}) ⇒ Object
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 |
# File 'lib/cfoundry/v2/model.rb', line 64 def to_one(name, opts = {}) obj = opts[:as] || name kls = obj.to_s.capitalize.gsub(/(.)_(.)/) do $1 + $2.upcase end define_method(name) { if @manifest && @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| Model.validate_type(x, CFoundry::V2.const_get(kls)) @manifest ||= {} @manifest[:entity] ||= {} @manifest[:entity][:"#{name}_guid"] = @diff[:"#{name}_guid"] = x.guid } end |
.validate_type(val, type) ⇒ Object
32 33 34 35 36 |
# File 'lib/cfoundry/v2/model.rb', line 32 def validate_type(val, type) unless value_matches?(val, type) raise "invalid attribute; expected #{type.inspect} but got #{val.inspect}" end end |
.value_matches?(val, type) ⇒ Boolean
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/cfoundry/v2/model.rb', line 6 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!’
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 |
# File 'lib/cfoundry/v2/model.rb', line 195 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
240 241 242 243 244 245 246 247 248 249 250 251 252 |
# File 'lib/cfoundry/v2/model.rb', line 240 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: ==
261 262 263 |
# File 'lib/cfoundry/v2/model.rb', line 261 def eql?(other) other.is_a?(self.class) && @guid == other.guid end |
#exists? ⇒ Boolean
254 255 256 257 258 259 |
# File 'lib/cfoundry/v2/model.rb', line 254 def exists? @client.base.send(object_name, @guid) true rescue CFoundry::APIError # TODO: NotFound would be better false end |
#hash ⇒ Object
266 267 268 |
# File 'lib/cfoundry/v2/model.rb', line 266 def hash @guid.hash end |
#inspect ⇒ Object
177 178 179 |
# File 'lib/cfoundry/v2/model.rb', line 177 def inspect "\#<#{self.class.name} '#@guid'>" end |
#invalidate! ⇒ Object
188 189 190 191 |
# File 'lib/cfoundry/v2/model.rb', line 188 def invalidate! @manifest = nil @diff = {} end |
#manifest ⇒ Object
173 174 175 |
# File 'lib/cfoundry/v2/model.rb', line 173 def manifest @manifest ||= @client.base.send(object_name, @guid) end |
#object_name ⇒ Object
181 182 183 184 185 186 |
# File 'lib/cfoundry/v2/model.rb', line 181 def object_name @object_name ||= self.class.name.split("::").last.gsub( /([a-z])([A-Z])/, '\1_\2').downcase end |
#update!(diff = {}) ⇒ Object
230 231 232 233 234 235 236 237 238 |
# File 'lib/cfoundry/v2/model.rb', line 230 def update!(diff = {}) diff = @diff.merge(diff) @manifest = @client.base.send(:"update_#{object_name}", @guid, diff) @diff.clear if diff == @diff true end |