Class: CsvReader::ParserStd

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

Constant Summary collapse

DOUBLE_QUOTE =

char constants

"\""
SINGLE_QUOTE =
"'"
BACKSLASH =

use BACKSLASH_ESCAPE ??

"\\"
COMMENT1 =

use COMMENT_HASH or HASH or ??

"#"
COMMENT2 =

use COMMENT_PERCENT or PERCENT or ??

"%"
DIRECTIVE =

use a different name e.g. AT 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'], numeric: false, nan: nil) ⇒ 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!!!


52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/csvreader/parser_std.rb', line 52

def initialize( null:     ['\N', 'NA'],  ## note: set to nil for no null vales / not availabe (na)
                numeric:  false,   ## (auto-)convert all non-quoted values to float
                nan:      nil      ## note: only if numeric - set mappings for Float::NAN (not a number) values
              )
  @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
  @config[:numeric] = numeric
  @config[:nan]     = nan   # not a number (NaN) e.g. Float::NAN

  @meta  = nil     ## no meta data block   (use empty hash {} - why? why not?)
end

Instance Attribute Details

#configObject (readonly)

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



44
45
46
# File 'lib/csvreader/parser_std.rb', line 44

def config
  @config
end

#metaObject (readonly)

Returns the value of attribute meta.



45
46
47
# File 'lib/csvreader/parser_std.rb', line 45

def meta
  @meta
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?


33
34
35
36
37
# File 'lib/csvreader/parser_std.rb', line 33

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



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

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

Instance Method Details

#loggerObject



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

def logger()  self.class.logger; end

#nan=(value) ⇒ Object



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

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

#null=(value) ⇒ Object

config convenience helpers

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


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

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

#numeric=(value) ⇒ Object



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

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

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



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/csvreader/parser_std.rb', line 81

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