Sha256: 4bab70447e327cd4874a47388c40efefc6796e45269bf6408709e100a0f3105f

Contents?: true

Size: 1.06 KB

Versions: 34

Compression:

Stored size: 1.06 KB

Contents

require 'arc-furnace/source'
require 'arc-furnace/csv_to_hash_with_duplicate_headers'
require 'csv'

module ArcFurnace
  class MultiCSVSource < Source
    include CSVToHashWithDuplicateHeaders
    private_attr_reader :csv, :filenames, :encoding
    attr_reader :value, :file

    def initialize(filenames: , encoding: 'UTF-8')
      @encoding = encoding
      @filenames = filenames.reverse
      open_next_file
    end

    # Is this source empty?
    def empty?
      !value
    end

    def advance
      advance_in_current_file || open_next_file
    end

    private

    def advance_in_current_file
      @value =
          begin
            csv_to_hash_with_duplicates(csv.next)
          rescue StopIteration
            nil
          end
      value
    end

    def open_next_file
      file.close if file
      @file = nil
      if filenames.empty?
        nil
      else
        @file = File.open(filenames.pop, encoding: encoding)
        @csv = CSV.new(file, encoding: encoding, headers: true).each
        advance_in_current_file || open_next_file
      end
    end
  end
end

Version data entries

34 entries across 34 versions & 1 rubygems

Version Path
arc-furnace-0.1.22 lib/arc-furnace/multi_csv_source.rb
arc-furnace-0.1.21 lib/arc-furnace/multi_csv_source.rb
arc-furnace-0.1.20 lib/arc-furnace/multi_csv_source.rb
arc-furnace-0.1.19 lib/arc-furnace/multi_csv_source.rb
arc-furnace-0.1.18 lib/arc-furnace/multi_csv_source.rb
arc-furnace-0.1.16 lib/arc-furnace/multi_csv_source.rb
arc-furnace-0.1.15 lib/arc-furnace/multi_csv_source.rb
arc-furnace-0.1.14 lib/arc-furnace/multi_csv_source.rb
arc-furnace-0.1.13 lib/arc-furnace/multi_csv_source.rb
arc-furnace-0.1.12 lib/arc-furnace/multi_csv_source.rb
arc-furnace-0.1.11 lib/arc-furnace/multi_csv_source.rb
arc-furnace-0.1.10 lib/arc-furnace/multi_csv_source.rb
arc-furnace-0.1.9 lib/arc-furnace/multi_csv_source.rb
arc-furnace-0.1.8 lib/arc-furnace/multi_csv_source.rb