Class: FastestCSV

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/fastest_csv.rb,
lib/fastest-csv/version.rb

Overview

Fast CSV parser using native code

Constant Summary collapse

VERSION =
"0.0.4"

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(io) ⇒ FastestCSV

Create new FastestCSV wrapping the specified IO object



70
71
72
# File 'lib/fastest_csv.rb', line 70

def initialize(io)
  @io = io
end

Class Method Details

.foreach(path, &block) ⇒ Object

Pass each line of the specified path as array to the provided block



20
21
22
23
24
# File 'lib/fastest_csv.rb', line 20

def self.foreach(path, &block)
  open(path) do |reader|
    reader.each(&block)
  end
end

.open(path, mode = "rb") ⇒ Object

Opens a csv file. Pass a FastestCSV instance to the provided block, or return it when no block is provided



28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/fastest_csv.rb', line 28

def self.open(path, mode = "rb")
  csv = new(File.open(path, mode))
  if block_given?
    begin
      yield csv
    ensure
      csv.close
    end
  else
    csv
  end
end

.parse(data, &block) ⇒ Object

Read all lines from the specified String into an array of arrays



52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/fastest_csv.rb', line 52

def self.parse(data, &block)
  csv = new(StringIO.new(data))
  if block.nil?
    begin
      csv.read
    ensure
      csv.close
    end
  else
    csv.each(&block)
  end
end

.parse_line(line) ⇒ Object



65
66
67
# File 'lib/fastest_csv.rb', line 65

def self.parse_line(line)
  CsvParser.parse_line(line)
end

.read(path) ⇒ Object

Read all lines from the specified path into an array of arrays



42
43
44
# File 'lib/fastest_csv.rb', line 42

def self.read(path)
  open(path, "rb") { |csv| csv.read }
end

.readlines(path) ⇒ Object

Alias for FastestCSV.read



47
48
49
# File 'lib/fastest_csv.rb', line 47

def self.readlines(path)
  read(path)
end

Instance Method Details

#closeObject

Close the wrapped IO



110
111
112
# File 'lib/fastest_csv.rb', line 110

def close
  @io.close
end

#closed?Boolean

Returns:

  • (Boolean)


114
115
116
# File 'lib/fastest_csv.rb', line 114

def closed?
  @io.closed?
end

#eachObject

Read from the wrapped IO passing each line as array to the specified block



75
76
77
78
79
80
81
82
83
# File 'lib/fastest_csv.rb', line 75

def each
  if block_given?
    while row = shift
      yield row
    end
  else
    to_enum # return enumerator
  end
end

#readObject Also known as: readlines

Read all remaining lines from the wrapped IO into an array of arrays



86
87
88
89
90
# File 'lib/fastest_csv.rb', line 86

def read
  table = Array.new
  each {|row| table << row}
  table
end

#rewindObject

Rewind the underlying IO object and reset line counter



94
95
96
# File 'lib/fastest_csv.rb', line 94

def rewind
  @io.rewind
end

#shiftObject Also known as: gets, readline

Read next line from the wrapped IO and return as array or nil at EOF



99
100
101
102
103
104
105
# File 'lib/fastest_csv.rb', line 99

def shift
  if line = @io.gets
    CsvParser.parse_line(line)
  else
    nil
  end
end