Class: MetalArchives::Collection

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/metal_archives/collection.rb

Overview

Enumerable collection over a paginated resource

Instance Method Summary collapse

Constructor Details

#initialize(proc) ⇒ Collection

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.



17
18
19
# File 'lib/metal_archives/collection.rb', line 17

def initialize(proc)
  @proc = proc
end

Instance Method Details

#each(&block) ⇒ Object

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



25
26
27
28
29
30
31
32
33
34
35
# 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?Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/metal_archives/collection.rb', line 37

def empty?
  first.nil?
end