class ArcFurnace::ExcelSource

Attributes

group_by[R]
group_by?[R]
key_column[R]
value[R]

Public Class Methods

new(excel: nil, filename: nil, sheet: nil, group_by: false, key_column: nil) click to toggle source
Calls superclass method ArcFurnace::EnumeratorSource::new
# File lib/arc-furnace/excel_source.rb, line 10
def initialize(excel: nil, filename: nil, sheet: nil, group_by: false, key_column: nil)
  @excel = excel ? excel : Roo::Excelx.new(filename)
  @preprocessed_excel = []
  @group_by = group_by
  @key_column = key_column
  @excel.default_sheet = sheet if sheet
  super()
end

Public Instance Methods

build_enumerator() click to toggle source
# File lib/arc-furnace/excel_source.rb, line 58
def build_enumerator
  Enumerator.new do |yielder|
    if group_by?
      @preprocessed_excel.each.with_index do |(_, array), index|
        next if index == 0 # skip header row
        yielder << array
      end
    else
      excel.each_row_streaming do |row|
        yielder << if header_row
          build_row(row)
        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
end
build_headers() click to toggle source
# File lib/arc-furnace/excel_source.rb, line 34
def build_headers
  @header_row = excel.first
end
build_row(row) click to toggle source
# File lib/arc-furnace/excel_source.rb, line 51
def build_row(row)
  row.each_with_object({}) do |cell, result|
    value = extract_cell_value(cell)
    result[header_row[cell.coordinate.column - 1]] = value if value
  end
end
close() click to toggle source
# File lib/arc-furnace/excel_source.rb, line 78
def close
  @excel.close if @excel
end
extract_cell_value(cell) click to toggle source
# File lib/arc-furnace/excel_source.rb, line 43
def extract_cell_value(cell)
  if cell
    return cell.value.strftime('%m-%d-%y') if cell.type == :date
    coerced_value = cell.type == :string ? cell.value : cell.cell_value.try(:to_s).try(:strip)
    coerced_value unless coerced_value.blank?
  end
end
group_rows() click to toggle source
# File lib/arc-furnace/excel_source.rb, line 38
def group_rows
  @excel.each_row_streaming { |row| @preprocessed_excel << build_row(row) }
  @preprocessed_excel = @preprocessed_excel.group_by { |row| row[key_column] }
end
preprocess() click to toggle source

note that group_by requires the entire file to be read into memory

# File lib/arc-furnace/excel_source.rb, line 25
def preprocess
  if group_by?
    build_headers
    group_rows
  else
    enumerator.next
  end
end