Class: Csv::Configuration

Inherits:
Object
  • Object
show all
Defined in:
lib/csvreader/reader.rb

Overview

STD_CSV_ENGINE = CSV ## to avoid name confusion use longer name - why? why not? find a better name? use __CSV__ or similar? or just ::CSV ??

Constant Summary collapse

COMMENTS_REGEX =

lines starting with # (note: only leading spaces allowed)

/^\s*#/
BLANK_REGEX =

skip all whitespace lines - note: use “” or , for a blank record!!!

/^\s*$/
SKIP_REGEX =
Regexp.union( COMMENTS_REGEX, BLANK_REGEX )

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfiguration

Returns a new instance of Configuration.



27
28
29
30
31
32
33
# File 'lib/csvreader/reader.rb', line 27

def initialize
  @sep = ','
  ## note: do NOT add headers as global - should ALWAYS be explicit
  ##   headers (true/false) - changes resultset and requires different processing!!!

  self  ## return self for chaining
end

Instance Attribute Details

#sepObject

col_sep (column separator)



25
26
27
# File 'lib/csvreader/reader.rb', line 25

def sep
  @sep
end

Instance Method Details

#blank?(line) ⇒ Boolean

Returns:

  • (Boolean)


35
36
37
38
39
40
# File 'lib/csvreader/reader.rb', line 35

def blank?( line )
  ## note:  blank line does NOT include "blank" with spaces only!!
  ##          use BLANK_REGEX in skip_lines to clean-up/skip/remove/ignore
  ##  see skip_blanks in default_options
  line.empty?
end

#default_optionsObject

built-in (default) options

todo: find a better name?


55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/csvreader/reader.rb', line 55

def default_options
  ## note:
  ##   do NOT include sep character and
  ##   do NOT include headers true/false here
  ##
  ##  make default sep its own "global" default config
  ##   e.g. Csv.config.sep =

  ## common options
  ##   skip comments starting with #
  ##   skip blank lines
  ##   strip leading and trailing spaces
  ##    NOTE/WARN:  leading and trailing spaces NOT allowed/working with double quoted values!!!!
  defaults = {
    skip_blanks: true,    ## note: skips lines with no whitespaces only!! (e.g. line with space is NOT blank!!)
    skip_lines:  SKIP_REGEX,
    :converters => :strip
  }
  defaults
end

#skip?(line) ⇒ Boolean

Returns:

  • (Boolean)


47
48
49
50
51
# File 'lib/csvreader/reader.rb', line 47

def skip?( line )
  ## check if comment line - skip comments
  ##  see skip_lines in default_options
  line =~ SKIP_REGEX
end