Class: Uncsv::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/uncsv/config.rb

Overview

Configuration options for parsing CSVs. It is a struct-like object with attribute acessors.

Constant Summary collapse

CSV_OPTS =

Options that directly map to Std-lib CSV options

%i[
  col_sep row_sep quote_char field_size_limit
].freeze
DEFAULTS =

The default values applied if an attribute's value is not specified when constructing a new Config object.

{
  col_sep: ',',
  expand_headers: false,
  field_size_limit: nil,
  header_rows: [],
  header_separator: '.',
  nil_empty: true,
  normalize_headers: false,
  quote_char: '"',
  row_sep: :auto,
  skip_rows: [],
  skip_blanks: false,
  unique_headers: false
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Config

Create a new Config object.

Options will be set to the defaults unless overridden by the opts parameter.

Parameters:

  • opts (Hash) (defaults to: {})

    A hash of configuration options. See the individual attributes for detailed descriptions.

See Also:



159
160
161
# File 'lib/uncsv/config.rb', line 159

def initialize(opts = {})
  DEFAULTS.merge(opts).each { |k, v| public_send("#{k}=", v) }
end

Instance Attribute Details

#col_sepString

The string that separates each field

Default: ",".

Returns:

  • (String)

    The column separator string



35
36
37
# File 'lib/uncsv/config.rb', line 35

def col_sep
  @col_sep
end

#expand_headersArray

Whether to fill empty headers with values from the left.

Default false. If set to true, blank header row cells will assume the header of the row to their left. This is useful for heirarchical headers where not all the header cells are filled in. If set to an array of header indexes, only the specified headers will be expanded.

Returns:

  • (Array)

    An array of expaned header indexes



# File 'lib/uncsv/config.rb', line 37

#field_size_limitnil, Integer

The maximum size CSV will read ahead looking for a closing quote.

Default: nil.

Returns:

  • (nil, Integer)

    The maximum field size



53
54
55
# File 'lib/uncsv/config.rb', line 53

def field_size_limit
  @field_size_limit
end

#header_rowsArray

Indexes of the rows to use as headers

Default: []. Accepts an array of zero-based indexes or a single index. For example, it could be set to 0 to indicate a header in the first row. If set to an array of indexes ([1,2]), the header row text will be joined by the :header_separator. For example, if if the cell (0,0) had the value "Personal" and cell (1,0) had the value "Name", the header would become "Personal.Name". Any data above the last header row will be ignored.

Returns:

  • (Array)

    The header row indexes



66
67
68
# File 'lib/uncsv/config.rb', line 66

def header_rows
  @header_rows
end

#header_separatorString

The separator between multiple header fields

Default: ".". When using multiple header rows, this is a string used to separate the individual header fields.

Returns:

  • (String)

    The separator string



74
75
76
# File 'lib/uncsv/config.rb', line 74

def header_separator
  @header_separator
end

#nil_emptyBoolean

Whether to represent empty cells as nil.

Default false. If true, empty cells will be set to nil, otherwise, they are set to an empty string.

Returns:

  • (Boolean)

    Whether empty cells will be niled



82
83
84
# File 'lib/uncsv/config.rb', line 82

def nil_empty
  @nil_empty
end

#normalize_headersKeyNormalizer, Object

Whether to rewrite headers to a standard format

Default false. If set to true, header field text will be normalized. The text will be lowercased, and non-alphanumeric characters will be replaced with underscores (_).

If set to a string, those characters will be replaced with the string instead.

If set to a hash, the hash will be treated as options to KeyNormalizer, accepting the :separator, and :downcase options.

If set to another object, it is expected to respond to the normalize(key) method by returning a normalized string.

Returns:

  • (KeyNormalizer, Object)

    The KeyNormalizer object or equivalent

See Also:



101
102
103
# File 'lib/uncsv/config.rb', line 101

def normalize_headers
  @normalize_headers
end

#quote_charString

The character used to quote individual fields

Default '"'. If set to true, header field text will be normalized. The text will be lowercased, and non-alphanumeric characters will be replaced with underscores (_). If set to a string, those characters will be replaced with the string instead.

Returns:

  • (String)

    The quote character



112
113
114
# File 'lib/uncsv/config.rb', line 112

def quote_char
  @quote_char
end

#row_sep:auto, String

The string at the end of each row

Default :auto.

Returns:

  • (:auto, String)

    The row separator



120
121
122
# File 'lib/uncsv/config.rb', line 120

def row_sep
  @row_sep
end

#skip_blanksBoolean

Whether to skip blank rows

Default false. If true, rows whose fields are all empty will be skipped.

Returns:

  • (Boolean)

    Whether blank rows will be skipped



128
129
130
# File 'lib/uncsv/config.rb', line 128

def skip_blanks
  @skip_blanks
end

#skip_rowsArray

An array of row indexes to skip

Default []. If set to an array of zero-based row indexes, those rows will be skipped. This option does not apply to header rows.

Returns:

  • (Array)

    The row index to skip



136
137
138
# File 'lib/uncsv/config.rb', line 136

def skip_rows
  @skip_rows
end

#unique_headersBoolean

Whether to force headers to be unique

Default false. If set to true, headers will be forced to be unique by appending numbers to duplicates. For example, if two header cells have the text "Name", the headers will become "Name.0", and "Name.1". The separator between the text and the number can be set using the :header_separator option.

Returns:

  • (Boolean)

    Whether headers will be uniqued



147
148
149
# File 'lib/uncsv/config.rb', line 147

def unique_headers
  @unique_headers
end

Instance Method Details

#csv_optsHash

Get options passed through to CSV#new.

Returns:

  • (Hash)

    A hash of the CSV options

See Also:



200
201
202
# File 'lib/uncsv/config.rb', line 200

def csv_opts
  Hash[CSV_OPTS.map { |k| [k, public_send(k)] }]
end