class ArcFurnace::MultiExcelSource

Attributes

excel[R]
sheets_info_array[R]
value[R]

Public Class Methods

new(sheets_info_array: []) click to toggle source

Sheets is in the format of: [

{ filename: 'foo.xlsx', sheet: 'sheet name' },
{ filename: 'foo2.xlsx', sheet: 'sheet name' }

]

The value for the :sheet key points to the sheet that we want to parse. If sheets are not explicitly indicated, they will not be parsed.

# File lib/arc-furnace/multi_excel_source.rb, line 19
def initialize(sheets_info_array: [])
  @sheets_info_array = sheets_info_array.reverse
  open_next_file
end

Private Instance Methods

advance() click to toggle source
# File lib/arc-furnace/multi_excel_source.rb, line 30
def advance
  advance_in_current_file || open_next_file
end
advance_in_current_file() click to toggle source
# File lib/arc-furnace/multi_excel_source.rb, line 34
def advance_in_current_file
  @value =
    begin
      enumerator.next if enumerator
    rescue StopIteration
      @enumerator = nil
      nil
    end
  value
end
build_enumerator() click to toggle source
# File lib/arc-furnace/multi_excel_source.rb, line 68
def build_enumerator
  Enumerator.new do |yielder|
    excel.each_row_streaming do |row|
      yielder <<
        if header_row
          row.each_with_object({}) do |cell, result|
            value = extract_cell_value(cell)
            result[header_row[cell.coordinate.column - 1]] = value if value
          end
        else
          # First time, return the header row so we can save it.
          @header_row = row.map { |value| extract_cell_value(value) }
        end
    end
  end
end
extract_cell_value(cell) click to toggle source
# File lib/arc-furnace/multi_excel_source.rb, line 61
def extract_cell_value(cell)
  if cell
    coerced_value = cell.type == :string ? cell.value : cell.cell_value.try(:to_s).try(:strip)
    coerced_value unless coerced_value.blank?
  end
end
open_next_file() click to toggle source
# File lib/arc-furnace/multi_excel_source.rb, line 45
def open_next_file
  excel.close if excel
  @excel = nil
  @header_row = nil
  if sheets_info_array.empty?
    nil
  else
    sheets_info = sheets_info_array.pop
    @excel = Roo::Excelx.new(sheets_info[:filename])
    @excel.default_sheet = sheets_info[:sheet]
    @enumerator = build_enumerator
    preprocess
    advance
  end
end
preprocess() click to toggle source
# File lib/arc-furnace/multi_excel_source.rb, line 26
def preprocess
  enumerator.next
end