class Fog::Model

Attributes

collection[RW]
service[R]

Public Class Methods

new(new_attributes = {}) click to toggle source
# File lib/fog/core/model.rb, line 13
def initialize(new_attributes = {})
  # TODO: Remove compatibility with old connection option
  attribs = new_attributes.clone
  @service = attribs.delete(:service)
  if @service.nil? && attribs[:connection]
    Fog::Logger.deprecation("Passing :connection option is deprecated, use :service instead [light_black](#{caller.first})[/]")
    @service = attribs[:connection]
  end
  merge_attributes(attribs)
end

Public Instance Methods

==(o) click to toggle source
Calls superclass method
# File lib/fog/core/model.rb, line 59
def ==(o)
  unless o.is_a?(Fog::Model)
    super
  else
    if (o.identity.nil? and self.identity.nil?)
      o.object_id == self.object_id
    else
      o.class == self.class and o.identity == self.identity
    end
  end
end
cache() click to toggle source
# File lib/fog/core/model.rb, line 51
def cache
  Fog::Cache.new(self)
end
create() click to toggle source

Creates new entity from model @raise [Fog::Errors::NotImplemented] you must implement create method in child class and return self @return [self]

# File lib/fog/core/model.rb, line 33
def create
  raise Fog::Errors::NotImplemented, "Implement method #create for #{self.class}. Method must return self"
end
destroy() click to toggle source

Destroys entity by model identity @raise [Fog::Errors::NotImplemented] you must implement destroy method in child class and return self @return [self]

# File lib/fog/core/model.rb, line 47
def destroy
  raise Fog::Errors::NotImplemented, "Implement method #destroy for #{self.class}. Method must return self"
end
inspect() click to toggle source
# File lib/fog/core/model.rb, line 55
def inspect
  Fog::Formatador.format(self)
end
reload() click to toggle source

@return [self] if model successfully reloaded @return [nil] if something went wrong or model was not found

# File lib/fog/core/model.rb, line 73
def reload
  requires :identity

  object = collection.get(identity)

  return unless object

  merge_attributes(object.all_associations_and_attributes)

  self
rescue Excon::Errors::SocketError
  nil
end
save() click to toggle source

Creates new or updates existing model @return [self]

# File lib/fog/core/model.rb, line 26
def save
  persisted? ? update : create
end
symbolize_keys(hash) click to toggle source
# File lib/fog/core/model.rb, line 91
def symbolize_keys(hash)
  return nil if hash.nil?

  hash.reduce({}) do |options, (key, value)|
    options[(key.to_sym rescue key) || key] = value
    options
  end
end
to_json(_options = {}) click to toggle source
# File lib/fog/core/model.rb, line 87
def to_json(_options = {})
  Fog::JSON.encode(attributes)
end
update() click to toggle source

Updates new entity with model @raise [Fog::Errors::NotImplemented] you must implement update method in child class and return self @return [self]

# File lib/fog/core/model.rb, line 40
def update
  raise Fog::Errors::NotImplemented, "Implement method #update for #{self.class}. Method must return self"
end
wait_for(timeout = Fog.timeout, interval = Fog.interval, &block) click to toggle source
# File lib/fog/core/model.rb, line 100
def wait_for(timeout = Fog.timeout, interval = Fog.interval, &block)
  reload_has_succeeded = false

  duration = Fog.wait_for(timeout, interval) do # Note that duration = false if it times out
    if reload
      reload_has_succeeded = true
      instance_eval(&block)
    else
      false
    end
  end
  raise Fog::Errors::Error, "Reload failed, #{self.class} #{identity} not present." unless reload_has_succeeded

  duration # false if timeout; otherwise {:duration => elapsed time }
end