Module: SmarterCSV

Defined in:
lib/smarter_csv/parse.rb,
lib/smarter_csv/file_io.rb,
lib/smarter_csv/headers.rb,
lib/smarter_csv/version.rb,
lib/smarter_csv/variables.rb,
lib/smarter_csv/smarter_csv.rb,
lib/smarter_csv/auto_detection.rb,
lib/smarter_csv/header_validations.rb,
lib/smarter_csv/options_processing.rb,
lib/smarter_csv/hash_transformations.rb,
lib/smarter_csv/header_transformations.rb,
ext/smarter_csv/smarter_csv.c

Defined Under Namespace

Classes: DeprecatedOptions, DuplicateHeaders, HeaderSizeMismatch, IncorrectOption, KeyMappingError, MissingKeys, NoColSepDetected, SmarterCSVException, ValidationError

Constant Summary collapse

VERSION =
"1.11.0.pre2"
COMMON_OPTIONS =
{
  acceleration: true,
  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_simple_split: false,
  force_utf8: false,
  headers_in_file: true,
  invalid_byte_sequence: '',
  quote_char: '"',
  remove_unmapped_keys: false,
  row_sep: :auto, # was: $/,
  silence_deprecations: false, # new in 1.11
  silence_missing_keys: false,
  skip_lines: nil,
  user_provided_headers: nil,
  verbose: false,
  with_line_numbers: false,
  v2_mode: false,
}.freeze
V1_DEFAULT_OPTIONS =
{
  keep_original_headers: false,
  key_mapping: nil,
  remove_empty_hashes: true,
  remove_empty_values: true,
  remove_values_matching: nil,
  remove_zero_values: false,
  required_headers: nil,
  required_keys: nil,
  strings_as_keys: false,
  strip_chars_from_headers: nil,
  strip_whitespace: true,
  value_converters: nil,
  v2_mode: false,
}.freeze
DEPRECATED_OPTIONS =
[
  :convert_values_to_numeric,
  :downcase_headers,
  :keep_original_headers,
  :key_mapping,
  :remove_empty_hashes,
  :remove_empty_values,
  :remove_values_matching,
  :remove_zero_values,
  :required_headers,
  :required_keys,
  :stirngs_as_keys,
  :strip_cars_from_headers,
  :strip_whitespace,
  :value_converters,
].freeze

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.chunk_countObject (readonly)

Returns the value of attribute chunk_count.



5
6
7
# File 'lib/smarter_csv/variables.rb', line 5

def chunk_count
  @chunk_count
end

.csv_line_countObject (readonly)

Returns the value of attribute csv_line_count.



5
6
7
# File 'lib/smarter_csv/variables.rb', line 5

def csv_line_count
  @csv_line_count
end

.errorsObject (readonly)

Returns the value of attribute errors.



5
6
7
# File 'lib/smarter_csv/variables.rb', line 5

def errors
  @errors
end

.file_line_countObject (readonly)

Returns the value of attribute file_line_count.



5
6
7
# File 'lib/smarter_csv/variables.rb', line 5

def file_line_count
  @file_line_count
end

.has_railsObject (readonly)

Returns the value of attribute has_rails.



5
6
7
# File 'lib/smarter_csv/variables.rb', line 5

def has_rails
  @has_rails
end

.headersObject (readonly)

Returns the value of attribute headers.



5
6
7
# File 'lib/smarter_csv/variables.rb', line 5

def headers
  @headers
end

.raw_headerObject (readonly)

Returns the value of attribute raw_header.



5
6
7
# File 'lib/smarter_csv/variables.rb', line 5

def raw_header
  @raw_header
end

.resultObject (readonly)

Returns the value of attribute result.



5
6
7
# File 'lib/smarter_csv/variables.rb', line 5

def result
  @result
end

.warningsObject (readonly)

Returns the value of attribute warnings.



5
6
7
# File 'lib/smarter_csv/variables.rb', line 5

def warnings
  @warnings
end

Class Method Details

.apply_transformation(transformation, header_array, args, options) ⇒ Object



104
105
106
107
108
109
110
111
112
# File 'lib/smarter_csv/header_transformations.rb', line 104

def apply_transformation(transformation, header_array, args, options)
  if transformation.respond_to?(:call)
    # If transformation is a callable object (like a Proc)
    transformation.call(header_array, args, options)
  else
    # If transformation is a symbol (method name)
    public_send(transformation, header_array, args, options)
  end
