Class: TableStructure::Iterator
- Inherits:
-
Object
- Object
- TableStructure::Iterator
- Defined in:
- lib/table_structure/iterator.rb
Defined Under Namespace
Classes: HeaderOptions
Instance Method Summary collapse
-
#initialize(schema, header: { context: nil, step: nil }, row_type: :array) ⇒ Iterator
constructor
A new instance of Iterator.
- #iterate(items, &block) ⇒ Object
Constructor Details
#initialize(schema, header: { context: nil, step: nil }, row_type: :array) ⇒ Iterator
Returns a new instance of Iterator.
18 19 20 21 22 23 24 25 |
# File 'lib/table_structure/iterator.rb', line 18 def initialize( schema, header: { context: nil, step: nil }, row_type: :array ) @table = Table.new(schema, row_type: row_type) @header_options = HeaderOptions.new(header) end |
Instance Method Details
#iterate(items, &block) ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/table_structure/iterator.rb', line 27 def iterate(items, &block) unless items.respond_to?(:each) raise ::TableStructure::Error, "Must be enumerable. #{items}" end table_enum = ::Enumerator.new do |y| body_enum = @table.body(items) if @header_options.enabled? header_row = @table.header(context: @header_options.context) y << header_row if @header_options.step&.positive? loop do @header_options.step.times { y << body_enum.next } y << header_row end else body_enum.each { |row| y << row } end else body_enum.each { |row| y << row } end end table_enum = table_enum.lazy.map(&block) if block_given? table_enum end |