Sha256: 4d581584bb8c5de057996a2bef6c48960dafddf2995d41628c8de2604a256cee

Contents?: true

Size: 1.28 KB

Versions: 3

Compression:

Stored size: 1.28 KB

Contents

module CsvFastImporter

  # Gather all import configurations based on given file and additional parameters.
  # This class is also responsible for default configuration.
  class Configuration

    attr_accessor :file

    def initialize(file, parameters = {})
      @file = file
      @parameters = parameters
    end

    def encoding
      @encoding ||= @parameters[:encoding] || 'UTF-8'
    end

    def column_separator
      @column_separator ||= @parameters[:col_sep] || ';'
    end

    def mapping
      @mapping ||= downcase_keys_and_values(@parameters[:mapping] || {})
    end

    def destination_table
      @destination_table ||= (@parameters[:destination] || File.basename(@file, '.*'))
    end

    def row_index_column
      @row_index_column ||= @parameters[:row_index_column]
    end

    def transactional?
      @transactional ||= !(@parameters[:transaction] == :disabled)
    end

    def transactional_forced?
      @transactional_forced ||= (@parameters[:transaction] == :enabled)
    end

    def truncate?
      @deletion ||= @parameters[:deletion] == :truncate
    end

    def deletion?
      @deletion ||= !(@parameters[:deletion] == :none)
    end

  private

    def downcase_keys_and_values(hash)
      Hash[hash.map{ |k, v| [k.to_s.downcase, v.to_s.downcase] }]
    end

  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
csv_fast_importer-1.2.0 lib/csv_fast_importer/configuration.rb
csv_fast_importer-1.1.0 lib/csv_fast_importer/configuration.rb
csv_fast_importer-1.0.0 lib/csv_fast_importer/configuration.rb