Class: TableStructure::Writer

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

Defined Under Namespace

Classes: Output

Instance Method Summary collapse

Constructor Details

#initialize(schema, header: { context: nil, step: nil }, method: :<<, row_type: :array) ⇒ Writer

Returns a new instance of Writer.



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

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

Instance Method Details

#write(items, to:, method: @options[:method], &block) ⇒ Object



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

def write(
  items,
  to:,
  method: @options[:method],
  &block
)
  output = Output.new(to, method: method)

  Iterator
    .new(
      @schema,
      header: @options[:header],
      row_type: @options[:row_type]
    )
    .iterate(items, &block)
    .each { |row| output.write(row) }

  nil
end