Sha256: 41aaacaad94ee8e036844c3e02ecb501ce9d2189a7351e097478b0a3b2b3af31

Contents?: true

Size: 1.22 KB

Versions: 14

Compression:

Stored size: 1.22 KB

Contents

require 'arc-furnace/enumerator_source'
require 'roo'

module ArcFurnace
  class ExcelSource < EnumeratorSource

    private_attr_reader :excel, :header_row
    attr_reader :value

    def initialize(filename: , sheet: nil)
      @excel = Roo::Excelx.new(filename)
      if sheet
        excel.default_sheet = sheet
      end
      super()
    end

    def close
      @excel.close if @excel
    end

    def preprocess
      enumerator.next
    end

    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

    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
  end
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
arc-furnace-0.1.30 lib/arc-furnace/excel_source.rb
arc-furnace-0.1.29 lib/arc-furnace/excel_source.rb
arc-furnace-0.1.28 lib/arc-furnace/excel_source.rb
arc-furnace-0.1.27 lib/arc-furnace/excel_source.rb
arc-furnace-0.1.26 lib/arc-furnace/excel_source.rb
arc-furnace-0.1.25 lib/arc-furnace/excel_source.rb
arc-furnace-0.1.24 lib/arc-furnace/excel_source.rb
arc-furnace-0.1.23 lib/arc-furnace/excel_source.rb
arc-furnace-0.1.22 lib/arc-furnace/excel_source.rb
arc-furnace-0.1.21 lib/arc-furnace/excel_source.rb
arc-furnace-0.1.20 lib/arc-furnace/excel_source.rb
arc-furnace-0.1.19 lib/arc-furnace/excel_source.rb
arc-furnace-0.1.18 lib/arc-furnace/excel_source.rb
arc-furnace-0.1.16 lib/arc-furnace/excel_source.rb