Class: Fog::Collection

Inherits:
Array
  • Object
show all
Defined in:
lib/fog/collection.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ Collection

Returns a new instance of Collection.



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

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

Class Method Details

._load(marhsalled) ⇒ Object



13
14
15
# File 'lib/fog/collection.rb', line 13

def self._load(marhsalled)
  new(Marshal.load(marshalled))
end

.aliasesObject



32
33
34
# File 'lib/fog/collection.rb', line 32

def self.aliases
  @aliases ||= {}
end

.attribute(name, other_names = []) ⇒ Object



17
18
19
20
21
22
23
24
25
26
# File 'lib/fog/collection.rb', line 17

def self.attribute(name, other_names = [])
  class_eval <<-EOS, __FILE__, __LINE__
    attr_accessor :#{name}
  EOS
  @attributes ||= []
  @attributes |= [name]
  for other_name in [*other_names]
    aliases[other_name] = name
  end
end

.attributesObject



36
37
38
# File 'lib/fog/collection.rb', line 36

def self.attributes
  @attributes ||= []
end

.model(new_model) ⇒ Object



28
29
30
# File 'lib/fog/collection.rb', line 28

def self.model(new_model)
  @model = new_model
end

Instance Method Details

#_dumpObject



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

def _dump
  Marshal.dump(attributes)
end

#attributesObject



44
45
46
47
48
49
50
# File 'lib/fog/collection.rb', line 44

def attributes
  attributes = {}
  for attribute in self.class.attributes
    attributes[attribute] = send("#{attribute}")
  end
  attributes
end

#connectionObject



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

def connection
  @connection
end

#connection=(new_connection) ⇒ Object



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

def connection=(new_connection)
  @connection = new_connection
end

#create(attributes = {}) ⇒ Object



60
61
62
63
64
# File 'lib/fog/collection.rb', line 60

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

#inspectObject



71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/fog/collection.rb', line 71

def inspect
  data = "#<#{self.class.name}"
  for attribute in self.class.attributes
    data << " #{attribute}=#{send(attribute).inspect}"
  end
  data << " ["
  for member in self
    data << "#{member.inspect},"
  end
  data.chop! unless self.empty?
  data << "]>"
end

#merge_attributes(new_attributes = {}) ⇒ Object



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

def merge_attributes(new_attributes = {})
  for key, value in new_attributes
    if aliased_key = self.class.aliases[key]
      send("#{aliased_key}=", value)
    else
      send("#{key}=", value)
    end
  end
  self
end

#modelObject



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

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

#new(attributes = {}) ⇒ Object



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

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

#reloadObject



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

def reload
  self.clear.concat(all)
end