Class: Philiprehberger::CsvKit::Dialect

Inherits:
Object
  • Object
show all
Defined in:
lib/philiprehberger/csv_kit/dialect.rb

Overview

Predefined and custom CSV dialects for controlling parsing and writing behavior.

Constant Summary collapse

PRESETS =
{
  excel: { col_sep: ',', row_sep: "\r\n", strip: true },
  excel_tab: { col_sep: "\t" },
  unix: { col_sep: ',', row_sep: "\n" }
}.freeze
OPTION_MAP =
{
  delimiter: :col_sep,
  quote: :quote_char,
  line_ending: :row_sep
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name_or_hash) ⇒ Dialect

Build a Dialect from a preset name or a custom options hash.

Parameters:

  • name_or_hash (Symbol, Hash)

    preset name (:excel, :excel_tab, :unix) or custom hash



25
26
27
# File 'lib/philiprehberger/csv_kit/dialect.rb', line 25

def initialize(name_or_hash)
  @options = resolve(name_or_hash)
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



19
20
21
# File 'lib/philiprehberger/csv_kit/dialect.rb', line 19

def options
  @options
end

Instance Method Details

#merge_into(base) ⇒ Hash

Merge dialect options into a base CSV options hash.

Parameters:

  • base (Hash)

    base CSV options

Returns:

  • (Hash)

    merged options



33
34
35
# File 'lib/philiprehberger/csv_kit/dialect.rb', line 33

def merge_into(base)
  base.merge(@options)
end