Class: TableStructure::Iterator

Inherits:
Object
  • Object
show all
Defined in:
lib/table_structure/iterator.rb

Instance Method Summary collapse

Constructor Details

#initialize(schema, header: { context: nil }, row_type: :array) ⇒ Iterator

Returns a new instance of Iterator.



5
6
7
8
9
10
11
12
13
14
15
# File 'lib/table_structure/iterator.rb', line 5

def initialize(
  schema,
  header: { context: nil },
  row_type: :array
)
  @schema = schema
  @options = {
    header: header,
    row_type: row_type
  }
end

Instance Method Details

#iterate(items, &block) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/table_structure/iterator.rb', line 17

def iterate(items, &block)
  unless items.respond_to?(:each)
    raise ::TableStructure::Error, "Must be enumerable. #{items}"
  end

  enum =
    Table::Iterator
    .new(
      Table.new(@schema, row_type: @options[:row_type]),
      header: @options[:header]
    )
    .iterate(items)

  if block_given?
    enum =
      enum
      .lazy
      .map { |row| block.call(row) }
  end

  enum
end