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):



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

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

Instance Attribute Details

#serviceObject (readonly)

Returns the value of attribute service.



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

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
# File 'lib/fog/core/collection.rb', line 44

def clear
  @loaded = 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

#destroy(identity) ⇒ Object



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

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

#inspectObject



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

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

#load(objects) ⇒ Object



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

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

#modelObject



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

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

#new(attributes = {}) ⇒ Object



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

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



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

def reload
  clear && lazy_load
  self
end

#table(attributes = nil) ⇒ Object



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

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

#to_json(_options = {}) ⇒ Object



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

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