Class: Cubits::ResourceCollection
- Inherits:
-
Object
- Object
- Cubits::ResourceCollection
- Includes:
- Enumerable
- Defined in:
- lib/cubits/resource_collection.rb
Instance Attribute Summary collapse
-
#path ⇒ Object
readonly
Returns the value of attribute path.
-
#resource ⇒ Object
readonly
Returns the value of attribute resource.
Instance Method Summary collapse
-
#all ⇒ Array<Resource>
Loads collection of resources.
-
#collection_name ⇒ Object
By convention collection name for the resource is the last part of the path.
-
#count ⇒ Object
(also: #size)
Returns number of elements in the collection.
-
#each(&_block) ⇒ Object
Iterates through the collection, yielding the giving block for each resource.
-
#exposed_method?(method_name) ⇒ Boolean
True if the method is exposed by this resource.
-
#find(id) ⇒ Object
Loads resource.
-
#first ⇒ Object
Returns first element of the collection.
-
#initialize(params = {}) ⇒ ResourceCollection
constructor
A new instance of ResourceCollection.
-
#last ⇒ Object
Returns last element of the collection.
- #name ⇒ Object
-
#path_to(resource_or_id = nil) ⇒ Object
Returns API path to resource.
-
#reload ⇒ self
Reloads collection.
- #to_s ⇒ Object
Constructor Details
#initialize(params = {}) ⇒ ResourceCollection
Returns a new instance of ResourceCollection.
7 8 9 10 11 |
# File 'lib/cubits/resource_collection.rb', line 7 def initialize(params = {}) @path = params[:path] @resource = params[:resource] @exposed_methods = params[:expose_methods] end |
Instance Attribute Details
#path ⇒ Object (readonly)
Returns the value of attribute path.
5 6 7 |
# File 'lib/cubits/resource_collection.rb', line 5 def path @path end |
#resource ⇒ Object (readonly)
Returns the value of attribute resource.
5 6 7 |
# File 'lib/cubits/resource_collection.rb', line 5 def resource @resource end |
Instance Method Details
#all ⇒ Array<Resource>
Loads collection of resources
36 37 38 39 40 41 42 43 44 45 |
# File 'lib/cubits/resource_collection.rb', line 36 def all fail NoMethodError, "Resource #{name} does not expose .all" unless exposed_method?(:all) list = [] page_count.times do |i| list += page(i + 1) end list rescue NotFound [] end |
#collection_name ⇒ Object
By convention collection name for the resource is the last part of the path
15 16 17 |
# File 'lib/cubits/resource_collection.rb', line 15 def collection_name path.split('/').last end |
#count ⇒ Object Also known as: size
Returns number of elements in the collection
27 28 29 |
# File 'lib/cubits/resource_collection.rb', line 27 def count pagination.total_count end |
#each(&_block) ⇒ Object
Iterates through the collection, yielding the giving block for each resource.
84 85 86 87 88 89 90 91 |
# File 'lib/cubits/resource_collection.rb', line 84 def each(&_block) return to_enum unless block_given? page_count.times do |i| page(i + 1).each do |r| yield r end end end |
#exposed_method?(method_name) ⇒ Boolean
Returns true if the method is exposed by this resource.
21 22 23 |
# File 'lib/cubits/resource_collection.rb', line 21 def exposed_method?(method_name) (@exposed_methods || []).include?(method_name) end |
#find(id) ⇒ Object
Loads resource
65 66 67 68 69 70 |
# File 'lib/cubits/resource_collection.rb', line 65 def find(id) fail NoMethodError, "Resource #{name} does not expose .find" unless exposed_method?(:find) resource.new Cubits.connection.get(path_to(id)) rescue NotFound nil end |
#first ⇒ Object
Returns first element of the collection
49 50 51 |
# File 'lib/cubits/resource_collection.rb', line 49 def first first_page.first end |
#last ⇒ Object
Returns last element of the collection
55 56 57 |
# File 'lib/cubits/resource_collection.rb', line 55 def last last_page.last end |
#name ⇒ Object
106 107 108 |
# File 'lib/cubits/resource_collection.rb', line 106 def name "Collection of #{resource.name}" end |
#path_to(resource_or_id = nil) ⇒ Object
Returns API path to resource
95 96 97 98 99 100 101 102 103 104 |
# File 'lib/cubits/resource_collection.rb', line 95 def path_to(resource_or_id = nil) fail ArgumentError, "Resource path is not set for #{self}" unless path if resource_or_id.is_a?(Resource) "#{path}/#{resource_or_id.id}" elsif resource_or_id "#{path}/#{resource_or_id}" else path end end |
#reload ⇒ self
Reloads collection
76 77 78 79 80 |
# File 'lib/cubits/resource_collection.rb', line 76 def reload @pagination = nil @pages = nil self end |
#to_s ⇒ Object
110 111 112 |
# File 'lib/cubits/resource_collection.rb', line 110 def to_s "<#{self.class.name} of #{resource.name}, #{path}>" end |