Class: Fog::Collection

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Attributes::ClassMethods

_load, aliases, attribute, attributes, identity, ignore_attributes, ignored_attributes

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.



54
55
56
57
# File 'lib/fog/core/collection.rb', line 54

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

Instance Attribute Details

#connectionObject

Returns the value of attribute connection.



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

def connection
  @connection
end

Class Method Details

.model(new_model = nil) ⇒ Object



32
33
34
35
36
37
38
# File 'lib/fog/core/collection.rb', line 32

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

Instance Method Details

#clearObject



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

def clear
  @loaded = true
  super
end

#create(attributes = {}) ⇒ Object



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

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

#inspectObject



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/fog/core/collection.rb', line 60

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



84
85
86
87
88
89
90
# File 'lib/fog/core/collection.rb', line 84

def load(objects)
  clear
  for object in objects
    self << new(object)
  end
  self
end

#modelObject



92
93
94
# File 'lib/fog/core/collection.rb', line 92

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

#new(attributes = {}) ⇒ Object



96
97
98
99
100
101
102
103
# File 'lib/fog/core/collection.rb', line 96

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

#reloadObject



105
106
107
108
109
# File 'lib/fog/core/collection.rb', line 105

def reload
  clear
  lazy_load
  self
end

#table(attributes = nil) ⇒ Object



111
112
113
# File 'lib/fog/core/collection.rb', line 111

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

#to_jsonObject



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

def to_json
  require 'json'
  self.map {|member| member.attributes}.to_json
end