# class ZOOM::ResultSet

### Public Instance Methods

[](key) click to toggle source

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[0]
    # Gets the first, second and third records.
    rset[1..3]
    # Gets three records starting from the second one.
    rset[2, 3]

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

    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| ... } click to toggle source

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.

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

get_option(key) click to toggle source

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.

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

length()
Alias for: size
records() click to toggle source

Lists the records inside the result set.

Returns: an array of ZOOM::Record objects.

    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) click to toggle source

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.

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

        return self;
    }

size() click to toggle source

Returns: the number of hits.

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

Also aliased as: length