Class: Asana::Resources::Collection
- Inherits:
-
Object
- Object
- Asana::Resources::Collection
- Includes:
- ResponseHelper, Enumerable
- Defined in:
- lib/asana/resource_includes/collection.rb
Overview
Represents a paginated collection of Asana resources.
Instance Attribute Summary collapse
- #elements ⇒ Object readonly
Instance Method Summary collapse
-
#each(&block) ⇒ Object
Iterates over the elements of the collection.
-
#initialize(elements, extra, type: Resource, client: required('client')) ⇒ Collection
constructor
Initializes a collection representing a page of resources of a given type.
-
#next_page ⇒ Object
Returns a new Asana::Resources::Collection with the next page or nil if there are no more pages.
-
#size ⇒ Object
(also: #length)
Returns the size of the collection.
-
#to_s ⇒ Object
(also: #inspect)
Returns a String representation of the collection.
Methods included from ResponseHelper
Constructor Details
#initialize(elements, extra, type: Resource, client: required('client')) ⇒ Collection
Initializes a collection representing a page of resources of a given type.
(elements, extra) - [Array] an (String, Hash) tuple coming from the
response parser.
20 21 22 23 24 25 26 27 |
# File 'lib/asana/resource_includes/collection.rb', line 20 def initialize((elements, extra), type: Resource, client: required('client')) @elements = elements.map { |elem| type.new(elem, client: client) } @type = type @next_page_data = extra['next_page'] @client = client end |
Instance Attribute Details
#elements ⇒ Object (readonly)
10 11 12 |
# File 'lib/asana/resource_includes/collection.rb', line 10 def elements @elements end |
Instance Method Details
#each(&block) ⇒ Object
Iterates over the elements of the collection.
30 31 32 33 34 35 36 37 |
# File 'lib/asana/resource_includes/collection.rb', line 30 def each(&block) if block @elements.each(&block) (next_page || []).each(&block) else to_enum end end |
#next_page ⇒ Object
Returns a new Asana::Resources::Collection with the next page or nil if there are no more pages. Caches the result.
55 56 57 58 59 60 61 62 63 64 |
# File 'lib/asana/resource_includes/collection.rb', line 55 def next_page if defined?(@next_page) @next_page else @next_page = if @next_page_data response = parse(@client.get(@next_page_data['path'])) self.class.new(response, type: @type, client: @client) end end end |
#size ⇒ Object Also known as: length
Returns the size of the collection.
40 41 42 |
# File 'lib/asana/resource_includes/collection.rb', line 40 def size to_a.size end |
#to_s ⇒ Object Also known as: inspect
Returns a String representation of the collection.
46 47 48 49 50 |
# File 'lib/asana/resource_includes/collection.rb', line 46 def to_s "#<Asana::Collection<#{@type}> " \ "[#{@elements.map(&:inspect).join(', ')}" + (@next_page_data ? ', ...' : '') + ']>' end |