class CsvJ
note: for now CsvJson
is a class!! (not a module)
Constants
- MAJOR
- MINOR
- PATCH
- VERSION
Public Class Methods
build_logger()
click to toggle source
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?
# File lib/csvjson/parser.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
foreach( path, &block )
click to toggle source
# File lib/csvjson/parser.rb, line 48 def self.foreach( path, &block ) csv = open( path ) if block_given? begin csv.each( &block ) ensure csv.close end else csv.to_enum ## note: caller (responsible) must close file!!! ## remove version without block given - why? why not? ## use Csv.open().to_enum or Csv.open().each ## or Csv.new( File.new() ).to_enum or Csv.new( File.new() ).each ??? end end
logger()
click to toggle source
# File lib/csvjson/parser.rb, line 18 def self.logger() @@logger ||= build_logger; end
new( data )
click to toggle source
# File lib/csvjson/parser.rb, line 78 def initialize( data ) if data.is_a?( String ) @input = data # note: just needs each for each_line else ## assume io @input = data end end
open( path, mode=nil, &block )
click to toggle source
# File lib/csvjson/parser.rb, line 24 def self.open( path, mode=nil, &block ) ## rename path to filename or name - why? why not? ## note: default mode (if nil/not passed in) to 'r:bom|utf-8' f = File.open( path, mode ? mode : 'r:bom|utf-8' ) csv = new( f ) # handle blocks like Ruby's open() if block_given? begin block.call( csv ) ensure csv.close end else csv end end
parse( data, &block )
click to toggle source
# File lib/csvjson/parser.rb, line 66 def self.parse( data, &block ) csv = new( data ) if block_given? csv.each( &block ) ## note: caller (responsible) must close file!!! - add autoclose - why? why not? else # slurp contents, if no block is given csv.read ## note: caller (responsible) must close file!!! - add autoclose - why? why not? end end
read( path )
click to toggle source
# File lib/csvjson/parser.rb, line 43 def self.read( path ) open( path ) { |csv| csv.read } end
root()
click to toggle source
# File lib/csvjson/version.rb, line 20 def self.root "#{File.expand_path( File.dirname(File.dirname(File.dirname(__FILE__))) )}" end
version()
click to toggle source
# File lib/csvjson/version.rb, line 12 def self.version VERSION end
Public Instance Methods
close()
click to toggle source
# File lib/csvjson/parser.rb, line 127 def close @input.close if @input.respond_to?(:close) ## note: string needs no close end
each( &block )
click to toggle source
# File lib/csvjson/parser.rb, line 90 def each( &block ) if block_given? @input.each_line do |line| logger.debug "line:" if logger.debug? logger.debug line.pretty_inspect if logger.debug? ## note: chomp('') if is an empty string, ## it will remove all trailing newlines from the string. ## use line.sub(/[\n\r]*$/, '') or similar instead - why? why not? line = line.chomp( '' ) line = line.strip ## strip leading and trailing whitespaces (space/tab) too logger.debug line.pretty_inspect if logger.debug? if line.empty? ## skip blank lines logger.debug "skip blank line" if logger.debug? next end if line.start_with?( "#" ) ## skip comment lines logger.debug "skip comment line" if logger.debug? next end ## note: auto-wrap in array e.g. with [] json = JSON.parse( "[#{line}]" ) logger.debug json.pretty_inspect if logger.debug? block.call( json ) end else to_enum end end
logger()
click to toggle source
# File lib/csvjson/parser.rb, line 19 def logger() self.class.logger; end
read()
click to toggle source
# File lib/csvjson/parser.rb, line 125 def read() to_a; end