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.42 lib/arc-furnace/multi_csv_source.rb
arc-furnace-0.1.41 lib/arc-furnace/multi_csv_source.rb
arc-furnace-0.1.40 lib/arc-furnace/multi_csv_source.rb
arc-furnace-0.1.39 lib/arc-furnace/multi_csv_source.rb
arc-furnace-0.1.38 lib/arc-furnace/multi_csv_source.rb
arc-furnace-0.1.37 lib/arc-furnace/multi_csv_source.rb
arc-furnace-0.1.36 lib/arc-furnace/multi_csv_source.rb
arc-furnace-0.1.35 lib/arc-furnace/multi_csv_source.rb
arc-furnace-0.1.34 lib/arc-furnace/multi_csv_source.rb
arc-furnace-0.1.33 lib/arc-furnace/multi_csv_source.rb
arc-furnace-0.1.32 lib/arc-furnace/multi_csv_source.rb
arc-furnace-0.1.31 lib/arc-furnace/multi_csv_source.rb
arc-furnace-0.1.30 lib/arc-furnace/multi_csv_source.rb
arc-furnace-0.1.29 lib/arc-furnace/multi_csv_source.rb
arc-furnace-0.1.28 lib/arc-furnace/multi_csv_source.rb
arc-furnace-0.1.27 lib/arc-furnace/multi_csv_source.rb
arc-furnace-0.1.26 lib/arc-furnace/multi_csv_source.rb
arc-furnace-0.1.25 lib/arc-furnace/multi_csv_source.rb
arc-furnace-0.1.24 lib/arc-furnace/multi_csv_source.rb
arc-furnace-0.1.23 lib/arc-furnace/multi_csv_source.rb