class CFoundry::V2::Model

Attributes

cache[RW]
changes[RW]
created_at[R]
diff[R]
guid[RW]
updated_at[R]

Public Class Methods

inherited(klass) click to toggle source
Calls superclass method
# File lib/cfoundry/v2/model.rb, line 17
def inherited(klass)
  @@objects[klass.object_name] = klass
  super
end
new(guid, client, manifest = nil, partial = false) click to toggle source
# File lib/cfoundry/v2/model.rb, line 27
def initialize(guid, client, manifest = nil, partial = false)
  @guid = guid
  @client = client
  @manifest = manifest
  @partial = partial
  @cache = {}
  @diff = {}
  @changes = {}
end
objects() click to toggle source
# File lib/cfoundry/v2/model.rb, line 13
def objects
  @@objects
end

Public Instance Methods

==(other)
Alias for: eql?
attribute_for_error(error) click to toggle source
# File lib/cfoundry/v2/model.rb, line 91
def attribute_for_error(error)
  :base
end
changed?() click to toggle source
# File lib/cfoundry/v2/model.rb, line 55
def changed?
  !@changes.empty?
end
create(options = {}) click to toggle source
# File lib/cfoundry/v2/model.rb, line 79
def create(options = {})
  create!(options)
  true
rescue CFoundry::APIError => e
  if e.instance_of? CFoundry::APIError
    errors.add(:base, :cc_client)
  else
    errors.add(attribute_for_error(e), e.message)
  end
  false
end
create!(options = {}) click to toggle source

this does a bit of extra processing to allow for `delete!' followed by `create!'

# File lib/cfoundry/v2/model.rb, line 97
def create!(options = {})
  payload = {}

  @manifest ||= {}
  @manifest[:entity] ||= {}

  @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.empty? && 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
    else
      payload[k] = v
    end
  end

  @manifest = @client.base.post("v2", create_endpoint_name,
    :content => :json,
    :accept => :json,
    :payload => payload,
    :params => options
  )

  @guid = @manifest[:metadata][:guid]

  @diff.clear

  true
end
create_endpoint_name() click to toggle source
# File lib/cfoundry/v2/model.rb, line 137
def create_endpoint_name
  plural_object_name
end
delete(options = {}) click to toggle source
# File lib/cfoundry/v2/model.rb, line 154
def delete(options = {})
  delete!(options)
rescue CFoundry::APIError => e
  if e.instance_of? CFoundry::APIError
    errors.add(:base, :cc_client)
  else
    errors.add(attribute_for_error(e), e.message)
  end
  false
end
delete!(options = {}) click to toggle source
# File lib/cfoundry/v2/model.rb, line 165
def delete!(options = {})
  @client.base.delete("v2", plural_object_name, guid, :params => options)

  @deleted = true

  @diff.clear

  if @manifest
    @manifest.delete :metadata
  end

  true
end
eql?(other) click to toggle source
# File lib/cfoundry/v2/model.rb, line 203
def eql?(other)
  other.is_a?(self.class) && @guid == other.guid
end
Also aliased as: ==
exists?() click to toggle source
# File lib/cfoundry/v2/model.rb, line 191
def exists?
  invalidate!
  manifest
  true
rescue CFoundry::NotFound
  false
end
hash() click to toggle source
# File lib/cfoundry/v2/model.rb, line 209
def hash
  @guid.hash
end
inspect() click to toggle source
# File lib/cfoundry/v2/model.rb, line 59
def inspect
  "\#<#{self.class.name} '#@guid'>"
end
invalidate!() click to toggle source
# File lib/cfoundry/v2/model.rb, line 71
def invalidate!
  @manifest = nil
  @partial = false
  @cache = {}
  @diff = {}
  @changes = {}
end
manifest() click to toggle source
# File lib/cfoundry/v2/model.rb, line 47
def manifest
  @manifest ||= @client.base.send(object_name, @guid)
end
object_name() click to toggle source
# File lib/cfoundry/v2/model.rb, line 63
def object_name
  @object_name ||= self.class.object_name
end
partial?() click to toggle source
# File lib/cfoundry/v2/model.rb, line 51
def partial?
  @partial
end
persisted?() click to toggle source
# File lib/cfoundry/v2/model.rb, line 187
def persisted?
  @guid && !@deleted
end
plural_object_name() click to toggle source
# File lib/cfoundry/v2/model.rb, line 67
def plural_object_name
  @plural_object_name ||= self.class.plural_object_name
end
query_target(klass) click to toggle source
# File lib/cfoundry/v2/model.rb, line 199
def query_target(klass)
  self
end
to_key() click to toggle source
# File lib/cfoundry/v2/model.rb, line 183
def to_key
  persisted? ? [@guid] : nil
end
to_param() click to toggle source
# File lib/cfoundry/v2/model.rb, line 179
def to_param
  persisted? ? @guid.to_s : nil
end
update!(options ={}) click to toggle source
# File lib/cfoundry/v2/model.rb, line 141
def update!(options ={})
  @manifest = @client.base.put("v2", plural_object_name, guid,
    :content => :json,
    :accept => :json,
    :payload => @diff,
    :params => options
  )

  @diff.clear

  true
end