Class: SimpleStack::Collection

Inherits:
Object
  • Object
show all
Includes:
Cacheable
Defined in:
lib/simple_stack/collection.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Cacheable

#cacheable?, #cached_attributes, #reload

Constructor Details

#initialize(hypervisor, parent, url, clazz) ⇒ Collection

Returns a new instance of Collection.



7
8
9
10
11
12
# File 'lib/simple_stack/collection.rb', line 7

def initialize(hypervisor, parent, url, clazz)
  self.hypervisor = hypervisor
  self.parent = parent
  self.url = url.to_s
  self.clazz = clazz
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object



33
34
35
# File 'lib/simple_stack/collection.rb', line 33

def method_missing(method, *args, &block)
  to_a.send(method, *args, &block)
end

Instance Attribute Details

#clazzObject

Returns the value of attribute clazz.



5
6
7
# File 'lib/simple_stack/collection.rb', line 5

def clazz
  @clazz
end

#hypervisorObject

Returns the value of attribute hypervisor.



5
6
7
# File 'lib/simple_stack/collection.rb', line 5

def hypervisor
  @hypervisor
end

#parentObject

Returns the value of attribute parent.



5
6
7
# File 'lib/simple_stack/collection.rb', line 5

def parent
  @parent
end

#urlObject

Returns the value of attribute url.



5
6
7
# File 'lib/simple_stack/collection.rb', line 5

def url
  @url
end

Instance Method Details

#connectionObject



41
42
43
# File 'lib/simple_stack/collection.rb', line 41

def connection
  hypervisor.connection
end

#create(options = {}) ⇒ Object



24
25
26
27
28
29
30
31
# File 'lib/simple_stack/collection.rb', line 24

def create(options={})
  response = hypervisor.post(url, options)
  entity_path = response.headers["location"].sub(/^\//, "").sub(/\/$/, "")
  entity_url = "#{connection.url}/#{entity_path}"
  new_item = clazz.new hypervisor, self, entity_url
  reload if cacheable?
  new_item
end

#find(id) ⇒ Object



20
21
22
# File 'lib/simple_stack/collection.rb', line 20

def find(id)
  clazz.new hypervisor, self, "#{url}/#{id}"
end

#inspectObject



37
38
39
# File 'lib/simple_stack/collection.rb', line 37

def inspect
  to_a.inspect
end

#to_aObject



14
15
16
17
18
# File 'lib/simple_stack/collection.rb', line 14

def to_a
  cached_attributes[:items] ||= hypervisor.get(url).map do |item|
    clazz.new hypervisor, self, "#{url}/#{item["id"]}"
  end
end