Class: Fog::Collection

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

Direct Known Subclasses

PagedCollection

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 Fog::Core::DeprecatedConnectionAccessors

#connection, #connection=, #prepare_service_value

Methods included from Attributes::InstanceMethods

#_dump, #attributes, #dup, #identity, #identity=, #merge_attributes, #new_record?, #persisted?, #requires, #requires_one

Constructor Details

#initialize(attributes = {}) ⇒ Fog::Collection

Creates a new Fog::Collection based around the passed service

Parameters:

  • attributes (Hash) (defaults to: {})

Options Hash (attributes):



67
68
69
70
71
# File 'lib/fog/core/collection.rb', line 67

def initialize(attributes = {})
  @service = attributes.delete(:service)
  @loaded = false
  merge_attributes(attributes)
end

Instance Attribute Details

#serviceObject (readonly)

Returns the value of attribute service.



9
10
11
# File 'lib/fog/core/collection.rb', line 9

def service
  @service
end

Class Method Details

.model(new_model = nil) ⇒ Object



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

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

Instance Method Details

#clearObject



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

def clear
  @loaded = true
  super
end

#create(attributes = {}) ⇒ Object



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

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

#destroy(identity) ⇒ Object



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

def destroy(identity)
  object = new(:identity => identity)
  object.destroy
end

#inspectObject



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/fog/core/collection.rb', line 74

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



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

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

#modelObject



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

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

#new(attributes = {}) ⇒ Object



110
111
112
113
114
115
116
117
118
119
120
# File 'lib/fog/core/collection.rb', line 110

def new(attributes = {})
  unless attributes.is_a?(::Hash)
    raise(ArgumentError.new("Initialization parameters must be an attributes hash, got #{attributes.class} #{attributes.inspect}"))
  end
  model.new(
    {
      :collection => self,
      :service => service
    }.merge(attributes)
  )
end

#reloadObject



122
123
124
125
126
# File 'lib/fog/core/collection.rb', line 122

def reload
  clear
  lazy_load
  self
end

#table(attributes = nil) ⇒ Object



128
129
130
# File 'lib/fog/core/collection.rb', line 128

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

#to_json(options = {}) ⇒ Object



132
133
134
# File 'lib/fog/core/collection.rb', line 132

def to_json(options = {})
  Fog::JSON.encode(self.map {|member| member.attributes})
end