Class: SimpleStack::Collection
- Inherits:
-
Object
- Object
- SimpleStack::Collection
- Includes:
- Cacheable
- Defined in:
- lib/simple_stack/collection.rb
Instance Attribute Summary collapse
-
#clazz ⇒ Object
Returns the value of attribute clazz.
-
#hypervisor ⇒ Object
Returns the value of attribute hypervisor.
-
#parent ⇒ Object
Returns the value of attribute parent.
-
#url ⇒ Object
Returns the value of attribute url.
Instance Method Summary collapse
- #connection ⇒ Object
- #create(options = {}) ⇒ Object
- #find(id) ⇒ Object
-
#initialize(hypervisor, parent, url, clazz) ⇒ Collection
constructor
A new instance of Collection.
- #inspect ⇒ Object
- #method_missing(method, *args, &block) ⇒ Object
- #to_a ⇒ Object
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
#clazz ⇒ Object
Returns the value of attribute clazz.
5 6 7 |
# File 'lib/simple_stack/collection.rb', line 5 def clazz @clazz end |
#hypervisor ⇒ Object
Returns the value of attribute hypervisor.
5 6 7 |
# File 'lib/simple_stack/collection.rb', line 5 def hypervisor @hypervisor end |
#parent ⇒ Object
Returns the value of attribute parent.
5 6 7 |
# File 'lib/simple_stack/collection.rb', line 5 def parent @parent end |
#url ⇒ Object
Returns the value of attribute url.
5 6 7 |
# File 'lib/simple_stack/collection.rb', line 5 def url @url end |
Instance Method Details
#connection ⇒ Object
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(={}) response = hypervisor.post(url, ) 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 |
#inspect ⇒ Object
37 38 39 |
# File 'lib/simple_stack/collection.rb', line 37 def inspect to_a.inspect end |
#to_a ⇒ Object
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 |