lib/rio/ext/csv.rb in rio-0.3.2 vs lib/rio/ext/csv.rb in rio-0.3.3

- old
+ new

@@ -57,10 +57,15 @@ def columns(*ranges,&block) @cnames = nil cx['col_args'] = ranges.flatten cxx('columns',true,&block) end + def nocolumns(*ranges,&block) + @cnames = nil + cx['nocol_args'] = ranges.flatten + cxx('columns',true,&block) + end def columns?() cxx?('columns') end end end @@ -107,15 +112,39 @@ end private def trim(fields) - return fields if cx['col_args'].nil? or cx['col_args'].empty? + ycols = cx['col_args'] + ncols = cx['nocol_args'] + return [] if ncols and ncols.empty? + if ycols.nil? and ncols.nil? + return fields + end + ncols = [] if ncols.nil? + ycols = [(0...fields.size)] if ycols.nil? or ycols.empty? + cols = [] + fields.each_index { |i| + yes = nil + no = nil + ycols.each { |yc| + if yc === i + yes = true + break + end + } + ncols.each { |nc| + if nc === i + no = true + break + end + } + + cols << i if yes and !no + } tfields = [] - fields.each_index do |i| - cx['col_args'].each do |rng| - tfields << fields[i] if rng === i - end + cols.each do |i| + tfields << fields[i] end tfields end def _l2a(line,fs,rs) ::CSV.parse_line(line,fs,rs)