/*
 *  call-seq:
 *     SpanNearQuery.new(options = {}) -> query
 *
 *  Create a new SpanNearQuery. You can add an array of clauses with the
 *  +:clause+ parameter or you can add clauses individually using the
 *  SpanNearQuery#add method.
 *
 *    query = SpanNearQuery.new(:clauses => [spanq1, spanq2, spanq3])
 *    # is equivalent to
 *    query = SpanNearQuery.new()
 *    query << spanq1 << spanq2 << spanq3
 *
 *  You have two other options which you can set.
 *
 *  :slop::     Default: 0. Works exactly like a PhraseQuery slop. It is the
 *              amount of slop allowed in the match (the term edit distance
 *              allowed in the match).
 *  :in_order:: Default: false. Specifies whether or not the matches have to
 *              occur in the order they were added to the query. When slop is
 *              set to 0, this parameter will make no difference.
 */
static VALUE
frt_spannq_init(int argc, VALUE *argv, VALUE self)
{
    Query *q;
    VALUE roptions;
    int slop = 0;
    bool in_order = false;

    if (rb_scan_args(argc, argv, "01", &roptions) > 0) {
        VALUE v;
        if (Qnil != (v = rb_hash_aref(roptions, sym_slop))) {
            slop = FIX2INT(v);
        }
        if (Qnil != (v = rb_hash_aref(roptions, sym_in_order))) {
            in_order = RTEST(v);
        }
    }
    q = spannq_new(slop, in_order);
    if (argc > 0) {
        VALUE v;
        if (Qnil != (v = rb_hash_aref(roptions, sym_clauses))) {
            int i;
            Query *clause;
            Check_Type(v, T_ARRAY);
            for (i = 0; i < RARRAY(v)->len; i++) {
                Data_Get_Struct(RARRAY(v)->ptr[i], Query, clause);
                spannq_add_clause(q, clause);
            }
        }
    }

    Frt_Wrap_Struct(self, &frt_spannq_mark, &frt_q_free, q);
    object_add(q, self);
    return self;
}