Class: ODSExtractor::RowOutput

Inherits:
Object
  • Object
show all
Defined in:
lib/ods_extractor/row_output.rb

Instance Method Summary collapse

Constructor Details

#initialize(use_header_row:, &row_handler_block) ⇒ RowOutput

Returns a new instance of RowOutput.



2
3
4
5
6
# File 'lib/ods_extractor/row_output.rb', line 2

def initialize(use_header_row:, &row_handler_block)
  @header_cells = []
  @row_handler = row_handler_block
  @use_header_row = use_header_row
end

Instance Method Details

#build_row_hash(row_values) ⇒ Object



28
29
30
31
32
# File 'lib/ods_extractor/row_output.rb', line 28

def build_row_hash(row_values)
  padded_row = row_values.take(@header_cells.length)
  (@header_cells.length - padded_row.length).times { padded_row << nil }
  @header_cells.zip(padded_row).to_h
end

#end_sheetObject



25
26
# File 'lib/ods_extractor/row_output.rb', line 25

def end_sheet
end

#start_sheet(sheet_name) ⇒ Object



8
9
10
11
# File 'lib/ods_extractor/row_output.rb', line 8

def start_sheet(sheet_name)
  @sheet_name = sheet_name
  @header_cells = nil
end

#write_row(row_values) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
# File 'lib/ods_extractor/row_output.rb', line 13

def write_row(row_values)
  if @use_header_row
    if @header_cells
      @row_handler.call(sheet_name: @sheet_name, row: build_row_hash(row_values))
    else
      @header_cells = row_values.map(&:to_s)
    end
  else
    @row_handler.call(sheet_name: @sheet_name, row: row_values)
  end
end