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.
-
#last ⇒ Object
Returns the last item in the collection.
-
#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.
22 23 24 25 26 27 28 29 |
# File 'lib/asana/resource_includes/collection.rb', line 22 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)
12 13 14 |
# File 'lib/asana/resource_includes/collection.rb', line 12 def elements @elements end |
Instance Method Details
#each(&block) ⇒ Object
Iterates over the elements of the collection.
32 33 34 35 36 37 38 39 |
# File 'lib/asana/resource_includes/collection.rb', line 32 def each(&block) if block @elements.each(&block) (next_page || []).each(&block) else to_enum end end |
#last ⇒ Object
Returns the last item in the collection.
42 43 44 |
# File 'lib/asana/resource_includes/collection.rb', line 42 def last @elements.last 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.
60 61 62 63 64 65 66 67 68 69 |
# File 'lib/asana/resource_includes/collection.rb', line 60 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.
47 48 49 |
# File 'lib/asana/resource_includes/collection.rb', line 47 def size to_a.size end |
#to_s ⇒ Object Also known as: inspect
Returns a String representation of the collection.
53 54 55 |
# File 'lib/asana/resource_includes/collection.rb', line 53 def to_s "#<Asana::Collection<#{@type}> [#{@elements.map(&:inspect).join(', ')}#{@next_page_data ? ', ...' : ''}]>" end |