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/utils/collection.rb, line 16
def initialize(proc)
  @proc = proc
end

Public Instance Methods

each() { |item| ... } 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/utils/collection.rb, line 24
def each
  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
empty?() click to toggle source
# File lib/metal_archives/utils/collection.rb, line 38
def empty?
  first.nil?
end