class Asana::Resources::Collection

Public: Represents a paginated collection of Asana resources.

Attributes

elements[R]

Public Class Methods

new((elements, extra), type: Resource, client: required('client')) click to toggle source

Public: Initializes a collection representing a page of resources of a given type.

(elements, extra) - [Array] an (String, Hash) tuple coming from the

response parser.

type - [Class] the type of resource that the collection

contains. Defaults to the generic Resource.

client - [Asana::Client] the client to perform requests.

# 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

Public Instance Methods

each(&block) click to toggle source

Public: Iterates over the elements of the collection.

# 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
inspect()
Alias for: to_s
length()
Alias for: size
next_page() click to toggle source

Public: Returns a new Asana::Resources::Collection with the next page or nil if there are no more pages. Caches the result.

# 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() click to toggle source

Public: Returns the size of the collection.

# File lib/asana/resource_includes/collection.rb, line 40
def size
  to_a.size
end
Also aliased as: length
to_s() click to toggle source

Public: Returns a String representation of the collection.

# File lib/asana/resource_includes/collection.rb, line 46
def to_s
  "#<Asana::Collection<#{@type}> "            "[#{@elements.map(&:inspect).join(', ')}" +
    (@next_page_data ? ', ...' : '') + ']>'
end
Also aliased as: inspect