Class: Fog::Model

Inherits:
Object
  • Object
show all
Defined in:
lib/fog/model.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(new_attributes = {}) ⇒ Model

Returns a new instance of Model.



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

def initialize(new_attributes = {})
  merge_attributes(new_attributes)
end

Instance Attribute Details

#connectionObject

Returns the value of attribute connection.



36
37
38
# File 'lib/fog/model.rb', line 36

def connection
  @connection
end

Class Method Details

._load(marshalled) ⇒ Object



4
5
6
# File 'lib/fog/model.rb', line 4

def self._load(marshalled)
  new(Marshal.load(marshalled))
end

.aliasesObject



8
9
10
# File 'lib/fog/model.rb', line 8

def self.aliases
  @aliases ||= {}
end

.attribute(name, other_names = []) ⇒ Object



16
17
18
19
20
21
22
23
24
25
# File 'lib/fog/model.rb', line 16

def self.attribute(name, other_names = [])
  class_eval <<-EOS, __FILE__, __LINE__
    attr_accessor :#{name}
  EOS
  @attributes ||= []
  @attributes |= [name]
  for other_name in [*other_names]
    aliases[other_name] = name
  end
end

.attributesObject



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

def self.attributes
  @attributes ||= []
end

.identity(name, other_names = []) ⇒ Object



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

def self.identity(name, other_names = [])
  @identity = name
  self.attribute(name, other_names)
end

Instance Method Details

#_dumpObject



32
33
34
# File 'lib/fog/model.rb', line 32

def _dump
  Marshal.dump(attributes)
end

#attributesObject



38
39
40
41
42
43
44
# File 'lib/fog/model.rb', line 38

def attributes
  attributes = {}
  for attribute in self.class.attributes
    attributes[attribute] = send("#{attribute}")
  end
  attributes
end

#collectionObject



46
47
48
# File 'lib/fog/model.rb', line 46

def collection
  @collection
end

#identityObject



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

def identity
  send(self.class.instance_variable_get('@identity'))
end

#inspectObject



58
59
60
61
62
63
64
# File 'lib/fog/model.rb', line 58

def inspect
  data = "#<#{self.class.name}"
  for attribute in self.class.attributes
    data << " #{attribute}=#{send(attribute).inspect}"
  end
  data << ">"
end

#merge_attributes(new_attributes = {}) ⇒ Object



66
67
68
69
70
71
72
73
74
75
# File 'lib/fog/model.rb', line 66

def merge_attributes(new_attributes = {})
  for key, value in new_attributes
    if aliased_key = self.class.aliases[key]
      send("#{aliased_key}=", value)
    else
      send("#{key}=", value)
    end
  end
  self
end

#new_record?Boolean

Returns:

  • (Boolean)


77
78
79
# File 'lib/fog/model.rb', line 77

def new_record?
  !identity
end

#reloadObject



81
82
83
84
# File 'lib/fog/model.rb', line 81

def reload
  new_attributes = collection.get(identity).attributes
  merge_attributes(new_attributes)
end

#requires(*args) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/fog/model.rb', line 86

def requires(*args)
  missing = []
  for arg in [:connection] | args
    missing << arg unless send("#{arg}")
  end
  unless missing.empty?
    if missing.length == 1
      raise(ArgumentError, "#{missing.first} is required for this operation")
    else
      raise(ArgumentError, "#{missing[0...-1].join(", ")} and #{missing[-1]} are required for this operation")
    end
  end
end

#wait_for(timeout = 600, &block) ⇒ Object



100
101
102
103
104
105
106
107
108
109
# File 'lib/fog/model.rb', line 100

def wait_for(timeout = 600, &block)
  start = Time.now
  until instance_eval(&block)
    if Time.now - start > timeout
      break
    end
    reload
    sleep(1)
  end
end