/*
 *  call-seq:
 *     SortField.new(field, options = {}) -> sort_field
 *
 *  Create a new SortField which can be used to sort the result-set by the
 *  value in field +field+.
 *
 *  === Options
 *
 *  :type::         Default: +:auto+. Specifies how a field should be sorted.
 *                  Choose from one of; +:auto+, +:integer+, +:float+,
 *                  +:string+, +:byte+, +:doc_id+ or +:score+. +:auto+ will
 *                  check the datatype of the field by trying to parse it into
 *                  either a number or a float before settling on a string
 *                  sort. String sort is locale dependent and works for
 *                  multibyte character sets like UTF-8 if you have your
 *                  locale set correctly.
 *  :reverse        Default: false. Set to true if you want to reverse the
 *                  sort.
 */
static VALUE
frt_sf_init(int argc, VALUE *argv, VALUE self)
{
    SortField *sf;
    VALUE rfield, roptions;
    VALUE rval;
    int type = SORT_TYPE_AUTO;
    int is_reverse = false;

    if (rb_scan_args(argc, argv, "11", &rfield, &roptions) == 2) {
        if (Qnil != (rval = rb_hash_aref(roptions, sym_type))) {
            type = get_sort_type(rval);
        }
        if (Qnil != (rval = rb_hash_aref(roptions, sym_reverse))) {
            is_reverse = RTEST(rval);
        }
        if (Qnil != (rval = rb_hash_aref(roptions, sym_comparator))) {
            rb_raise(rb_eArgError, "Unsupported argument ':comparator'");
        }
    }
    if (NIL_P(rfield)) rb_raise(rb_eArgError, "must pass a valid field name");
    rfield = rb_obj_as_string(rfield);

    sf = sort_field_new(RSTRING(rfield)->ptr, type, is_reverse);
    if (sf->field == NULL && RSTRING(rfield)->ptr != NULL) {
        sf->field = estrdup(RSTRING(rfield)->ptr);
    }

    Frt_Wrap_Struct(self, NULL, &frt_sf_free, sf);
    object_add(sf, self);
    return self;
}