Sha256: 4260cad071cd4a5f9b7fe9054a245ea2c26fd021defbd8e108be26fa3ae908b6

Contents?: true

Size: 1001 Bytes

Versions: 10

Compression:

Stored size: 1001 Bytes

Contents

require 'csv'

class Cranium::DataReader

  def initialize(source)
    @source = source
    @source_field_names = @source.fields.keys
  end



  def read(&block)
    @source.files.each do |input_file|
      read_input_file File.join(Cranium.configuration.upload_path, input_file), block
    end
  end



  private

  def read_input_file(input_file, read_block)
    Cranium::ProgressOutput.show_progress File.basename(input_file), Cranium::FileUtils.line_count(input_file) do |progress_bar|
      line_number = 0
      CSV.foreach input_file, csv_read_options_for(@source) do |row|
        next if 1 == (line_number += 1)

        record = Hash[@source_field_names.zip row]
        self.instance_exec record, &read_block

        progress_bar.inc
      end
    end
  end



  def csv_read_options_for(source_definition)
    {
      encoding: source_definition.encoding,
      col_sep: source_definition.delimiter,
      quote_char: source_definition.quote,
      return_headers: false
    }
  end

end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
cranium-0.8.1 lib/cranium/data_reader.rb
cranium-0.8 lib/cranium/data_reader.rb
cranium-0.7 lib/cranium/data_reader.rb
cranium-0.6.1 lib/cranium/data_reader.rb
cranium-0.5 lib/cranium/data_reader.rb
cranium-0.4.3 lib/cranium/data_reader.rb
cranium-0.4.2 lib/cranium/data_reader.rb
cranium-0.4.1 lib/cranium/data_reader.rb
cranium-0.4 lib/cranium/data_reader.rb
cranium-0.3.1 lib/cranium/data_reader.rb