Class: Csvtool::Infrastructure::Output::CsvRowConsoleWriter

Inherits:
Object
  • Object
show all
Defined in:
lib/csvtool/infrastructure/output/csv_row_console_writer.rb

Instance Method Summary collapse

Constructor Details

#initialize(stdout:, row_streamer:) ⇒ CsvRowConsoleWriter

Returns a new instance of CsvRowConsoleWriter.



9
10
11
12
# File 'lib/csvtool/infrastructure/output/csv_row_console_writer.rb', line 9

def initialize(stdout:, row_streamer:)
  @stdout = stdout
  @row_streamer = row_streamer
end

Instance Method Details

#call(file_path:, col_sep:, headers:, start_row:, end_row:) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/csvtool/infrastructure/output/csv_row_console_writer.rb', line 14

def call(file_path:, col_sep:, headers:, start_row:, end_row:)
  wrote_header = false
  stats = @row_streamer.each_in_range(
    file_path: file_path,
    col_sep: col_sep,
    start_row: start_row,
    end_row: end_row
  ) do |fields|
    unless wrote_header
      @stdout.puts ::CSV.generate_line(headers, row_sep: "").chomp
      wrote_header = true
    end
    @stdout.puts ::CSV.generate_line(fields, row_sep: "").chomp
  end

  stats
end