Class: CsvReader::ParserStrict

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

Constant Summary collapse

BACKSLASH =

char constants

"\\"
LF =

use BACKSLASH_ESCAPE ??

"\n"
CR =

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

"\r"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(sep: ',', quote: '"', doublequote: true, escape: false, null: nil, comment: false) ⇒ ParserStrict

Returns a new instance of ParserStrict.



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/csvreader/parser_strict.rb', line 33

def initialize( sep:         ',',
                quote:       '"',  ## note: set to false/nil for no quote
                doublequote: true,
                escape:      false,   ## true/false
                null:        nil,     ## note: set to nil for no null vales / not availabe (na)
                comment:     false   ## note: comment char e.g. # or false/nil
               )
  @config = {}   ## todo/fix: change config to proper dialect class/struct - why? why not?
  @config[:sep]          = sep
  @config[:quote]        = quote
  @config[:doublequote]  = doublequote
  @config[:escape]  = escape
  @config[:null]     = null
  @config[:comment] = comment
end

Instance Attribute Details

#configObject (readonly)

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



31
32
33
# File 'lib/csvreader/parser_strict.rb', line 31

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?


21
22
23
24
25
# File 'lib/csvreader/parser_strict.rb', line 21

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



26
# File 'lib/csvreader/parser_strict.rb', line 26

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

Instance Method Details

#comment=(value) ⇒ Object



59
# File 'lib/csvreader/parser_strict.rb', line 59

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

#doublequote=(value) ⇒ Object



56
# File 'lib/csvreader/parser_strict.rb', line 56

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

#escape=(value) ⇒ Object



57
# File 'lib/csvreader/parser_strict.rb', line 57

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

#loggerObject



27
# File 'lib/csvreader/parser_strict.rb', line 27

def logger()  self.class.logger; end

#null=(value) ⇒ Object



58
# File 'lib/csvreader/parser_strict.rb', line 58

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

#parse(data, sep: , &block) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/csvreader/parser_strict.rb', line 63

def parse( data, sep: config[:sep], &block )
  ## note: data - will wrap either a String or IO object passed in data

  ##   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, sep: sep, &block )
  else
    records = []

    parse_lines( input, sep: sep ) do |record|
      records << record
    end

    records
  end

end

#quote=(value) ⇒ Object



55
# File 'lib/csvreader/parser_strict.rb', line 55

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

#sep=(value) ⇒ Object

config convenience helpers

e.g. use like  Csv.mysql.sep = ','   etc.   instead of
               Csv.mysql.config[:sep] = ','


54
# File 'lib/csvreader/parser_strict.rb', line 54

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