Class: ZSV::Parser

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/zsv.rb,
ext/zsv/zsv_ext.c

Overview

Parser class methods for convenience

Instance Method Summary collapse

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

#closenil

Closes the parser and releases resources.

Returns:

  • (nil)


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.

Returns:

  • (Boolean)


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 #eachObject Also known as: each_row

Iterates over all rows.

Overloads:

  • #each {|row| ... } ⇒ nil

    Yields:

    • (row)

    Returns:

    • (nil)


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;
}

#headersArray?

Returns the headers if header mode is enabled.

Returns:

  • (Array, nil)


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);
}

#readArray<Array, Hash> Also known as: to_a

Read all rows and return as an array

Returns:

  • (Array<Array, Hash>)

    All rows



72
73
74
75
76
# File 'lib/zsv.rb', line 72

def read
  rows = []
  each { |row| rows << row }
  rows
end

#rewindnil

Rewinds the parser to the beginning.

Returns:

  • (nil)


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;
}

#shiftArray, ...

Reads and returns the next row. Returns nil at EOF.

Returns:

  • (Array, Hash, nil)


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);
}