end

.check_duplicate_headers_v1(headers, _options) ⇒ Object



20
21
22
23
24
25
26
27
28
29
# File 'lib/smarter_csv/header_validations.rb', line 20

def check_duplicate_headers_v1(headers, _options)
  header_counts = Hash.new(0)
  headers.each { |header| header_counts[header] += 1 unless header.nil? }

  duplicates = header_counts.select { |_, count| count > 1 }

  unless duplicates.empty?
    raise(SmarterCSV::DuplicateHeaders, "Duplicate Headers in CSV: #{duplicates.inspect}")
  end
end

.check_required_headers_v1(headers, options) ⇒ Object



31
32
33
34
35
36
37
38
39
40
# File 'lib/smarter_csv/header_validations.rb', line 31

def check_required_headers_v1(headers, options)
  if options[:required_keys] && options[:required_keys].is_a?(Array)
    headers_set = headers.to_set
    missing_keys = options[:required_keys].select { |k| !headers_set.include?(k) }

    unless missing_keys.empty?
      raise SmarterCSV::MissingKeys, "ERROR: missing attributes: #{missing_keys.join(',')}"
    end
  end
end

.convert_to_float(hash, _options) ⇒ Object



153
154
155
# File 'lib/smarter_csv/hash_transformations.rb', line 153

def convert_to_float(hash, _options)
  hash.each_key {|key| hash[key] = hash[key].to_f }
end

.convert_to_integer(hash, _options) ⇒ Object

IMPORTANT NOTE: this can lead to cases where a nil or empty value gets converted into 0 or 0.0, and can then not be properly removed!

you should first try to use convert_values_to_numeric or convert_values_to_numeric_unless_leading_zeroes



149
150
151
# File 'lib/smarter_csv/hash_transformations.rb', line 149

def convert_to_integer(hash, _options)
  hash.each_key {|key| hash[key] = hash[key].to_i }
end

.convert_values_to_numeric(hash, _options) ⇒ Object



121
122
123
124
125
126
127
128
129
130
# File 'lib/smarter_csv/hash_transformations.rb', line 121

def convert_values_to_numeric(hash, _options)
  hash.each_key do |k|
    case hash[k]
    when /^[+-]?\d+\.\d+$/
      hash[k] = hash[k].to_f
    when /^[+-]?\d+$/
      hash[k] = hash[k].to_i
    end
  end
end

.convert_values_to_numeric_unless_leading_zeroes(hash, _options) ⇒ Object



132
133
134
135
136
137
138
139
140
141
# File 'lib/smarter_csv/hash_transformations.rb', line 132

def convert_values_to_numeric_unless_leading_zeroes(hash, _options)
  hash.each_key do |k|
    case hash[k]
    when /^[+-]?[1-9]\d*\.\d+$/
      hash[k] = hash[k].to_f
    when /^[+-]?[1-9]\d*$/
      hash[k] = hash[k].to_i
    end
  end
end

.count_quote_chars(line, quote_char) ⇒ Object

Counts the number of quote characters in a line, excluding escaped quotes. FYI: using Ruby built-in regex processing to determine the number of quotes



175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/smarter_csv/smarter_csv.rb', line 175

def count_quote_chars(line, quote_char)
  return 0 if line.nil? || quote_char.nil? || quote_char.empty?

  # Escaped quote character (e.g., if quote_char is ", then escaped is \")
  escaped_quote = Regexp.escape(quote_char)

  # Pattern to match a quote character not preceded by a backslash
  pattern = /(?<!\\)(?:\\\\)*#{escaped_quote}/

  # Count occurrences
  line.scan(pattern).count
end

.default_optionsObject

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

ONLY FOR BACKWARDS-COMPATIBILITY



86
87
88
# File 'lib/smarter_csv/options_processing.rb', line 86

def default_options
  COMMON_OPTIONS.merge(V1_DEFAULT_OPTIONS)
end

.disambiguate_headers(headers, options) ⇒ Object



35
36
37
38
39
40
41
# File 'lib/smarter_csv/header_transformations.rb', line 35

def disambiguate_headers(headers, options)
  counts = Hash.new(0)
  headers.map do |header|
    counts[header] += 1
    counts[header] > 1 ? "#{header}#{options[:duplicate_header_suffix]}#{counts[header]}" : header
  end
end

.downcase_headers(headers, _options) ⇒ Object



