Class: CsvReader::ParserTable

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(space: nil) ⇒ ParserTable

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!!!


31
32
33
34
35
36
37
38
39
# File 'lib/csvreader/parser_table.rb', line 31

def initialize( space: nil )
  @config = {}   ## todo/fix: change config to proper dialect class/struct - why? why not?

  ## e.g. treat/convert char to space e.g. _-+• etc
  ##   Man_Utd   => Man Utd
  ##  or use it for leading and trailing spaces without quotes
  ##  todo/check: only use for unquoted values? why? why not?
  @config[:space]   = space
end

Instance Attribute Details

#configObject (readonly)

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



24
25
26
# File 'lib/csvreader/parser_table.rb', line 24

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?


13
14
15
16
17
# File 'lib/csvreader/parser_table.rb', line 13

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



18
# File 'lib/csvreader/parser_table.rb', line 18

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

Instance Method Details

#loggerObject



19
# File 'lib/csvreader/parser_table.rb', line 19

def logger()  self.class.logger; end

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



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

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

  ## note: input: required each_line (string or io/file for example)
  ## note: kwargs NOT used for now (but required for "protocol/interface" by other parsers)

  input = str_or_readable   ## assume it's a string or io/file handle

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

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

    records
  end
end

#space=(value) ⇒ Object

config convenience helpers



44
# File 'lib/csvreader/parser_table.rb', line 44

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