Class: ZSV::Parser
- Inherits:
-
Object
- Object
- ZSV::Parser
- Includes:
- Enumerable
- Defined in:
- lib/zsv.rb,
ext/zsv/zsv_ext.c
Overview
Parser class methods for convenience
Instance Method Summary collapse
-
#close ⇒ nil
Closes the parser and releases resources.
-
#closed? ⇒ Boolean
Returns true if the parser is closed.
-
#each ⇒ Object
(also: #each_row)
Iterates over all rows.
-
#headers ⇒ Array?
Returns the headers if header mode is enabled.
-
#ZSV::Parser.new(io, **options) ⇒ Object
constructor
Creates a new parser for the given IO object.
-
#read ⇒ Array<Array, Hash>
(also: #to_a)
Read all rows and return as an array.
-
#rewind ⇒ nil
Rewinds the parser to the beginning.
-
#shift ⇒ Array, ...
Reads and returns the next row.
Constructor Details
#ZSV::Parser.new(io, **options) ⇒ Object
Creates a new parser for the given IO object.
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'ext/zsv/zsv_ext.c', line 79
static VALUE rb_zsv_parser_initialize(int argc, VALUE *argv, VALUE self)
{
VALUE io, opts;
rb_scan_args(argc, argv, "11", &io, &opts);
zsv_ruby_parser_t *parser;
if (TYPE(io) == T_STRING) {
const char *str = StringValueCStr(io);
/* Detect if it's a file path or CSV content */
/* If it contains newlines or commas and doesn't exist as a file, treat as CSV data */
if (strchr(str, '\n') != NULL || strchr(str, ',') != NULL) {
/* Looks like CSV content, parse as string */
parser = zsv_parser_new_from_string(io, opts);
} else {
/* Try as file path */
parser = zsv_parser_new_from_path(str, opts);
}
} else {
/* IO object */
parser = zsv_parser_new_from_io(io, opts);
}
DATA_PTR(self) = parser;
return self;
}
|
Instance Method Details
#close ⇒ nil
Closes the parser and releases resources.
154 155 156 157 158 159 |
# File 'ext/zsv/zsv_ext.c', line 154
static VALUE rb_zsv_parser_close(VALUE self)
{
zsv_ruby_parser_t *parser = get_parser(self);
zsv_parser_close(parser);
return Qnil;
}
|
#closed? ⇒ Boolean
Returns true if the parser is closed.
179 180 181 182 183 |
# File 'ext/zsv/zsv_ext.c', line 179
static VALUE rb_zsv_parser_closed_p(VALUE self)
{
zsv_ruby_parser_t *parser = get_parser(self);
return zsv_parser_closed(parser) ? Qtrue : Qfalse;
}
|
#each {|row| ... } ⇒ nil #each ⇒ Object Also known as: each_row
Iterates over all rows.
126 127 128 129 130 131 132 133 |
# File 'ext/zsv/zsv_ext.c', line 126
static VALUE rb_zsv_parser_each(VALUE self)
{
RETURN_ENUMERATOR(self, 0, 0);
zsv_ruby_parser_t *parser = get_parser(self);
zsv_parser_each(parser);
return Qnil;
}
|
#headers ⇒ Array?
Returns the headers if header mode is enabled.
167 168 169 170 171 |
# File 'ext/zsv/zsv_ext.c', line 167
static VALUE rb_zsv_parser_headers_get(VALUE self)
{
zsv_ruby_parser_t *parser = get_parser(self);
return zsv_parser_headers(parser);
}
|
#read ⇒ Array<Array, Hash> Also known as: to_a
Read all rows and return as an array
72 73 74 75 76 |
# File 'lib/zsv.rb', line 72 def read rows = [] each { |row| rows << row } rows end |
#rewind ⇒ nil
Rewinds the parser to the beginning.
141 142 143 144 145 146 |
# File 'ext/zsv/zsv_ext.c', line 141
static VALUE rb_zsv_parser_rewind(VALUE self)
{
zsv_ruby_parser_t *parser = get_parser(self);
zsv_parser_rewind(parser);
return Qnil;
}
|
#shift ⇒ Array, ...
Reads and returns the next row. Returns nil at EOF.
113 114 115 116 117 |
# File 'ext/zsv/zsv_ext.c', line 113
static VALUE rb_zsv_parser_shift(VALUE self)
{
zsv_ruby_parser_t *parser = get_parser(self);
return zsv_parser_shift(parser);
}
|