Class: Fog::Model

Inherits:
Object
  • Object
show all
Extended by:
Attributes::ClassMethods
Includes:
Attributes::InstanceMethods, Core::DeprecatedConnectionAccessors
Defined in:
lib/fog/core/model.rb

Direct Known Subclasses

Compute::Server

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Attributes::ClassMethods

_load, aliases, associations, attribute, attributes, default_values, has_many, has_many_identities, has_one, has_one_identity, identity, ignore_attributes, ignored_attributes, masks

Methods included from Core::DeprecatedConnectionAccessors

#connection, #connection=, #prepare_service_value

Methods included from Attributes::InstanceMethods

#_dump, #all_associations, #all_associations_and_attributes, #all_attributes, #associations, #attributes, #dup, #identity, #identity=, #identity_name, #masks, #merge_attributes, #new_record?, #persisted?, #requires, #requires_one

Constructor Details

#initialize(new_attributes = {}) ⇒ Model

Returns a new instance of Model.



13
14
15
16
17
18
19
20
21
22
# 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

Instance Attribute Details

#collectionObject

Returns the value of attribute collection.



10
11
12
# File 'lib/fog/core/model.rb', line 10

def collection
  @collection
end

#serviceObject (readonly)

Returns the value of attribute service.



11
12
13
# File 'lib/fog/core/model.rb', line 11

def service
  @service
end

Instance Method Details

#==(o) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
# File 'lib/fog/core/model.rb', line 32

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

#cacheObject



24
25
26
# File 'lib/fog/core/model.rb', line 24

def cache
  Fog::Cache.new(self)
end

#inspectObject



28
29
30
# File 'lib/fog/core/model.rb', line 28

def inspect
  Fog::Formatador.format(self)
end

#reloadObject



44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/fog/core/model.rb', line 44

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

#symbolize_keys(hash) ⇒ Object



62
63
64
65
66
67
68
# File 'lib/fog/core/model.rb', line 62

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 = {}) ⇒ Object



58
59
60
# File 'lib/fog/core/model.rb', line 58

def to_json(_options = {})
  Fog::JSON.encode(attributes)
end

#wait_for(timeout = Fog.timeout, interval = Fog.interval, &block) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/fog/core/model.rb', line 70

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
  if reload_has_succeeded
    return duration # false if timeout; otherwise {:duration => elapsed time }
  else
    raise Fog::Errors::Error, "Reload failed, #{self.class} #{identity} not present."
  end
end