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, #filter_attributes, #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):



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

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



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

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

Instance Method Details

#clearObject



47
48
49
# File 'lib/fog/core/collection.rb', line 47

def clear
  @loaded = super
end

#create(attributes = {}) ⇒ Object



51
52
53
54
55
# File 'lib/fog/core/collection.rb', line 51

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

#destroy(identity) ⇒ Object



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

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

#inspectObject



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

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

#load(objects) ⇒ Object



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

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

#modelObject



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

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

#new(attributes = {}) ⇒ Object



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

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



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

def reload
  clear && lazy_load
  self
end

#table(attributes = nil) ⇒ Object



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

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

#to_json(_options = {}) ⇒ Object



109
110
111
# File 'lib/fog/core/collection.rb', line 109

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