Class: Uncsv::Config
- Inherits:
-
Object
- Object
- Uncsv::Config
- 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
-
#col_sep ⇒ String
The string that separates each field.
-
#expand_headers ⇒ Array
Whether to fill empty headers with values from the left.
-
#field_size_limit ⇒ nil, Integer
The maximum size CSV will read ahead looking for a closing quote.
-
#header_rows ⇒ Array
Indexes of the rows to use as headers.
-
#header_separator ⇒ String
The separator between multiple header fields.
-
#nil_empty ⇒ Boolean
Whether to represent empty cells as
nil
. -
#normalize_headers ⇒ KeyNormalizer, Object
Whether to rewrite headers to a standard format.
-
#quote_char ⇒ String
The character used to quote individual fields.
-
#row_sep ⇒ :auto, String
The string at the end of each row.
-
#skip_blanks ⇒ Boolean
Whether to skip blank rows.
-
#skip_rows ⇒ Array
An array of row indexes to skip.
-
#unique_headers ⇒ Boolean
Whether to force headers to be unique.
Instance Method Summary collapse
-
#csv_opts ⇒ Hash
Get options passed through to
CSV#new
. -
#initialize(opts = {}) ⇒ Config
constructor
Create a new
Config
object.
Constructor Details
Instance Attribute Details
#col_sep ⇒ String
The string that separates each field
Default: ","
.
35 36 37 |
# File 'lib/uncsv/config.rb', line 35 def col_sep @col_sep end |
#expand_headers ⇒ Array
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.
|
# File 'lib/uncsv/config.rb', line 37
|
#field_size_limit ⇒ nil, Integer
The maximum size CSV will read ahead looking for a closing quote.
Default: nil
.
53 54 55 |
# File 'lib/uncsv/config.rb', line 53 def field_size_limit @field_size_limit end |
#header_rows ⇒ Array
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.
66 67 68 |
# File 'lib/uncsv/config.rb', line 66 def header_rows @header_rows end |
#header_separator ⇒ String
The separator between multiple header fields
Default: "."
. When using multiple header rows, this is a string used
to separate the individual header fields.
74 75 76 |
# File 'lib/uncsv/config.rb', line 74 def header_separator @header_separator end |
#nil_empty ⇒ Boolean
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.
82 83 84 |
# File 'lib/uncsv/config.rb', line 82 def nil_empty @nil_empty end |
#normalize_headers ⇒ KeyNormalizer, 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.
101 102 103 |
# File 'lib/uncsv/config.rb', line 101 def normalize_headers @normalize_headers end |
#quote_char ⇒ String
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.
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
.
120 121 122 |
# File 'lib/uncsv/config.rb', line 120 def row_sep @row_sep end |
#skip_blanks ⇒ Boolean
Whether to skip blank rows
Default false
. If true
, rows whose fields are all empty will be
skipped.
128 129 130 |
# File 'lib/uncsv/config.rb', line 128 def skip_blanks @skip_blanks end |
#skip_rows ⇒ Array
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.
136 137 138 |
# File 'lib/uncsv/config.rb', line 136 def skip_rows @skip_rows end |
#unique_headers ⇒ Boolean
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.
147 148 149 |
# File 'lib/uncsv/config.rb', line 147 def unique_headers @unique_headers end |
Instance Method Details
#csv_opts ⇒ Hash
Get options passed through to CSV#new
.
200 201 202 |
# File 'lib/uncsv/config.rb', line 200 def csv_opts Hash[CSV_OPTS.map { |k| [k, public_send(k)] }] end |