Class: Fog::Collection
- Inherits:
-
Array
- Object
- Array
- Fog::Collection
show all
- Defined in:
- lib/fog/collection.rb
Direct Known Subclasses
AWS::EC2::Addresses, AWS::EC2::Instances, AWS::EC2::KeyPairs, AWS::EC2::SecurityGroups, AWS::EC2::Snapshots, AWS::EC2::Volumes, AWS::S3::Buckets, AWS::S3::Objects, Rackspace::Servers::Flavors, Rackspace::Servers::Images, Rackspace::Servers::Servers
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(attributes = {}) ⇒ Collection
Returns a new instance of Collection.
57
58
59
|
# File 'lib/fog/collection.rb', line 57
def initialize(attributes = {})
merge_attributes(attributes)
end
|
Class Method Details
._load(marhsalled) ⇒ Object
4
5
6
|
# File 'lib/fog/collection.rb', line 4
def self._load(marhsalled)
new(Marshal.load(marshalled))
end
|
.aliases ⇒ Object
23
24
25
|
# File 'lib/fog/collection.rb', line 23
def self.aliases
@aliases ||= {}
end
|
.attribute(name, other_names = []) ⇒ Object
8
9
10
11
12
13
14
15
16
17
|
# File 'lib/fog/collection.rb', line 8
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
27
28
29
|
# File 'lib/fog/collection.rb', line 27
def self.attributes
@attributes ||= []
end
|
.model(new_model) ⇒ Object
19
20
21
|
# File 'lib/fog/collection.rb', line 19
def self.model(new_model)
@model = new_model
end
|
Instance Method Details
#_dump ⇒ Object
31
32
33
|
# File 'lib/fog/collection.rb', line 31
def _dump
Marshal.dump(attributes)
end
|
#attributes ⇒ Object
35
36
37
38
39
40
41
|
# File 'lib/fog/collection.rb', line 35
def attributes
attributes = {}
for attribute in self.class.attributes
attributes[attribute] = send("#{attribute}")
end
attributes
end
|
#connection ⇒ Object
47
48
49
|
# File 'lib/fog/collection.rb', line 47
def connection
@connection
end
|
#connection=(new_connection) ⇒ Object
43
44
45
|
# File 'lib/fog/collection.rb', line 43
def connection=(new_connection)
@connection = new_connection
end
|
#create(attributes = {}) ⇒ Object
51
52
53
54
55
|
# File 'lib/fog/collection.rb', line 51
def create(attributes = {})
object = new(attributes)
object.save
object
end
|
#inspect ⇒ Object
61
62
63
64
65
66
67
68
69
70
71
72
|
# File 'lib/fog/collection.rb', line 61
def inspect
data = "#<#{self.class.name}"
for attribute in self.class.attributes
data << " #{attribute}=#{send(attribute).inspect}"
end
data << " ["
for member in self
data << "#{member.inspect},"
end
data.chop! unless self.empty?
data << "]>"
end
|
#merge_attributes(new_attributes = {}) ⇒ Object
78
79
80
81
82
83
84
85
86
87
|
# File 'lib/fog/collection.rb', line 78
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
|
#model ⇒ Object
74
75
76
|
# File 'lib/fog/collection.rb', line 74
def model
self.class.instance_variable_get('@model')
end
|
#new(attributes = {}) ⇒ Object
89
90
91
92
93
94
95
96
|
# File 'lib/fog/collection.rb', line 89
def new(attributes = {})
model.new(
attributes.merge!(
:collection => self,
:connection => connection
)
)
end
|
#reload ⇒ Object
98
99
100
|
# File 'lib/fog/collection.rb', line 98
def reload
self.clear.concat(all)
end
|