Class: Fog::Collection

Inherits:
Array
  • Object
show all
Extended by:
Attributes::ClassMethods
Includes:
Attributes::InstanceMethods
Defined in:
lib/fog/collection.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Attributes::ClassMethods

_load, aliases, attribute, attributes, identity

Methods included from Attributes::InstanceMethods

#_dump, #attributes, #identity, #identity=, #merge_attributes, #new_record?, #requires

Constructor Details

#initialize(attributes = {}) ⇒ Collection

Returns a new instance of Collection.



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

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

Instance Attribute Details

#connectionObject

Returns the value of attribute connection.



34
35
36
# File 'lib/fog/collection.rb', line 34

def connection
  @connection
end

Class Method Details

.model(new_model = nil) ⇒ Object



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

def self.model(new_model=nil)
  if new_model == nil
    @model
  else
    @model = new_model
  end
end

Instance Method Details

#create(attributes = {}) ⇒ Object



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

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

#inspectObject



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/fog/collection.rb', line 47

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



71
72
73
74
75
76
77
78
79
80
# File 'lib/fog/collection.rb', line 71

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

#modelObject



82
83
84
# File 'lib/fog/collection.rb', line 82

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

#new(attributes = {}) ⇒ Object



86
87
88
89
90
91
92
93
# File 'lib/fog/collection.rb', line 86

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

#reloadObject



95
96
97
# File 'lib/fog/collection.rb', line 95

def reload
  self.clear.concat(all)
end

#table(attributes = nil) ⇒ Object



99
100
101
# File 'lib/fog/collection.rb', line 99

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

#to_jsonObject



103
104
105
# File 'lib/fog/collection.rb', line 103

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