Sha256: c7e093c11692685e640c12cea70a64f0435b802f0f1fa0931b4d547479c9abbb

Contents?: true

Size: 1.06 KB

Versions: 72

Compression:

Stored size: 1.06 KB

Contents

module Braintree
  class ResourceCollection # :nodoc:
    include Enumerable

    def initialize(response, &block) # :nodoc:
      @ids = Util.extract_attribute_as_array(response[:search_results], :ids)
      @page_size = response[:search_results][:page_size]
      @paging_block = block
    end

    # Yields each item
    def each(&block)
      @ids.each_slice(@page_size) do |page_of_ids|
        resources = @paging_block.call(page_of_ids)
        resources.each(&block)
      end
    end

    def empty?
      @ids.empty?
    end

    # Returns the first item in the collection or nil if the collection is empty
    def first
      @paging_block.call([@ids.first]).first
    end

    # Only the maximum size of a resource collection can be determined since the data on the server can change while
    # fetching blocks of results for iteration.  For example, customers can be deleted while iterating, so the number
    # of results iterated over may be less than the maximum_size.  In general, this method should be avoided.
    def maximum_size
      @ids.size
    end
  end
end

Version data entries

72 entries across 72 versions & 1 rubygems

Version Path
braintree-2.41.0 lib/braintree/resource_collection.rb
braintree-2.40.0 lib/braintree/resource_collection.rb
braintree-2.39.0 lib/braintree/resource_collection.rb
braintree-2.38.0 lib/braintree/resource_collection.rb
braintree-2.37.0 lib/braintree/resource_collection.rb
braintree-2.36.0 lib/braintree/resource_collection.rb
braintree-2.35.0 lib/braintree/resource_collection.rb
braintree-2.34.1 lib/braintree/resource_collection.rb
braintree-2.34.0 lib/braintree/resource_collection.rb
braintree-2.33.1 lib/braintree/resource_collection.rb
braintree-2.33.0 lib/braintree/resource_collection.rb
braintree-2.32.1 lib/braintree/resource_collection.rb
braintree-2.31.0 lib/braintree/resource_collection.rb
braintree-2.30.2 lib/braintree/resource_collection.rb
braintree-2.30.0 lib/braintree/resource_collection.rb
braintree-2.29.0 lib/braintree/resource_collection.rb
braintree-2.28.0 lib/braintree/resource_collection.rb
braintree-2.27.1 lib/braintree/resource_collection.rb
braintree-2.27.0 lib/braintree/resource_collection.rb
braintree-2.26.0 lib/braintree/resource_collection.rb