Class: CsvReader::ParserStd

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

Constant Summary collapse

DOUBLE_QUOTE =

char constants

"\""
BACKSLASH =

use BACKSLASH_ESCAPE ??

"\\"
COMMENT =

use COMMENT_HASH or HASH or ??

"#"
SPACE =

s == ASCII 32 (dec) = (Space)

" "
TAB =

t == ASCII 0x09 (hex) = HT (Tab/horizontal tab)

"\t"
LF =

n == ASCII 0x0A (hex) 10 (dec) = LF (Newline/line feed)

"\n"
CR =

r == ASCII 0x0D (hex) 13 (dec) = CR (Carriage return)

"\r"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(null: ['\N', 'NA']) ⇒ ParserStd

todo/check:

null values - include NA - why? why not?
    make null values case sensitive or add an option for case sensitive
    or better allow a proc as option for checking too!!!


47
48
49
50
51
52
53
54
55
# File 'lib/csvreader/parser_std.rb', line 47

def initialize( null: ['\N', 'NA']  ## note: set to nil for no null vales / not availabe (na)
              )
  @config = {}   ## todo/fix: change config to proper dialect class/struct - why? why not?

  ## note: null values must get handled by parser
  ##   only get checked for unquoted strings (and NOT for quoted strings)
  ##   "higher-level" code only knows about strings and has no longer any info if string was quoted or unquoted
  @config[:null] = null   ## null values
end

Instance Attribute Details

#configObject (readonly)

todo/fix: change config to proper dialect class/struct - why? why not?



40
41
42
# File 'lib/csvreader/parser_std.rb', line 40

def config
  @config
end

Class Method Details

.build_loggerObject

add simple logger with debug flag/switch

use Parser.debug = true   # to turn on

todo/fix: use logutils instead of std logger - why? why not?


29
30
31
32
33
# File 'lib/csvreader/parser_std.rb', line 29

def self.build_logger()
  l = Logger.new( STDOUT )
  l.level = :info    ## set to :info on start; note: is 0 (debug) by default
  l
end

.loggerObject



34
# File 'lib/csvreader/parser_std.rb', line 34

def self.logger() @@logger ||= build_logger; end

Instance Method Details

#loggerObject



35
# File 'lib/csvreader/parser_std.rb', line 35

def logger()  self.class.logger; end

#null=(value) ⇒ Object

config convenience helpers

e.g. use like  Csv.defaultl.null = '\N'   etc.   instead of
               Csv.default.config[:null] = '\N'


61
# File 'lib/csvreader/parser_std.rb', line 61

def null=( value )     @config[:null]=value; end

#parse(data, **kwargs, &block) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/csvreader/parser_std.rb', line 66

def parse( data, **kwargs, &block )

  ## note: data - will wrap either a String or IO object passed in data
  ## note: kwargs NOT used for now (but required for "protocol/interface" by other parsers)

  ##   make sure data (string or io) is a wrapped into Buffer!!!!!!
  if data.is_a?( Buffer )    ### allow (re)use of Buffer if managed from "outside"
    input = data
  else
    input = Buffer.new( data )
  end

  if block_given?
    parse_lines( input, &block )
  else
    records = []

    parse_lines( input ) do |record|
      records << record
    end

    records
  end
end