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, #filter_attributes, #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



59
60
61
62
63
64
65
66
67
68
69
# 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

#cacheObject



51
52
53
# File 'lib/fog/core/model.rb', line 51

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

#createself

Creates new entity from model

Returns:

  • (self)

Raises:



33
34
35
# 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

#destroyself

Destroys entity by model identity

Returns:

  • (self)

Raises:



47
48
49
# 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

#inspectObject



55
56
57
# File 'lib/fog/core/model.rb', line 55

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

#reloadself?

Returns:

  • (self)

    if model successfully reloaded

  • (nil)

    if something went wrong or model was not found



73
74
75
76
77
78
79
80
81
82
83
84
85
# 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

#saveself

Creates new or updates existing model

Returns:

  • (self)


26
27
28
# File 'lib/fog/core/model.rb', line 26

def save
  persisted? ? update : create
end

#symbolize_keys(hash) ⇒ Object



91
92
93
94
95
96
97
98
# 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 = {}) ⇒ Object



87
88
89
# File 'lib/fog/core/model.rb', line 87

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

#updateself

Updates new entity with model

Returns:

  • (self)

Raises:



40
41
42
# 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) ⇒ Object

Raises:



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# 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