Class: Fog::Model
- Inherits:
-
Object
show all
- Defined in:
- lib/fog/model.rb
Direct Known Subclasses
AWS::EC2::Address, AWS::EC2::Flavor, AWS::EC2::Image, AWS::EC2::KeyPair, AWS::EC2::SecurityGroup, AWS::EC2::Server, AWS::EC2::Snapshot, AWS::EC2::Volume, AWS::S3::Directory, AWS::S3::File, Rackspace::Files::Directory, Rackspace::Files::File, Rackspace::Servers::Flavor, Rackspace::Servers::Image, Rackspace::Servers::Server, Slicehost::Flavor, Slicehost::Image, Slicehost::Server
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
#connection ⇒ Object
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
|
.aliases ⇒ Object
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
|
.attributes ⇒ Object
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
#_dump ⇒ Object
32
33
34
|
# File 'lib/fog/model.rb', line 32
def _dump
Marshal.dump(attributes)
end
|
#attributes ⇒ Object
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
|
#collection ⇒ Object
46
47
48
|
# File 'lib/fog/model.rb', line 46
def collection
@collection
end
|
#identity ⇒ Object
50
51
52
|
# File 'lib/fog/model.rb', line 50
def identity
send(self.class.instance_variable_get('@identity'))
end
|
#inspect ⇒ Object
58
59
60
61
62
63
64
65
66
67
68
69
|
# File 'lib/fog/model.rb', line 58
def inspect
Thread.current[:formatador] ||= Formatador.new
data = "#{Thread.current[:formatador].indentation}<#{self.class.name}"
Thread.current[:formatador].indent do
unless self.class.attributes.empty?
data << "\n#{Thread.current[:formatador].indentation}"
data << self.class.attributes.map {|attribute| "#{attribute}=#{send(attribute).inspect}"}.join(",\n#{Thread.current[:formatador].indentation}")
end
end
data << "\n#{Thread.current[:formatador].indentation}>"
data
end
|
#merge_attributes(new_attributes = {}) ⇒ Object
71
72
73
74
75
76
77
78
79
80
|
# File 'lib/fog/model.rb', line 71
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
82
83
84
|
# File 'lib/fog/model.rb', line 82
def new_record?
!identity
end
|
#reload ⇒ Object
86
87
88
89
|
# File 'lib/fog/model.rb', line 86
def reload
new_attributes = collection.get(identity).attributes
merge_attributes(new_attributes)
end
|
#requires(*args) ⇒ Object
91
92
93
94
95
96
97
98
99
100
101
102
103
|
# File 'lib/fog/model.rb', line 91
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
|
#to_json ⇒ Object
105
106
107
|
# File 'lib/fog/model.rb', line 105
def to_json
attributes.to_json
end
|
#wait_for(timeout = 600, &block) ⇒ Object
109
110
111
112
113
114
115
116
117
118
|
# File 'lib/fog/model.rb', line 109
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
|