Sha256: d791d637ca114cc88140bfca6f91157187da764229ebd546513fcb4c598a2b8d

Contents?: true

Size: 1.25 KB

Versions: 6

Compression:

Stored size: 1.25 KB

Contents

module Braintree
  class PagedCollection
    include BaseModule
    include Enumerable

    attr_reader :current_page_number, :items, :next_page_number, :page_size, :previous_page_number, :total_items

    def initialize(attributes, &block) # :nodoc:
      set_instance_variables_from_hash attributes
      @paging_block = block
    end

    # Returns the item from the current page at the given +index+.
    def [](index)
      @items[index]
    end

    # Yields each item on the current page.
    def each(&block)
      @items.each(&block)
    end

    # Returns the first item from the current page.
    def first
      @items.first
    end

    # Returns true if the page is the last page. False otherwise.
    def last_page?
      current_page_number == total_pages
    end

    # Retrieves the next page of records.
    def next_page
      if last_page?
        return nil
      end
      @paging_block.call(next_page_number)
    end

    # The next page number. Returns +nil+ if on the last page.
    def next_page_number
      last_page? ? nil : current_page_number + 1
    end

    # Returns the total number of pages.
    def total_pages
      total = total_items / page_size
      if total_items % page_size != 0
        total += 1
      end
      total
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
braintree-1.2.1 lib/braintree/paged_collection.rb
braintree-1.2.0 lib/braintree/paged_collection.rb
braintree-1.1.3 lib/braintree/paged_collection.rb
braintree-1.1.2 lib/braintree/paged_collection.rb
braintree-1.1.1 lib/braintree/paged_collection.rb
braintree-1.1.0 lib/braintree/paged_collection.rb