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.



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

def initialize(attributes = {})
  merge_attributes(attributes)
  @loaded = false
end

Class Method Details

._load(marshalled) ⇒ Object



22
23
24
# File 'lib/fog/collection.rb', line 22

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

.aliasesObject



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

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

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



26
27
28
29
30
31
32
33
34
35
# File 'lib/fog/collection.rb', line 26

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



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

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

.model(new_model) ⇒ Object



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

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

Instance Method Details

#_dumpObject



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

def _dump
  Marshal.dump(attributes)
end

#attributesObject



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

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

#connectionObject



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

def connection
  @connection
end

#connection=(new_connection) ⇒ Object



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

def connection=(new_connection)
  @connection = new_connection
end

#create(attributes = {}) ⇒ Object



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

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

#inspectObject



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

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



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

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

#merge_attributes(new_attributes = {}) ⇒ Object



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

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



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

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

#new(attributes = {}) ⇒ Object



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

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

#reloadObject



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

def reload
  self.clear.concat(all)
end

#table(attributes = nil) ⇒ Object



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

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