133
134
135
136
137
# File 'lib/smarter_csv/header_transformations.rb', line 133

def downcase_headers(headers, _options)
  headers.map do |header|
    header.strip.downcase!
  end
end

.has_acceleration?Boolean

Returns:

  • (Boolean)


188
189
190
# File 'lib/smarter_csv/smarter_csv.rb', line 188

def has_acceleration?
  @has_acceleration ||= !!defined?(parse_csv_line_c)
end

.hash_transformations(hash, options) ⇒ Object

this is processing the headers from the input file



6
7
8
9
10
11
12
# File 'lib/smarter_csv/hash_transformations.rb', line 6

def hash_transformations(hash, options)
  if options[:v2_mode]
    hash_transformations_v2(hash, options)
  else
    hash_transformations_v1(hash, options)
  end
end

.hash_transformations_v1(hash, options) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/smarter_csv/hash_transformations.rb', line 14

def hash_transformations_v1(hash, options)
  # there may be unmapped keys, or keys purposedly mapped to nil or an empty key..
  # make sure we delete any key/value pairs from the hash, which the user wanted to delete:
  remove_empty_values = options[:remove_empty_values] == true
  remove_zero_values = options[:remove_zero_values]
  remove_values_matching = options[:remove_values_matching]
  convert_to_numeric = options[:convert_values_to_numeric]
  value_converters = options[:value_converters]

  hash.each_with_object({}) do |(k, v), new_hash|
    next if k.nil? || k == '' || k == :""
    next if remove_empty_values && (has_rails ? v.blank? : blank?(v))
    next if remove_zero_values && v.is_a?(String) && v =~ /^(0+|0+\.0+)$/ # values are Strings
    next if remove_values_matching && v =~ remove_values_matching

    # deal with the :only / :except options to :convert_values_to_numeric
    if convert_to_numeric && !limit_execution_for_only_or_except(options, :convert_values_to_numeric, k)
      if v =~ /^[+-]?\d+\.\d+$/
        v = v.to_f
      elsif v =~ /^[+-]?\d+$/
        v = v.to_i
      end
    end

    converter = value_converters[k] if value_converters
    v = converter.convert(v) if converter

    new_hash[k] = v
  end
end

.hash_transformations_v2(hash, options) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/smarter_csv/hash_transformations.rb', line 45

def hash_transformations_v2(hash, options)
  return hash if options[:hash_transformations].nil? || options[:hash_transformations].empty?

  # do the header transformations the user requested:
  if options[:hash_transformations]
    options[:hash_transformations].each do |transformation|
      if transformation.respond_to?(:call) # this is used when a user-provided Proc is passed in
        hash = transformation.call(hash, options)
      else
        case transformation
        when Symbol # this is used for pre-defined transformations that are defined in the SmarterCSV module
          hash = public_send(transformation, hash, options)
        when Hash # this is called for hash arguments, e.g. hash_transformations
          trans, args = transformation.first # .first treats the hash first element as an array
          hash = apply_transformation(trans, hash, args, options)
        when Array # this can be used for passing additional arguments in array form (e.g. into a Proc)
          trans, *args = transformation
          hash = apply_transformation(trans, hash, args, options)
        else
          raise SmarterCSV::IncorrectOption, "Invalid transformation type: #{transformation.class}"
        end
      end
    end
  end

  hash
end

.header_transformations(header_array, options) ⇒ Object

this is processing the headers from the input file



6
7
8
9
10
11
12
# File 'lib/smarter_csv/header_transformations.rb', line 6

def header_transformations(header_array, options)
  if options[:v2_mode]
    header_transformations_v2(header_array, options)
  else
    header_transformations_v1(header_array, options)
  end
end

.header_transformations_v1(header_array, options) ⇒ Object

—- V1.x Version: transform the headers that were in the file: ——————————————



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/smarter_csv/header_transformations.rb', line 16

