Sha256: a56cf266526b457b1204695161bf11ea68a438371f551088d8f83309cad2be98

Contents?: true

Size: 821 Bytes

Versions: 6

Compression:

Stored size: 821 Bytes

Contents

module MetalArchives
  ##
  # Enumerable collection over a paginated resource
  #
  class Collection
    include Enumerable

    ##
    # Construct a new Collection
    #
    # [+proc+]
    #   +Proc+ or +lambda+, called repeatedly when iterating. Should return an array of results (stateful),
    #   should return an empty array when there are no results left.
    #
    def initialize(proc)
      @proc = proc
    end

    ##
    # Calls the given block once for each element, passing that element as a parameter.
    # If no block is given, an Enumerator is returned.
    #
    def each(&block)
      return to_enum :each unless block_given?

      loop do
        items = instance_exec(&@proc)

        items.each do |item|
          yield item
        end

        break if items.empty?
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
metal_archives-0.8.0 lib/metal_archives/utils/collection.rb
metal_archives-0.7.1 lib/metal_archives/utils/collection.rb
metal_archives-0.6.0 lib/metal_archives/utils/collection.rb
metal_archives-0.5.0 lib/metal_archives/utils/collection.rb
metal_archives-0.4.0 lib/metal_archives/utils/collection.rb
metal_archives-0.3.0 lib/metal_archives/utils/collection.rb