Class: ArcFurnace::ExcelSource

Inherits:
EnumeratorSource show all
Defined in:
lib/arc-furnace/excel_source.rb

Instance Attribute Summary collapse

Attributes inherited from Node

#error_handler, #node_id, #params

Instance Method Summary collapse

Methods inherited from EnumeratorSource

#advance, #empty?

Methods inherited from Source

#advance, #empty?, #finalize, #prepare, #row

Constructor Details

#initialize(excel: nil, filename: nil, sheet: nil, group_by: false, key_column: nil) ⇒ ExcelSource

Returns a new instance of ExcelSource.



10
11
12
13
14
15
16
17
# 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

Instance Attribute Details

#group_byObject (readonly) Also known as: group_by?

Returns the value of attribute group_by.



8
9
10
# File 'lib/arc-furnace/excel_source.rb', line 8

def group_by
  @group_by
end

#key_columnObject (readonly)

Returns the value of attribute key_column.



8
9
10
# File 'lib/arc-furnace/excel_source.rb', line 8

def key_column
  @key_column
end

#valueObject (readonly)

Returns the value of attribute value.



8
9
10
# File 'lib/arc-furnace/excel_source.rb', line 8

def value
  @value
end

Instance Method Details

#build_enumeratorObject



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# 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_headersObject



34
35
36
# File 'lib/arc-furnace/excel_source.rb', line 34

def build_headers
  @header_row = excel.first
end

#build_row(row) ⇒ Object



51
52
53
54
55
56
# 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

#closeObject



78
79
80
# File 'lib/arc-furnace/excel_source.rb', line 78

def close
  @excel.close if @excel
end

#extract_cell_value(cell) ⇒ Object



43
44
45
46
47
48
49
# 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_rowsObject



38
39
40
41
# 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

#preprocessObject

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



25
26
27
28
29
30
31
32
# File 'lib/arc-furnace/excel_source.rb', line 25

def preprocess
  if group_by?
    build_headers
    group_rows
  else
    enumerator.next
  end
end