Class: ZOOM::ResultSet

Inherits:
Object
  • Object
show all
Defined in:
ext/rbzoomresultset.c

Instance Method Summary collapse

Instance Method Details

#[](key) ⇒ Object

key: either an integer, a range or an interval of 2 integers.

Retrieves one or many records from the result set, according to the given key.

# Gets the first record. rset # Gets the first, second and third records. rset # Gets three records starting from the second one. rset[2, 3]

Returns: one or many references to ZOOM::Record objects.



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
# File 'ext/rbzoomresultset.c', line 122

static VALUE
rbz_resultset_index (int argc, VALUE *argv, VALUE self)
{
    ZOOM_record *records;
    ZOOM_record record;
    VALUE ary;
    size_t begin;
    size_t count;
    size_t i;
    
    if (argc == 1) {
        VALUE arg = argv [0];

        if (TYPE (arg) == T_FIXNUM || TYPE (arg) == T_BIGNUM) {
            record = ZOOM_resultset_record (rbz_resultset_get (self),
                                            NUM2LONG (arg));
            return record != NULL
                ? rbz_record_make (ZOOM_record_clone (record))
                : Qnil;
        }
       
        if (CLASS_OF (arg) == rb_cRange) {
            begin = NUM2LONG (rb_funcall (arg, rb_intern ("begin"), 0));
            count = NUM2LONG (rb_funcall (arg, rb_intern ("end"), 0));
            count -= begin;
        }
        else
            rb_raise (rb_eArgError, 
                      "Invalid argument of type %s (not Numeric or Range)",
                      rb_class2name (CLASS_OF (arg)));
    }
    else {
        VALUE rb_begin;
        VALUE rb_count;
        
        rb_scan_args (argc, argv, "2", &rb_begin, &rb_count);
        
        begin = NUM2LONG (rb_begin);
        count = NUM2LONG (rb_count);
    }
        
    ary = rb_ary_new ();
    if (count == 0)
        return ary;

    /* Allocate array */
    records = ALLOC_N (ZOOM_record, count);

    /* Download records in batches */
    ZOOM_resultset_records (rbz_resultset_get (self), records, begin, count);

    /* Test the first record in the set.  If null, then fall back.  If valid, 
     * generate the ruby array.
     */

    if (records[0]!=NULL) {
       for (i = 0; i < count; i++)

         /* We don't want any null records -- if there is on in the resultset, 
          * ignore it.
          */

         if (records[i]!=NULL)
            rb_ary_push (ary, rbz_record_make (ZOOM_record_clone (records [i])));
    } else {
      /* This is our fallback function
       * It exists for those anomalies where the server 
       * will not respect the batch request and will return just 
       * a null array (per change request 36 where Laurent Sansonetti notes
       *    Retrieves the record one by one using ZOOM_resultset_record instead
       *    of getting them all in once with ZOOM_resultset_records (for a strange
       *    reason sometimes the resultset was not empty but ZOOM_resultset_records
       *    used to return empty records).
       */

      for (i = 0; i < count; i++) {
        record = ZOOM_resultset_record (rbz_resultset_get (self),
                                        begin + i);
        /* Ignore null records */
        if (record != NULL)
            rb_ary_push (ary, rbz_record_make (ZOOM_record_clone (record)));
      }
    }

    return ary;
}

#each_record {|record| ... } ⇒ Object

Parses the records inside the result set and call the given block for each record, passing a reference to a ZOOM::Record object as parameter.

Returns: self.

Yields:

  • (record)


234
235
236
237
238
239
# File 'ext/rbzoomresultset.c', line 234

static VALUE
rbz_resultset_each_record (VALUE self)
{
    rb_ary_each (rbz_resultset_records (self));
    return self;
}

#get_option(key) ⇒ Object

key: the name of the option, as a string.

Gets the value of a result set’s option.

Returns: the value of the given option, as a string, integer or boolean.



84
85
86
87
88
89
90
91
92
93
# File 'ext/rbzoomresultset.c', line 84

static VALUE
rbz_resultset_get_option (VALUE self, VALUE key)
{
    const char *value;
 
    value = ZOOM_resultset_option_get (rbz_resultset_get (self),
                                       RVAL2CSTR (key));

    return zoom_option_value_to_ruby_value (value);
}

#recordsObject

Lists the records inside the result set.

Returns: an array of ZOOM::Record objects.



214
215
216
217
218
219
220
221
222
223
# File 'ext/rbzoomresultset.c', line 214

static VALUE
rbz_resultset_records (VALUE self)
{
    VALUE argv [2];

    argv [0] = INT2FIX (0);
    argv [1] = rbz_resultset_size (self);
    
    return rbz_resultset_index (2, argv, self);
}

#set_option(key, value) ⇒ Object

key: the name of the option, as a string.

value: the value of this option (as a string, integer or boolean).

Sets an option on the result set.

Returns: self.



64
65
66
67
68
69
70
71
72
# File 'ext/rbzoomresultset.c', line 64

static VALUE
rbz_resultset_set_option (VALUE self, VALUE key, VALUE val)
{
    ZOOM_resultset_option_set (rbz_resultset_get (self),
                               RVAL2CSTR (key),
                               RVAL2CSTR (rb_obj_as_string (val)));
    
    return self;
}

#sizeObject Also known as: length

Returns: the number of hits.



98
99
100
101
102
# File 'ext/rbzoomresultset.c', line 98

static VALUE
rbz_resultset_size (VALUE self)
{
    return INT2NUM (ZOOM_resultset_size (rbz_resultset_get (self)));
}