Class: Fog::Model
- Inherits:
-
Object
- Object
- Fog::Model
- Extended by:
- Attributes::ClassMethods
- Defined in:
- lib/fog/core/model.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#collection ⇒ Object
Returns the value of attribute collection.
-
#service ⇒ Object
readonly
Returns the value of attribute service.
Instance Method Summary collapse
- #==(o) ⇒ Object
- #cache ⇒ Object
-
#create ⇒ self
Creates new entity from model.
-
#destroy ⇒ self
Destroys entity by model identity.
-
#initialize(new_attributes = {}) ⇒ Model
constructor
A new instance of Model.
- #inspect ⇒ Object
- #reload ⇒ self?
-
#save ⇒ self
Creates new or updates existing model.
- #symbolize_keys(hash) ⇒ Object
- #to_json(_options = {}) ⇒ Object
-
#update ⇒ self
Updates new entity with model.
- #wait_for(timeout = Fog.timeout, interval = Fog.interval, &block) ⇒ Object
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
#collection ⇒ Object
Returns the value of attribute collection.
10 11 12 |
# File 'lib/fog/core/model.rb', line 10 def collection @collection end |
#service ⇒ Object (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 |
#create ⇒ self
Creates new entity from model
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 |
#destroy ⇒ self
Destroys entity by model identity
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 |
#inspect ⇒ Object
55 56 57 |
# File 'lib/fog/core/model.rb', line 55 def inspect Fog::Formatador.format(self) end |
#reload ⇒ self?
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 |
#save ⇒ self
Creates new or updates existing model
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 |, (key, value)| [(key.to_sym rescue key) || key] = value end end |
#to_json(_options = {}) ⇒ Object
87 88 89 |
# File 'lib/fog/core/model.rb', line 87 def to_json( = {}) Fog::JSON.encode(attributes) end |
#update ⇒ self
Updates new entity with model
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
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 |