Class: Fog::Collection

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ Collection

Returns a new instance of Collection.



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

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

.aliasesObject



42
43
44
# File 'lib/fog/collection.rb', line 42

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

.attributesObject



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

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

.model(new_model) ⇒ Object



38
39
40
# File 'lib/fog/collection.rb', line 38

def self.model(new_model)
  @model = new_model
end

Instance Method Details

#_dumpObject



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

def _dump
  Marshal.dump(attributes)
end

#attributesObject



54
55
56
57
58
59
60
# File 'lib/fog/collection.rb', line 54

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

#connectionObject



66
67
68
# File 'lib/fog/collection.rb', line 66

def connection
  @connection
end

#connection=(new_connection) ⇒ Object



62
63
64
# File 'lib/fog/collection.rb', line 62

def connection=(new_connection)
  @connection = new_connection
end

#create(attributes = {}) ⇒ Object



70
71
72
73
74
# File 'lib/fog/collection.rb', line 70

def create(attributes = {})
  object = new(attributes)
  object.save
  object
end

#inspectObject



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/fog/collection.rb', line 81

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



105
106
107
108
109
110
111
112
113
114
# File 'lib/fog/collection.rb', line 105

def load(objects)
  if @loaded
    clear
  end
  @loaded = true
  for object in objects
    self << new(object)
  end
  self
end

#merge_attributes(new_attributes = {}) ⇒ Object



120
121
122
123
124
125
126
127
128
129
# File 'lib/fog/collection.rb', line 120

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

#modelObject



116
117
118
# File 'lib/fog/collection.rb', line 116

def model
  self.class.instance_variable_get('@model')
end

#new(attributes = {}) ⇒ Object



131
132
133
134
135
136
137
138
# File 'lib/fog/collection.rb', line 131

def new(attributes = {})
  model.new(
    attributes.merge!(
      :collection => self,
      :connection => connection
    )
  )
end

#reloadObject



140
141
142
# File 'lib/fog/collection.rb', line 140

def reload
  self.clear.concat(all)
end

#table(attributes = nil) ⇒ Object



144
145
146
# File 'lib/fog/collection.rb', line 144

def table(attributes = nil)
  Formatador.display_table(self.map {|instance| instance.attributes}, attributes)
end

#to_jsonObject



148
149
150
# File 'lib/fog/collection.rb', line 148

def to_json
  self.map {|member| member}.to_json
end