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::Flavors, AWS::EC2::Images, AWS::EC2::KeyPairs, AWS::EC2::SecurityGroups, AWS::EC2::Servers, AWS::EC2::Snapshots, AWS::EC2::Volumes, AWS::S3::Directories, AWS::S3::Files, Local::Directories, Local::Files, Rackspace::Files::Directories, Rackspace::Files::Files, Rackspace::Servers::Flavors, Rackspace::Servers::Images, Rackspace::Servers::Servers, Slicehost::Flavors, Slicehost::Images, Slicehost::Servers, Terremark::Shared::Addresses, Terremark::Shared::Networks, Terremark::Shared::Servers, Terremark::Shared::Tasks, Terremark::Shared::Vdcs, Vcloud::Collection
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(attributes = {}) ⇒ Collection
Returns a new instance of Collection.
80
81
82
83
|
# File 'lib/fog/collection.rb', line 80
def initialize(attributes = {})
merge_attributes(attributes)
@loaded = false
end
|
Class Method Details
._load(marshalled) ⇒ Object
23
24
25
|
# File 'lib/fog/collection.rb', line 23
def self._load(marshalled)
new(Marshal.load(marshalled))
end
|
.aliases ⇒ Object
46
47
48
|
# File 'lib/fog/collection.rb', line 46
def self.aliases
@aliases ||= {}
end
|
.attribute(name, other_names = []) ⇒ Object
27
28
29
30
31
32
33
34
35
36
|
# File 'lib/fog/collection.rb', line 27
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
50
51
52
|
# File 'lib/fog/collection.rb', line 50
def self.attributes
@attributes ||= []
end
|
.model(new_model = nil) ⇒ Object
38
39
40
41
42
43
44
|
# File 'lib/fog/collection.rb', line 38
def self.model(new_model=nil)
if new_model == nil
@model
else
@model = new_model
end
end
|
Instance Method Details
#_dump ⇒ Object
54
55
56
|
# File 'lib/fog/collection.rb', line 54
def _dump
Marshal.dump(attributes)
end
|
#attributes ⇒ Object
58
59
60
61
62
63
64
|
# File 'lib/fog/collection.rb', line 58
def attributes
attributes = {}
for attribute in self.class.attributes
attributes[attribute] = send("#{attribute}")
end
attributes
end
|
#connection ⇒ Object
70
71
72
|
# File 'lib/fog/collection.rb', line 70
def connection
@connection
end
|
#connection=(new_connection) ⇒ Object
66
67
68
|
# File 'lib/fog/collection.rb', line 66
def connection=(new_connection)
@connection = new_connection
end
|
#create(attributes = {}) ⇒ Object
74
75
76
77
78
|
# File 'lib/fog/collection.rb', line 74
def create(attributes = {})
object = new(attributes)
object.save
object
end
|
#inspect ⇒ Object
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
|
# File 'lib/fog/collection.rb', line 85
def inspect
Thread.current[:formatador] ||= Formatador.new
data = "#{Thread.current[:formatador].indentation}<#{self.class.name}\n"
Thread.current[:formatador].indent do
unless self.class.attributes.empty?
data << "#{Thread.current[:formatador].indentation}"
data << self.class.attributes.map {|attribute| "#{attribute}=#{send(attribute).inspect}"}.join(",\n#{Thread.current[:formatador].indentation}")
data << "\n"
end
data << "#{Thread.current[:formatador].indentation}["
unless self.empty?
data << "\n"
Thread.current[:formatador].indent do
data << self.map {|member| member.inspect}.join(",\n")
data << "\n"
end
data << Thread.current[:formatador].indentation
end
data << "]\n"
end
data << "#{Thread.current[:formatador].indentation}>"
data
end
|
#load(objects) ⇒ Object
109
110
111
112
113
114
115
116
117
118
|
# File 'lib/fog/collection.rb', line 109
def load(objects)
if @loaded
clear
end
@loaded = true
for object in objects
self << new(object)
end
self
end
|
#merge_attributes(new_attributes = {}) ⇒ Object
124
125
126
127
128
129
130
131
132
133
|
# File 'lib/fog/collection.rb', line 124
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
120
121
122
|
# File 'lib/fog/collection.rb', line 120
def model
self.class.instance_variable_get('@model')
end
|
#new(attributes = {}) ⇒ Object
135
136
137
138
139
140
141
142
|
# File 'lib/fog/collection.rb', line 135
def new(attributes = {})
model.new(
attributes.merge(
:collection => self,
:connection => connection
)
)
end
|
#reload ⇒ Object
144
145
146
|
# File 'lib/fog/collection.rb', line 144
def reload
self.clear.concat(all)
end
|
#table(attributes = nil) ⇒ Object
148
149
150
|
# File 'lib/fog/collection.rb', line 148
def table(attributes = nil)
Formatador.display_table(self.map {|instance| instance.attributes}, attributes)
end
|
#to_json ⇒ Object
152
153
154
|
# File 'lib/fog/collection.rb', line 152
def to_json
self.map {|member| member}.to_json
end
|