fasterer-csv 2.0.0 → 2.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/VERSION +1 -1
 - data/fasterer-csv.gemspec +1 -1
 - data/lib/fasterer_csv.rb +21 -16
 - data/spec/fasterer_csv_spec.rb +14 -3
 - metadata +1 -1
 
    
        data/VERSION
    CHANGED
    
    | 
         @@ -1 +1 @@ 
     | 
|
| 
       1 
     | 
    
         
            -
            2. 
     | 
| 
      
 1 
     | 
    
         
            +
            2.1.0
         
     | 
    
        data/fasterer-csv.gemspec
    CHANGED
    
    
    
        data/lib/fasterer_csv.rb
    CHANGED
    
    | 
         @@ -11,11 +11,12 @@ module FastererCSV 
     | 
|
| 
       11 
11 
     | 
    
         
             
                  end
         
     | 
| 
       12 
12 
     | 
    
         
             
                end
         
     | 
| 
       13 
13 
     | 
    
         | 
| 
       14 
     | 
    
         
            -
                attr_reader :headers, :lines
         
     | 
| 
      
 14 
     | 
    
         
            +
                attr_reader :headers, :lines, :line_block
         
     | 
| 
       15 
15 
     | 
    
         | 
| 
       16 
     | 
    
         
            -
                def initialize(headers, fail_on_malformed_columns = true)
         
     | 
| 
      
 16 
     | 
    
         
            +
                def initialize(headers, fail_on_malformed_columns = true, &line_block)
         
     | 
| 
       17 
17 
     | 
    
         
             
                  @headers = Table.format_headers(headers)
         
     | 
| 
       18 
18 
     | 
    
         
             
                  @fail_on_malformed_columns = fail_on_malformed_columns
         
     | 
| 
      
 19 
     | 
    
         
            +
                  @line_block = line_block
         
     | 
| 
       19 
20 
     | 
    
         
             
                  @lines = 0
         
     | 
| 
       20 
21 
     | 
    
         
             
                  @indexes = {}
         
     | 
| 
       21 
22 
     | 
    
         
             
                end
         
     | 
| 
         @@ -37,8 +38,13 @@ module FastererCSV 
     | 
|
| 
       37 
38 
     | 
    
         
             
                    puts error
         
     | 
| 
       38 
39 
     | 
    
         
             
                    raise error if @fail_on_malformed_columns
         
     | 
| 
       39 
40 
     | 
    
         
             
                  end
         
     | 
| 
       40 
     | 
    
         
            -
                   
     | 
| 
      
 41 
     | 
    
         
            +
                  if line_block
         
     | 
| 
      
 42 
     | 
    
         
            +
                    line_block.call(row)
         
     | 
| 
      
 43 
     | 
    
         
            +
                  else
         
     | 
| 
      
 44 
     | 
    
         
            +
                    super(row)
         
     | 
| 
      
 45 
     | 
    
         
            +
                  end
         
     | 
| 
       41 
46 
     | 
    
         
             
                end
         
     | 
| 
      
 47 
     | 
    
         
            +
                alias_method :push, :<<
         
     | 
| 
       42 
48 
     | 
    
         | 
| 
       43 
49 
     | 
    
         
             
                def merge(*tables)
         
     | 
| 
       44 
50 
     | 
    
         | 
| 
         @@ -122,7 +128,7 @@ module FastererCSV 
     | 
|
| 
       122 
128 
     | 
    
         
             
                end
         
     | 
| 
       123 
129 
     | 
    
         | 
| 
       124 
130 
     | 
    
         
             
                def headers
         
     | 
| 
       125 
     | 
    
         
            -
                  @table.headers
         
     | 
| 
      
 131 
     | 
    
         
            +
                  @headers ||= @table.headers.dup
         
     | 
| 
       126 
132 
     | 
    
         
             
                end
         
     | 
| 
       127 
133 
     | 
    
         | 
| 
       128 
134 
     | 
    
         
             
                attr_reader :line
         
     | 
| 
         @@ -293,21 +299,25 @@ module FastererCSV 
     | 
|
| 
       293 
299 
     | 
    
         
             
                end
         
     | 
| 
       294 
300 
     | 
    
         | 
| 
       295 
301 
     | 
    
         
             
                def read(file, quot = '~', sep = ',', fail_on_malformed = true, column = NoConversion.new, &block)
         
     | 
| 
       296 
     | 
    
         
            -
                   
     | 
| 
      
 302 
     | 
    
         
            +
                  File.open(file, 'r') do |io|
         
     | 
| 
      
 303 
     | 
    
         
            +
                    parse(io, quot, sep, fail_on_malformed, column, &block)
         
     | 
| 
      
 304 
     | 
    
         
            +
                  end
         
     | 
| 
       297 
305 
     | 
    
         
             
                end
         
     | 