def header_transformations_v1(header_array, options)
  header_array.map!{|x| x.gsub(%r/#{options[:quote_char]}/, '')}
  header_array.map!{|x| x.strip} if options[:strip_whitespace]

  unless options[:keep_original_headers]
    header_array.map!{|x| x.gsub(/\s+|-+/, '_')}
    header_array.map!{|x| x.downcase} if options[:downcase_header]
  end

  # detect duplicate headers and disambiguate
  header_array = disambiguate_headers(header_array, options) if options[:duplicate_header_suffix]
  # symbolize headers
  header_array = header_array.map{|x| x.to_sym } unless options[:strings_as_keys] || options[:keep_original_headers]
  # doesn't make sense to re-map when we have user_provided_headers
  header_array = remap_headers(header_array, options) if options[:key_mapping]

  header_array
end

.header_transformations_v2(header_array, options) ⇒ Object

—- V2.x Version: transform the headers that were in the file: ——————————————



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/smarter_csv/header_transformations.rb', line 76

def header_transformations_v2(header_array, options)
  return header_array if options[:header_transformations].nil? || options[:header_transformations].empty?

  # do the header transformations the user requested:
  if options[:header_transformations]
    options[:header_transformations].each do |transformation|
      if transformation.respond_to?(:call) # this is used when a user-provided Proc is passed in
        header_array = transformation.call(header_array, options)
      else
        case transformation
        when Symbol # this is used for pre-defined transformations that are defined in the SmarterCSV module
          header_array = public_send(transformation, header_array, options)
        when Hash # this is called for hash arguments, e.g. header_transformations
          trans, args = transformation.first # .first treats the hash first element as an array
          header_array = apply_transformation(trans, header_array, args, options)
        when Array # this can be used for passing additional arguments in array form (e.g. into a Proc)
          trans, *args = transformation
          header_array = apply_transformation(trans, header_array, args, options)
        else
          raise SmarterCSV::IncorrectOption, "Invalid transformation type: #{transformation.class}"
        end
      end
    end
  end

  header_array
end

.header_validations(headers, options) ⇒ Object



5
6
7
8
9
10
11
# File 'lib/smarter_csv/header_validations.rb', line 5

def header_validations(headers, options)
  if options[:v2_mode]
    header_validations_v2(headers, options)
  else
    header_validations_v1(headers, options)
  end
end

.header_validations_v1(headers, options) ⇒ Object

—- V1.x Version: validate the headers —————————————————————–



15
16
17
18
# File 'lib/smarter_csv/header_validations.rb', line 15

def header_validations_v1(headers, options)
  check_duplicate_headers_v1(headers, options)
  check_required_headers_v1(headers, options)
end

.header_validations_v2(headers, options) ⇒ Object

options.each do |validation|

  if validation.respond_to?(:call)
    # Directly call if it's a Proc or lambda
    validation.call(headers)
  else
    binding.pry
    # Handle Symbol, Hash, or Array
    method_name, args = validation.is_a?(Symbol) ? [validation, []] : validation
    public_send(method_name, headers, *Array(args))
  end
end

end



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/smarter_csv/header_validations.rb', line 60

def header_validations_v2(headers, options)
  return unless options[:header_validations]

  # do the header validations the user requested:
  # Header validations typically raise errors directly
  #
  options[:header_validations].each do |validation|
    if validation.respond_to?(:call)
      # Directly call if it's a Proc or lambda
      validation.call(headers)
    else
      case validation
      when Symbol
        public_send(validation, headers)
      when Hash
        val, args = validation.first
        public_send(val, headers, args)
      when Array
        val, *args = validation
        public_send(val, headers, args)
      else
        raise SmarterCSV::IncorrectOption, "Invalid validation type: #{validation.class}"
      end
    end
  end
end

.headerAObject

:nocov: rubocop:disable Naming/MethodName



24
25
26
27
# File 'lib/smarter_csv/variables.rb', line 24

def headerA
  warn "Deprecarion Warning: 'headerA' will be removed in future versions. Use 'headders'"
  @headerA
end

.initialize_variablesObject



7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/smarter_csv/variables.rb', line 7

def initialize_variables
  @has_rails = !!defined?(Rails)
  @csv_line_count = 0
  @chunk_count = 0
  @errors = {}
  @file_line_count = 0
  @headerA = []
  @headers = nil
  @raw_header = nil # header as it appears in the file
  @result = []
  @warnings = {}
  @v2_mode = false
  @enforce_utf8 = false # only set to true if needed (after options parsing)
end

.key_mapping(headers, mapping = {}, options) ⇒ Object



139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/smarter_csv/header_transformations.rb', line 139

def key_mapping(headers, mapping = {}, options)
  raise(SmarterCSV::IncorrectOption, "ERROR: incorrect format for key_mapping! Expecting hash with from -> to mappings") if mapping.empty? || !mapping.is_a?(Hash)

  headers_set = headers.to_set
  mapping_keys_set = mapping.keys.to_set
  silence_keys_set = (options[:silence_missing_keys] || []).to_set

  # Check for missing keys
  missing_keys = mapping_keys_set - headers_set - silence_keys_set
  raise SmarterCSV::KeyMappingError, "ERROR: cannot map headers: #{missing_keys.to_a.join(', ')}" if missing_keys.any? && !options[:silence_missing_keys]

  # Apply key mapping, retaining nils for explicitly mapped headers
  headers.map do |header|
    if mapping.key?(header)
      # Maps the key according to the mapping, including nil mapping
      mapping[header]
    elsif options[:remove_unmapped_keys]
      # Remove headers not specified in the mapping
      nil
    else
      # Keep the original header if not specified in the mapping
      header
    end
  end
end

.keys_as_strings(headers, options) ⇒ Object



127
128
129
130
131
# File 'lib/smarter_csv/header_transformations.rb', line 127

def keys_as_strings(headers, options)
  headers.map do |header|
    header.strip.gsub(%r{#{options[:quote_char]}}, '').downcase.gsub(/(\s|-)+/, '_')
  end
end

.keys_as_symbols(headers, options) ⇒ Object

these are some pre-defined header transformations which can be used all these take the headers array as the input

the computed options can be accessed via @options



121
122
123
124
125
# File 'lib/smarter_csv/header_transformations.rb', line 121

def keys_as_symbols(headers, options)
  headers.map do |header|
    header.strip.downcase.gsub(%r{#{options[:quote_char]}}, '').gsub(/(\s|-)+/, '_').to_sym
  end
end

.parse_csv_line_c(line, col_sep, quote_char, max_size) ⇒ Object

max_size: pass nil if no limit is specified



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
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
76
77
78
79
80
81
82
83
84
85
86
87
# File 'ext/smarter_csv/smarter_csv.c', line 15

static VALUE rb_parse_csv_line(VALUE self, VALUE line, VALUE col_sep, VALUE quote_char, VALUE max_size) {
  if (RB_TYPE_P(line, T_NIL) == 1) {
    return rb_ary_new();
  }

  if (RB_TYPE_P(line, T_STRING) != 1) {
    rb_raise(rb_eTypeError, "ERROR in SmarterCSV.parse_line: line has to be a string or nil");
  }

  rb_encoding *encoding = rb_enc_get(line); /* get the encoding from the input line */
  char *startP = RSTRING_PTR(line); /* may not be null terminated */
  long line_len = RSTRING_LEN(line);
  char *endP = startP + line_len ; /* points behind the string */
  char *p = startP;

  char *col_sepP = RSTRING_PTR(col_sep);
  long col_sep_len = RSTRING_LEN(col_sep);

  char *quoteP = RSTRING_PTR(quote_char);
  long quote_count = 0;

  bool col_sep_found = true;

  VALUE elements = rb_ary_new();
  VALUE field;
  long i;

  char prev_char = '\0'; // Store the previous character for comparison against an escape character
  long backslash_count = 0; // to count consecutive backslash characters

  while (p < endP) {
    /* does the remaining string start with col_sep ? */
    col_sep_found = true;
    for(i=0; (i < col_sep_len) && (p+i < endP) ; i++) {
      col_sep_found = col_sep_found && (*(p+i) == *(col_sepP+i));
    }
    /* if col_sep was found and we have even quotes */
    if (col_sep_found && (quote_count % 2 == 0)) {
      /* if max_size != nil && lements.size >= header_size */
      if ((max_size != Qnil) && RARRAY_LEN(elements) >= NUM2INT(max_size)) {
        break;
      } else {
        /* push that field with original encoding onto the results */
        field = rb_enc_str_new(startP, p - startP, encoding);
        rb_ary_push(elements, field);

        p += col_sep_len;
        startP = p;
      }
    } else {
      if (*p == '\\') {
        backslash_count++;
      } else {
        if (*p == *quoteP && (backslash_count % 2 == 0)) {
          quote_count++;
        }
        backslash_count = 0; // no more consecutive backslash characters
      }
      p++;
    }

    prev_char = *(p - 1); // Update the previous character
  } /* while */

  /* check if the last part of the line needs to be processed */
  if ((max_size == Qnil) || RARRAY_LEN(elements) < NUM2INT(max_size)) {
    /* copy the remaining line as a field with original encoding onto the results */
    field = rb_enc_str_new(startP, endP - startP, encoding);
    rb_ary_push(elements, field);
  }

  return elements;
}

.process(input, given_options = {}, &block) ⇒ Object

first parameter: filename or input object which responds to readline method



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/smarter_csv/smarter_csv.rb', line 15

def SmarterCSV.process(input, given_options = {}, &block) # rubocop:disable Lint/UnusedMethodArgument
  initialize_variables

  options = process_options(given_options)

  @enforce_utf8 = options[:force_utf8] || options[:file_encoding] !~ /utf-8/i
  @verbose = options[:verbose]

  begin
    fh = input.respond_to?(:readline) ? input : File.open(input, "r:#{options[:file_encoding]}")

    if (options[:force_utf8] || options[:file_encoding] =~ /utf-8/i) && (fh.respond_to?(:external_encoding) && fh.external_encoding != Encoding.find('UTF-8') || fh.respond_to?(:encoding) && fh.encoding != Encoding.find('UTF-8'))
      puts 'WARNING: you are trying to process UTF-8 input, but did not open the input with "b:utf-8" option. See README file "NOTES about File Encodings".'
    end

    # auto-detect the row separator
    options[:row_sep] = guess_line_ending(fh, options) if options[:row_sep]&.to_sym == :auto
    # attempt to auto-detect column separator
    options[:col_sep] = guess_column_separator(fh, options) if options[:col_sep]&.to_sym == :auto

    skip_lines(fh, options)

    @headers, header_size = process_headers(fh, options)
    @headerA = @headers # @headerA is deprecated, use @headers

    puts "Effective headers:\n#{pp(@headers)}\n" if @verbose

    header_validations(@headers, options)

    # in case we use chunking.. we'll need to set it up..
    if options[:chunk_size].to_i > 0
      use_chunks = true
      chunk_size = options[:chunk_size].to_i
      @chunk_count = 0
      chunk = []
    else
      use_chunks = false
    end

    # now on to processing all the rest of the lines in the CSV file:
    # fh.each_line |line|
    until fh.eof? # we can't use fh.readlines() here, because this would read the whole file into memory at once, and eof => true
      line = readline_with_counts(fh, options)

      # replace invalid byte sequence in UTF-8 with question mark to avoid errors
      line = enforce_utf8_encoding(line, options) if @enforce_utf8

      print "processing file line %10d, csv line %10d\r" % [@file_line_count, @csv_line_count] if @verbose

      next if options[:comment_regexp] && line =~ options[:comment_regexp] # ignore all comment lines if there are any

      # cater for the quoted csv data containing the row separator carriage return character
      # in which case the row data will be split across multiple lines (see the sample content in spec/fixtures/carriage_returns_rn.csv)
      # by detecting the existence of an uneven number of quote characters
      multiline = count_quote_chars(line, options[:quote_char]).odd?

      while multiline
        next_line = fh.readline(options[:row_sep])
        next_line = enforce_utf8_encoding(next_line, options) if @enforce_utf8
        line += next_line
        @file_line_count += 1

        break if fh.eof? # Exit loop if end of file is reached

        multiline = count_quote_chars(line, options[:quote_char]).odd?
      end

      # :nocov:
      if multiline && @verbose
        print "\nline contains uneven number of quote chars so including content through file line %d\n" % @file_line_count
      end
      # :nocov:

      line.chomp!(options[:row_sep])

      # --- SPLIT LINE & DATA TRANSFORMATIONS ------------------------------------------------------------
      dataA, _data_size = parse(line, options, header_size)

      dataA.map!{|x| x.strip} if options[:strip_whitespace]

      # if all values are blank, then ignore this line
      next if options[:remove_empty_hashes] && (dataA.empty? || blank?(dataA))

      # --- HASH TRANSFORMATIONS ------------------------------------------------------------
      hash = @headers.zip(dataA).to_h

      hash = hash_transformations(hash, options)

      # --- HASH VALIDATIONS ----------------------------------------------------------------
      # will go here, and be able to:
      #  - validate correct format of the values for fields
      #  - required fields to be non-empty
      #  - ...
      # -------------------------------------------------------------------------------------

      next if options[:remove_empty_hashes] && hash.empty?

      #
      # should HASH VALIDATIONS go here instead?
      #

      puts "CSV Line #{@file_line_count}: #{pp(hash)}" if @verbose == '2' # very verbose setting
      # optional adding of csv_line_number to the hash to help debugging
      hash[:csv_line_number] = @csv_line_count if options[:with_line_numbers]

      # process the chunks or the resulting hash
      if use_chunks
        chunk << hash # append temp result to chunk

        if chunk.size >= chunk_size || fh.eof? # if chunk if full, or EOF reached
          # do something with the chunk
          if block_given?
            yield chunk # do something with the hashes in the chunk in the block
          else
            @result << chunk.dup # Append chunk to result (use .dup to keep a copy after we do chunk.clear)
          end
          @chunk_count += 1
          chunk.clear # re-initialize for next chunk of data
        else
          # the last chunk may contain partial data, which is handled below
        end
        # while a chunk is being filled up we don't need to do anything else here

      else # no chunk handling
        if block_given?
          yield [hash] # do something with the hash in the block (better to use chunking here)
        else
          @result << hash
        end
      end
    end

    # print new line to retain last processing line message
    print "\n" if @verbose

    # handling of last chunk:
    if !chunk.nil? && chunk.size > 0
      # do something with the chunk
      if block_given?
        yield chunk # do something with the hashes in the chunk in the block
      else
        @result << chunk.dup # Append chunk to result (use .dup to keep a copy after we do chunk.clear)
      end
      @chunk_count += 1
      # chunk = [] # initialize for next chunk of data
    end
  ensure
    fh.close if fh.respond_to?(:close)
  end

  if block_given?
    @chunk_count # when we do processing through a block we only care how many chunks we processed
  else
    @result # returns either an Array of Hashes, or an Array of Arrays of Hashes (if in chunked mode)
  end
end

.process_headers(filehandle, options) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/smarter_csv/headers.rb', line 5

def process_headers(filehandle, options)
  @raw_header = nil # header as it appears in the file
  @headers = nil # the processed headers
  header_array = []
  file_header_size = nil

  # if headers_in_file, get the headers -> We get the number of columns, even when user provided headers
  if options[:headers_in_file] # extract the header line
    # process the header line in the CSV file..
    # the first line of a CSV file contains the header .. it might be commented out, so we need to read it anyhow
    header_line = @raw_header = readline_with_counts(filehandle, options)
    header_line = preprocess_header_line(header_line, options)

    file_header_array, file_header_size = parse(header_line, options)

    file_header_array = header_transformations(file_header_array, options)

  else
    unless options[:user_provided_headers]
      raise SmarterCSV::IncorrectOption, "ERROR: If :headers_in_file is set to false, you have to provide :user_provided_headers"
    end
  end

  if options[:user_provided_headers]
    unless options[:user_provided_headers].is_a?(Array) && !options[:user_provided_headers].empty?
      raise(SmarterCSV::IncorrectOption, "ERROR: incorrect format for user_provided_headers! Expecting array with headers.")
    end

    # use user-provided headers
    user_header_array = options[:user_provided_headers]
    # user_provided_headers: their count should match the headers_in_file if any
    if defined?(file_header_size) && !file_header_size.nil?
      if user_header_array.size != file_header_size
        raise SmarterCSV::HeaderSizeMismatch, "ERROR: :user_provided_headers defines #{user_header_array.size} headers !=  CSV-file has #{file_header_size} headers"
      else
        # we could print out the mapping of file_header_array to header_array here
      end
    end

    header_array = user_header_array
  else
    header_array = file_header_array
  end

  [header_array, header_array.size]
end

.process_options(given_options = {}) ⇒ Object

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



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/smarter_csv/options_processing.rb', line 65

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

  # fix invalid input
  given_options[:invalid_byte_sequence] = '' if given_options[:invalid_byte_sequence].nil?

  # warn about deprecated options / raises error for v2_mode
  handle_deprecations(given_options)

  given_options = preprocess_v2_options(given_options) if given_options[:v2_mode]

  @options = compute_default_options(given_options).merge!(given_options)
  puts "Computed options:\n#{pp(@options)}\n" if given_options[:verbose]

  validate_options!(@options)
  @options
end

.remap_headers(headers, options) ⇒ Object

do some key mapping on the keys in the file header if you want to completely delete a key, then map it to nil or to ”



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/smarter_csv/header_transformations.rb', line 45

def remap_headers(headers, options)
  key_mapping = options[:key_mapping]
  if key_mapping.empty? || !key_mapping.is_a?(Hash) || key_mapping.keys.empty?
    raise(SmarterCSV::IncorrectOption, "ERROR: incorrect format for key_mapping! Expecting hash with from -> to mappings")
  end

  key_mapping = options[:key_mapping]
  # if silence_missing_keys are not set, raise error if missing header
  missing_keys = key_mapping.keys - headers
  # if the user passes a list of speciffic mapped keys that are optional
  missing_keys -= options[:silence_missing_keys] if options[:silence_missing_keys].is_a?(Array)

  unless missing_keys.empty? || options[:silence_missing_keys] == true
    raise SmarterCSV::KeyMappingError, "ERROR: can not map headers: #{missing_keys.join(', ')}"
  end

  headers.map! do |header|
    if key_mapping.has_key?(header)
      key_mapping[header].nil? ? nil : key_mapping[header]
    elsif options[:remove_unmapped_keys]
      nil
    else
      header
    end
  end

  headers
end

.remove_blank_values(hash, _options) ⇒ Object



109
110
111
# File 'lib/smarter_csv/hash_transformations.rb', line 109

def remove_blank_values(hash, _options)
  hash.each_key {|key| hash.delete(key) if hash[key].nil? || hash[key].is_a?(String) && hash[key] !~ /[^[:space:]]/ }
end

.remove_empty_keys(hash, _options) ⇒ Object



117
118
119
# File 'lib/smarter_csv/hash_transformations.rb', line 117

def remove_empty_keys(hash, _options)
  hash.reject!{|key, _v| key.nil? || key.empty?}
end

.remove_zero_values(hash, _options) ⇒ Object



113
114
115
# File 'lib/smarter_csv/hash_transformations.rb', line 113

def remove_zero_values(hash, _options)
  hash.each_key {|key| hash.delete(key) if hash[key].is_a?(Numeric) && hash[key].zero? }
end

.required_headers(headers, required = []) ⇒ Object



125
126
127
128
129
130
131
132
133
134
# File 'lib/smarter_csv/header_validations.rb', line 125

def required_headers(headers, required = [])
  raise(SmarterCSV::IncorrectOption, "ERROR: required_headers validation needs an array argument") unless required.is_a?(Array)

  headers_set = headers.to_set
  missing = required.select { |r| !headers_set.include?(r) }

  unless missing.empty?
    raise(SmarterCSV::MissingKeys, "Missing Headers in CSV: #{missing.inspect}")
  end
end

.strip_spaces(hash, _options) ⇒ Object



105
106
107
# File 'lib/smarter_csv/hash_transformations.rb', line 105

def strip_spaces(hash, _options)
  hash.each_key {|key| hash[key].strip! unless hash[key].nil? } # &. syntax was introduced in Ruby 2.3 - need to stay backwards compatible
end

.unique_headers(headers) ⇒ Object

these are some pre-defined header validations which can be used all these take the headers array as the input

the computed options can be accessed via @options



114
115
116
117
118
119
120
121
122
123
# File 'lib/smarter_csv/header_validations.rb', line 114

def unique_headers(headers)
  header_counts = Hash.new(0)
  headers.each { |header| header_counts[header] += 1 unless header.nil? }

  duplicates = header_counts.select { |_, count| count > 1 }

  unless duplicates.empty?
    raise(SmarterCSV::DuplicateHeaders, "Duplicate Headers in CSV: #{duplicates.inspect}")
  end
end

.v1_backwards_compatibility(hash, options) ⇒ Object

To handle v1-backward-compatible behavior, it is faster to roll all behavior into one method



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/smarter_csv/hash_transformations.rb', line 76

def v1_backwards_compatibility(hash, options)
  hash.each_with_object({}) do |(k, v), new_hash|
    next if k.nil? || k == '' || k == :"" # remove_empty_keys
    next if has_rails ? v.blank? : blank?(v) # remove_empty_values

    # convert_values_to_numeric:
    # deal with the :only / :except options to :convert_values_to_numeric
    unless limit_execution_for_only_or_except(options, :convert_values_to_numeric, k)
      if v =~ /^[+-]?\d+\.\d+$/
        v = v.to_f
      elsif v =~ /^[+-]?\d+$/
        v = v.to_i
      end
    end

    new_hash[k] = v
  end
end

.value_converters(hash, _options) ⇒ Object

Building Blocks in case you want to build your own flow:



99
100
101
102
103
# File 'lib/smarter_csv/hash_transformations.rb', line 99

def value_converters(hash, _options)
  #
  # TO BE IMPLEMENTED
  #
end