Class: LightCsv

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/lightcsv.rb

Overview

CSV parser

Defined Under Namespace

Classes: InvalidFormat

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(src) ⇒ LightCsv

Returns a new instance of LightCsv.

Parameters:

  • src (String / IO)

    CSV source



70
71
72
73
74
75
76
77
78
79
80
# File 'lib/lightcsv.rb', line 70

def initialize(src)
  if src.kind_of? String
    @file = nil
    @ss = StringScanner.new(src)
  else
    @file = src
    @ss = StringScanner.new("")
  end
  @buf = ""
  @bufsize = 64*1024
end

Instance Attribute Details

#bufsizeObject

Returns the value of attribute bufsize.



81
82
83
# File 'lib/lightcsv.rb', line 81

def bufsize
  @bufsize
end

Class Method Details

.foreach(filename) {|row| ... } ⇒ void

This method returns an undefined value.

Parameters:

  • filename (String)

    Filename

Yields:

  • (row)

Yield Parameters:

  • row (Array<String>)

    One record



21
22
23
24
25
# File 'lib/lightcsv.rb', line 21

def self.foreach(filename, &block)
  self.open(filename) do |f|
    f.each(&block)
  end
end

.open(filename) {|csv| ... } ⇒ LightCsv, Object

Parameters:

  • filename (String)

    Filename

Yields:

  • (csv)

Yield Parameters:

Returns:

  • (LightCsv)

    if block is unspecified

  • (Object)

    block value if block is specified



55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/lightcsv.rb', line 55

def self.open(filename, &block)
  f = File.open(filename)
  csv = self.new(f)
  if block
    begin
      return block.call(csv)
    ensure
      csv.close
    end
  else
    return csv
  end
end

.parse(string) {|row| ... } ⇒ Array<Array<String>>?

Parameters:

  • string (String)

    CSV string

Yields:

  • (row)

Yield Parameters:

  • row (Array<String>)

    One record

Returns:

  • (Array<Array<String>>)

    if block is unspecified

  • (nil)

    if block is specified



40
41
42
43
44
45
46
47
48
# File 'lib/lightcsv.rb', line 40

def self.parse(string, &block)
  unless block
    return self.new(string).entries
  end
  self.new(string).each do |row|
    block.call row
  end
  return nil
end

.readlines(filename) ⇒ Array<Array<String>>

Returns All records.

Parameters:

  • filename (String)

    Filename

Returns:

  • (Array<Array<String>>)

    All records.



29
30
31
32
33
# File 'lib/lightcsv.rb', line 29

def self.readlines(filename)
  self.open(filename) do |f|
    return f.entries
  end
end

Instance Method Details

#closevoid

This method returns an undefined value.

close file



85
86
87
# File 'lib/lightcsv.rb', line 85

def close
  @file.close if @file
end

#each {|row| ... } ⇒ Object

iterator

Yields:

  • (row)

Yield Parameters:

  • row (Array<String>)

    One record



120
121
122
123
124
# File 'lib/lightcsv.rb', line 120

def each
  while row = shift
    yield row
  end
end

#readlinesArray<Array<String>>

Array of record

Returns:

  • (Array<Array<String>>)

    records



128
129
130
# File 'lib/lightcsv.rb', line 128

def readlines
  return entries
end

#shiftArray<String>?

return one record.

Returns:

  • (Array<String>)

    one record. empty array for empty line.

  • (nil)

    if end of data is reached.



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/lightcsv.rb', line 92

def shift
  return nil if @ss.eos? and ! read_next_data
  cols = []
  while true
    if @ss.eos? and ! read_next_data
      cols << ""
      break
    end
    if @ss.scan(/\"/)
      until @ss.scan(/((?:\"\"|[^\"])*)\"(,|\r\n|\n|\r|\z)/)
        read_next_data or raise InvalidFormat, @ss.rest[0,10]
      end
      cols << @ss[1].gsub(/\"\"/, '"')
    else
      unless @ss.scan(/([^\",\r\n]*)(,|\r\n|\n|\r|\z)/)
        raise InvalidFormat, @ss.rest[0,10]
      end
      cols << @ss[1]
    end
    break unless @ss[2] == ','
  end
  cols.clear if cols.size == 1 and cols.first.empty?
  cols
end