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

Overview

Fog::Collection

Direct Known Subclasses

Association, PagedCollection

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Attributes::ClassMethods

_load, aliases, associations, attribute, attributes, default_values, has_many, has_many_identities, has_one, has_one_identity, identity, ignore_attributes, ignored_attributes, masks

Methods included from Fog::Core::DeprecatedConnectionAccessors

#connection, #connection=, #prepare_service_value

Methods included from Attributes::InstanceMethods

#_dump, #all_associations, #all_associations_and_attributes, #all_attributes, #associations, #attributes, #dup, #identity, #identity=, #identity_name, #masks, #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):



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

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

Instance Attribute Details

#serviceObject (readonly)

Returns the value of attribute service.



11
12
13
# File 'lib/fog/core/collection.rb', line 11

def service
  @service
end

Class Method Details

.model(new_model = nil) ⇒ Object



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

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

Instance Method Details

#clearObject



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

def clear
  @loaded = 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
# File 'lib/fog/core/collection.rb', line 55

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

#inspectObject



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

def inspect
  Fog::Formatador.format(self)
end

#load(objects) ⇒ Object



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

def load(objects)
  clear && objects.each { |object| self << new(object) }
  self
end

#modelObject



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

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

#new(attributes = {}) ⇒ Object



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

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

#reloadObject



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

def reload
  clear && lazy_load
  self
end

#table(attributes = nil) ⇒ Object



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

def table(attributes = nil)
  Fog::Formatador.display_table(map(&:attributes), attributes)
end

#to_json(_options = {}) ⇒ Object



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

def to_json(_options = {})
  Fog::JSON.encode(map(&:attributes))
end