class MetalArchives::Collection

Enumerable collection over a paginated resource

Public Class Methods

new(proc) click to toggle source

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.

# File lib/metal_archives/collection.rb, line 17
def initialize(proc)
  @proc = proc
end

Public Instance Methods

each(&block) click to toggle source

Calls the given block once for each element, passing that element as a parameter. If no block is given, an Enumerator is returned.

# File lib/metal_archives/collection.rb, line 25
def each(&block)
  return to_enum :each unless block

  loop do
    items = instance_exec(&@proc)

    items.each(&block)

    break if items.empty?
  end
end
empty?() click to toggle source
# File lib/metal_archives/collection.rb, line 37
def empty?
  first.nil?
end