Class: FastestCSV
- Inherits:
-
Object
- Object
- FastestCSV
- Defined in:
- lib/fastest_csv.rb,
lib/fastest-csv/version.rb,
ext/csv_parser/parser.c
Constant Summary collapse
- VERSION =
"0.0.1"
Class Method Summary collapse
-
.foreach(path, &block) ⇒ Object
This method opens an accounting file and passes each record to the provided
block
. -
.open(path, mode = "rb") ⇒ Object
This method opens a csv file.
- .parse(data, &block) ⇒ Object
- .parse_line(str) ⇒ Object
- .read(path) ⇒ Object
- .readlines(path) ⇒ Object
Instance Method Summary collapse
- #close ⇒ Object
- #closed? ⇒ Boolean
- #each ⇒ Object
-
#initialize(io) ⇒ FastestCSV
constructor
A new instance of FastestCSV.
- #read ⇒ Object (also: #readlines)
- #shift ⇒ Object (also: #gets, #readline)
Constructor Details
#initialize(io) ⇒ FastestCSV
Returns a new instance of FastestCSV.
49 50 51 |
# File 'lib/fastest_csv.rb', line 49 def initialize(io) @io = io end |
Class Method Details
.foreach(path, &block) ⇒ Object
This method opens an accounting file and passes each record to the provided block
.
7 8 9 10 11 |
# File 'lib/fastest_csv.rb', line 7 def self.foreach(path, &block) open(path) do |reader| reader.each(&block) end end |
.open(path, mode = "rb") ⇒ Object
This method opens a csv file. It will pass a Reader object to the provided block, or return a Reader object when no block is provided.
15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/fastest_csv.rb', line 15 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
36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/fastest_csv.rb', line 36 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(str) ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'ext/csv_parser/parser.c', line 17
static VALUE parse_line(VALUE self, VALUE str)
{
if (NIL_P(str))
return Qnil;
const char *ptr = RSTRING_PTR(str);
int len = (int) RSTRING_LEN(str); /* cast to prevent warning in 64-bit OS */
if (len == 0)
return Qnil;
VALUE array = rb_ary_new2(DEF_ARRAY_LEN);
char value[len]; /* field value, no longer than line */
int state = 0;
int index = 0;
int i;
char c;
for (i = 0; i < len; i++)
{
c = ptr[i];
switch (c)
{
case ',':
if (state == 0) {
rb_ary_push(array, (index == 0 ? Qnil: rb_str_new(value, index)));
index = 0;
}
else if (state == 1) {
value[index++] = c;
}
else if (state == 2) {
rb_ary_push(array, rb_str_new(value, index));
index = 0;
state = 0; /* outside quoted */
}
break;
case '"':
if (state == 0) {
state = 1; /* in quoted */
}
else if (state == 1) {
state = 2; /* quote in quoted */
}
else if (state == 2) {
value[index++] = c; /* escaped quote */
state = 1; /* in quoted */
}
break;
case 13: /* \r */
case 10: /* \n */
if (state == 1) { /* quoted */
value[index++] = c;
}
else {
/* only do first line */
i = len;
}
/* else eat it ??? or return so far */
break;
default:
value[index++] = c;
}
}
if (state == 0) {
rb_ary_push(array, (index == 0 ? Qnil: rb_str_new(value, index)));
}
else if (state == 2) {
rb_ary_push(array, rb_str_new(value, index));
}
return array;
}
|
.read(path) ⇒ Object
28 29 30 |
# File 'lib/fastest_csv.rb', line 28 def self.read(path) open(path, "rb") { |csv| csv.read } end |
.readlines(path) ⇒ Object
32 33 34 |
# File 'lib/fastest_csv.rb', line 32 def self.readlines(path) read(path) end |
Instance Method Details
#close ⇒ Object
76 77 78 |
# File 'lib/fastest_csv.rb', line 76 def close @io.close end |
#closed? ⇒ Boolean
80 81 82 |
# File 'lib/fastest_csv.rb', line 80 def closed? @io.closed? end |
#each ⇒ Object
53 54 55 56 57 |
# File 'lib/fastest_csv.rb', line 53 def each while row = shift yield row end end |
#read ⇒ Object Also known as: readlines
59 60 61 62 63 |
# File 'lib/fastest_csv.rb', line 59 def read table = Array.new each {|row| table << row} table end |
#shift ⇒ Object Also known as: gets, readline
66 67 68 69 70 71 72 |
# File 'lib/fastest_csv.rb', line 66 def shift if line = @io.gets FastestCSV.parse_line(line) else nil end end |