Module: SmarterCSV::Options

Included in:
Reader
Defined in:
lib/smarter_csv/options.rb

Constant Summary collapse

DEFAULT_OPTIONS =
{
  acceleration: true, # if user wants to use accelleration or not
  auto_row_sep_chars: 500,
  chunk_size: nil,
  col_sep: :auto, # was: ',',
  comment_regexp: nil, # was: /\A#/,
  convert_values_to_numeric: true,
  downcase_header: true,
  duplicate_header_suffix: '', # was: nil,
  file_encoding: 'utf-8',
  force_utf8: false,
  headers_in_file: true,
  invalid_byte_sequence: '',
  keep_original_headers: false,
  key_mapping: nil,
  missing_header_prefix: 'column_',
  quote_char: '"',
  remove_empty_hashes: true,
  remove_empty_values: true,
  remove_unmapped_keys: false,
  remove_values_matching: nil,
  remove_zero_values: false,
  required_headers: nil,
  required_keys: nil,
  row_sep: :auto, # was: $/,
  silence_missing_keys: false,
  skip_lines: nil,
  strict: false,
  strings_as_keys: false,
  strip_chars_from_headers: nil,
  strip_whitespace: true,
  user_provided_headers: nil,
  value_converters: nil,
  verbose: false,
  with_line_numbers: false,
}.freeze

Instance Method Summary collapse

Instance Method Details

#process_options(given_options = {}) ⇒ Object

NOTE: this is not called when “parse” methods are tested by themselves



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/smarter_csv/options.rb', line 51

def process_options(given_options = {})
  puts "User provided options:\n#{pp(given_options)}\n" if given_options[:verbose]

  # Special case for :user_provided_headers:
  #
  # If we would use the default `headers_in_file: true`, and `:user_provided_headers` are given,
  # we could lose the first data row
  #
  # We now err on the side of treating an actual header as data, rather than losing a data row.
  #
  if given_options[:user_provided_headers] && !given_options.keys.include?(:headers_in_file)
    given_options[:headers_in_file] = false
    puts "WARNING: setting `headers_in_file: false` as a precaution to not lose the first row. Set explicitly to `true` if you have headers."
  end

  @options = DEFAULT_OPTIONS.dup.merge!(given_options)

  # fix invalid input
  @options[:invalid_byte_sequence] ||= ''

  puts "Computed options:\n#{pp(@options)}\n" if @options[:verbose]

  validate_options!(@options)
  @options
end