class CsvY
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/csvyaml/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/csvyaml/parser.rb, line 49 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/csvyaml/parser.rb, line 18 def self.logger() @@logger ||= build_logger; end
new( str_or_readable )
click to toggle source
# File lib/csvyaml/parser.rb, line 79 def initialize( str_or_readable ) if str_or_readable.is_a?( String ) @input = str_or_readable # note: just needs each for each_line else ## assume io @input = str_or_readable end end
open( path, mode=nil, &block )
click to toggle source
# File lib/csvyaml/parser.rb, line 25 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( str_or_readable, &block )
click to toggle source
# File lib/csvyaml/parser.rb, line 67 def self.parse( str_or_readable, &block ) csv = new( str_or_readable ) 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/csvyaml/parser.rb, line 44 def self.read( path ) open( path ) { |csv| csv.read } end
root()
click to toggle source
# File lib/csvyaml/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/csvyaml/version.rb, line 12 def self.version VERSION end
Public Instance Methods
close()
click to toggle source
# File lib/csvyaml/parser.rb, line 129 def close @input.close if @input.respond_to?(:close) ## note: string needs no close end
each( &block )
click to toggle source
# File lib/csvyaml/parser.rb, line 91 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 [] ## add document marker (---) why? why not? values = YAML.load( "---\n[#{line}]" ) logger.debug values.pretty_inspect if logger.debug? block.call( values ) end else to_enum end end
logger()
click to toggle source
# File lib/csvyaml/parser.rb, line 19 def logger() self.class.logger; end
read()
click to toggle source
# File lib/csvyaml/parser.rb, line 127 def read() to_a; end