Class: Braintree::ResourceCollection
- Inherits:
-
Object
- Object
- Braintree::ResourceCollection
- Includes:
- Enumerable
- Defined in:
- lib/braintree/resource_collection.rb
Instance Attribute Summary collapse
-
#ids ⇒ Object
readonly
Returns the value of attribute ids.
Instance Method Summary collapse
-
#each(&block) ⇒ Object
Yields each item.
- #empty? ⇒ Boolean
-
#first(amount = 1) ⇒ Object
Returns the first or the first N items in the collection or nil if the collection is empty.
-
#initialize(response, &block) ⇒ ResourceCollection
constructor
A new instance of ResourceCollection.
-
#maximum_size ⇒ Object
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.
Constructor Details
#initialize(response, &block) ⇒ ResourceCollection
Returns a new instance of ResourceCollection.
7 8 9 10 11 |
# File 'lib/braintree/resource_collection.rb', line 7 def initialize(response, &block) @ids = Util.extract_attribute_as_array(response[:search_results], :ids) @page_size = response[:search_results][:page_size] @paging_block = block end |
Instance Attribute Details
#ids ⇒ Object (readonly)
Returns the value of attribute ids.
5 6 7 |
# File 'lib/braintree/resource_collection.rb', line 5 def ids @ids end |
Instance Method Details
#each(&block) ⇒ Object
Yields each item
14 15 16 17 18 19 |
# File 'lib/braintree/resource_collection.rb', line 14 def each(&block) @ids.each_slice(@page_size) do |page_of_ids| resources = @paging_block.call(page_of_ids) resources.each(&block) end end |
#empty? ⇒ Boolean
21 22 23 |
# File 'lib/braintree/resource_collection.rb', line 21 def empty? @ids.empty? end |
#first(amount = 1) ⇒ Object
Returns the first or the first N items in the collection or nil if the collection is empty
26 27 28 29 30 31 32 33 |
# File 'lib/braintree/resource_collection.rb', line 26 def first(amount = 1) return nil if @ids.empty? return @paging_block.call([@ids.first]).first if amount == 1 @ids.first(amount).each_slice(@page_size).flat_map do |page_of_ids| @paging_block.call(page_of_ids) end end |
#maximum_size ⇒ Object
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.
38 39 40 |
# File 'lib/braintree/resource_collection.rb', line 38 def maximum_size @ids.size end |