lib/cfoundry/v2/model.rb in cfoundry-0.3.56 vs lib/cfoundry/v2/model.rb in cfoundry-0.3.57

- old
+ new

@@ -31,11 +31,11 @@ end end def validate_type(val, type) unless value_matches?(val, type) - raise "invalid attribute; expected #{type.inspect} but got #{val.inspect}" + raise CFoundry::Mismatch.new(type, val) end end def defaults @defaults ||= {} @@ -60,28 +60,28 @@ if has_default = opts.key?(:default) defaults[name] = default end - define_method(name) { + define_method(name) do return @cache[name] if @cache.key?(name) @cache[name] = manifest[:entity][name] || default - } + end - define_method(:"#{name}=") { |val| + 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 def scoped_to_organization(relation = :organization) @scoped_organization = relation end @@ -102,39 +102,39 @@ if has_default = opts.key?(:default) defaults[:"#{name}_guid"] = default end - define_method(name) { + 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") { + define_method(:"#{name}_url") do manifest[:entity][:"#{name}_url"] - } + end - define_method(:"#{name}=") { |x| + 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 def to_many(plural, opts = {}) to_many_relations[plural] = opts @@ -145,11 +145,11 @@ kls = object.to_s.capitalize.gsub(/(.)_(.)/) do $1 + $2.upcase end - define_method(plural) { |*args| + define_method(plural) do |*args| depth, query = args if !depth && !query && cache = @cache[plural] return cache end @@ -179,52 +179,52 @@ unless depth || query @cache[plural] = res end res - } + end - define_method(:"#{plural}_url") { + define_method(:"#{plural}_url") do manifest[:entity][:"#{plural}_url"] - } + end - define_method(:"add_#{singular}") { |x| + 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}") { |x| + 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}=") { |xs| + 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 def has_summary(actions = {}) define_method(:summary) do @client.base.request_path( @@ -246,14 +246,14 @@ self.send(:"#{key}=", val) elsif self.class.to_many_relations[key] singular = key.to_s.sub(/s$/, "").to_sym - vals = val.collect { |sub| + 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)