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
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/fog/collection.rb', line 71

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

#merge_attributes(new_attributes = {}) ⇒ Object



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

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



95
96
97
# File 'lib/fog/collection.rb', line 95

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

#new(attributes = {}) ⇒ Object



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

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

#reloadObject



119
120
121
# File 'lib/fog/collection.rb', line 119

def reload
  self.clear.concat(all)
end