| 
       298 
306 
     | 
    
         | 
| 
       299 
307 
     | 
    
         
             
                def convread(file, quot = '~', sep = ',', fail_on_malformed = true, column = NumericConversion.new, &block)
         
     | 
| 
       300 
     | 
    
         
            -
                   
     | 
| 
      
 308 
     | 
    
         
            +
                  File.open(file, 'r') do |io|
         
     | 
| 
      
 309 
     | 
    
         
            +
                    parse(io, quot, sep, fail_on_malformed, column, &block)
         
     | 
| 
      
 310 
     | 
    
         
            +
                  end
         
     | 
| 
       301 
311 
     | 
    
         
             
                end
         
     | 
| 
       302 
312 
     | 
    
         | 
| 
       303 
313 
     | 
    
         
             
                def parse_headers(data, quot = '~', sep = ',', fail_on_malformed = true, column = NoConversion.new, &block)
         
     | 
| 
       304 
314 
     | 
    
         
             
                  parse(data, quot, sep, fail_on_malformed, column, &block).headers
         
     | 
| 
       305 
315 
     | 
    
         
             
                end
         
     | 
| 
       306 
316 
     | 
    
         | 
| 
       307 
     | 
    
         
            -
                def parse( 
     | 
| 
      
 317 
     | 
    
         
            +
                def parse(io, quot = '~', sep = ',', fail_on_malformed = true, column = NoConversion.new, &block)
         
     | 
| 
       308 
318 
     | 
    
         
             
                  q, s, row, inquot, clean, maybe, table, field, endline = quot.ord, sep.ord, [], false, true, false, nil, true, false
         
     | 
| 
       309 
319 
     | 
    
         | 
| 
       310 
     | 
    
         
            -
                   
     | 
| 
      
 320 
     | 
    
         
            +
                  io.each_byte do |c|
         
     | 
| 
       311 
321 
     | 
    
         
             
                    next if c == ?\r.ord
         
     | 
| 
       312 
322 
     | 
    
         | 
| 
       313 
323 
     | 
    
         
             
                    if maybe && c == s
         
     | 
| 
         @@ -317,7 +327,7 @@ module FastererCSV 
     | 
|
| 
       317 
327 
     | 
    
         
             
                    elsif maybe && c == ?\n.ord && table.nil?
         
     | 
| 
       318 
328 
     | 
    
         
             
                      row << column.convert(true) unless (column.empty? && endline)
         
     | 
| 
       319 
329 
     | 
    
         
             
                      column.clear
         
     | 
| 
       320 
     | 
    
         
            -
                      table = Table.new(row, fail_on_malformed) unless row.empty?
         
     | 
| 
      
 330 
     | 
    
         
            +
                      table = Table.new(row, fail_on_malformed, &block) unless row.empty?
         
     | 
| 
       321 
331 
     | 
    
         
             
                      row, clean, inquot, maybe, field, endline = [], true, false, false, false, true
         
     | 
| 
       322 
332 
     | 
    
         
             
                    elsif maybe && c == ?\n.ord
         
     | 
| 
       323 
333 
     | 
    
         
             
                      row << column.convert(true) unless (column.empty? && endline)
         
     | 
| 
         @@ -343,7 +353,7 @@ module FastererCSV 
     | 
|
| 
       343 
353 
     | 
    
         
             
                      row << column.convert(false) unless column.empty? && endline
         
     | 
| 
       344 
354 
     | 
    
         | 
| 
       345 
355 
     | 
    
         
             
                      column.clear
         
     | 
| 
       346 
     | 
    
         
            -
                      table = Table.new(row, fail_on_malformed) unless row.empty?
         
     | 
| 
      
 356 
     | 
    
         
            +
                      table = Table.new(row, fail_on_malformed, &block) unless row.empty?
         
     | 
| 
       347 
357 
     | 
    
         
             
                      row, clean, inquot, field, endline = [], true, false, false, true
         
     | 
| 
       348 
358 
     | 
    
         
             
                    elsif c == ?\n.ord
         
     | 
| 
       349 
359 
     | 
    
         | 
| 
         @@ -363,16 +373,12 @@ module FastererCSV 
     | 
|
| 
       363 
373 
     | 
    
         
             
                    if table
         
     | 
| 
       364 
374 
     | 
    
         
             
                      table << row unless row.empty?
         
     | 
| 
       365 
375 
     | 
    
         
             
                    else
         
     | 
| 
       366 
     | 
    
         
            -
                      table = Table.new(row, fail_on_malformed) unless row.empty?
         
     | 
| 
      
 376 
     | 
    
         
            +
                      table = Table.new(row, fail_on_malformed, &block) unless row.empty?
         
     | 
| 
       367 
377 
     | 
    
         
             
                    end
         
     | 
| 
       368 
378 
     | 
    
         
             
                  elsif field
         
     | 
| 
       369 
379 
     | 
    
         
             
                    row << column.convert(maybe)
         
     | 
| 
       370 
380 
     | 
    
         
             
                  end
         
     | 
| 
       371 
381 
     | 
    
         | 
| 
       372 
     | 
    
         
            -
                  table.each do |line|
         
     | 
| 
       373 
     | 
    
         
            -
                    yield(line)
         
     | 
| 
       374 
     | 
    
         
            -
                  end if table && block_given?
         
     | 
| 
       375 
     | 
    
         
            -
             
     | 
| 
       376 
382 
     | 
    
         
             
                  table
         
     | 
| 
       377 
383 
     | 
    
         
             
                end
         
     | 
| 
       378 
384 
     | 
    
         | 
| 
         @@ -422,4 +428,3 @@ module FastererCSV 
     | 
|
| 
       422 
428 
     | 
    
         | 
| 
       423 
429 
     | 
    
         
             
              end
         
     | 
| 
       424 
430 
     | 
    
         
             
            end
         
     | 
| 
       425 
     | 
    
         
            -
             
     | 
    
        data/spec/fasterer_csv_spec.rb
    CHANGED
    
    | 
         @@ -105,11 +105,11 @@ a,b,c,d,e,f,g,h,i,j,k,l,m, 
     | 
|
| 
       105 
105 
     | 
    
         
             
                describe "parse" do
         
     | 
| 
       106 
106 
     | 
    
         
             
                  it "works" do
         
     | 
| 
       107 
107 
     | 
    
         
             
                    table = FastererCSV.parse(@data)
         
     | 
| 
       108 
     | 
    
         
            -
                    table.headers.should == [:a, :b, :c 
     | 
| 
      
 108 
     | 
    
         
            +
                    table.headers.should == [:a, :b, :c,:d,:e,:f,:g,:h,:i,:j,:k,:l,:m,:_]
         
     | 
| 
       109 
109 
     | 
    
         
             
                    table.lines.should == 2
         
     | 
| 
       110 
110 
     | 
    
         | 
| 
       111 
     | 
    
         
            -
                    table[0].should == [nil, nil, "1", "1.1", "-1", "-1.1", "1.1.1", "1", "a", "a", "a~a", "a\n~a", 
     | 
| 
       112 
     | 
    
         
            -
                    table[1].should == ["0", "1", "2", 
     | 
| 
      
 111 
     | 
    
         
            +
                    table[0].should == [nil, nil, "1", "1.1", "-1", "-1.1", "1.1.1", "1", "a", "a", "a~a", "a\n~a", ",", nil]
         
     | 
| 
      
 112 
     | 
    
         
            +
                    table[1].should == ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "14"]
         
     | 
| 
       113 
113 
     | 
    
         | 
| 
       114 
114 
     | 
    
         
             
                    row = table[1]
         
     | 
| 
       115 
115 
     | 
    
         
             
                    row.pull(:a, nil, 'd').should == ["0","14","3"]
         
     | 
| 
         @@ -117,6 +117,17 @@ a,b,c,d,e,f,g,h,i,j,k,l,m, 
     | 
|
| 
       117 
117 
     | 
    
         
             
                    row["b"].should == "1"
         
     | 
| 
       118 
118 
     | 
    
         | 
| 
       119 
119 
     | 
    
         
             
                  end
         
     | 
| 
      
 120 
     | 
    
         
            +
             
     | 
| 
      
 121 
     | 
    
         
            +
                  it "allows mutation of rows" do
         
     | 
| 
      
 122 
     | 
    
         
            +
                    each_called = false
         
     | 
| 
      
 123 
     | 
    
         
            +
                    table = FastererCSV.parse(@data).each do |row|
         
     | 
| 
      
 124 
     | 
    
         
            +
                      each_called = true
         
     | 
| 
      
 125 
     | 
    
         
            +
                      row[:foo] = "bar"
         
     | 
| 
      
 126 
     | 
    
         
            +
                      row.headers.should == [:a, :b, :c,:d,:e,:f,:g,:h,:i,:j,:k,:l,:m,:_, :foo]
         
     | 
| 
      
 127 
     | 
    
         
            +
                    end
         
     | 
| 
      
 128 
     | 
    
         
            +
                    each_called.should be_true
         
     | 
| 
      
 129 
     | 
    
         
            +
                    table.headers.should == [:a, :b, :c,:d,:e,:f,:g,:h,:i,:j,:k,:l,:m,:_]
         
     | 
| 
      
 130 
     | 
    
         
            +
                  end
         
     | 
| 
       120 
131 
     | 
    
         
             
                end
         
     | 
| 
       121 
132 
     | 
    
         | 
| 
       122 
133 
     | 
    
         
             
                describe "read" do
         
     |