Navigation Menu

Skip to content

Commit

Permalink
Add object type predicates
Browse files Browse the repository at this point in the history
  • Loading branch information
kou committed Apr 14, 2015
1 parent ff4085a commit 8681e69
Show file tree
Hide file tree
Showing 4 changed files with 173 additions and 0 deletions.
136 changes: 136 additions & 0 deletions ext/groonga/rb-grn-object.c
Expand Up @@ -1502,6 +1502,134 @@ rb_grn_object_builtin_p (VALUE self)
return CBOOL2RVAL(builtin);
}

/*
* Checks whether the object is table or not.
*
* @overload table?
* @return [Boolean] `true` if the object is table, `false` otherwise.
*
* @since 5.0.1
*/
static VALUE
rb_grn_object_table_p (VALUE self)
{
grn_ctx *context;
grn_obj *object;
grn_bool table_p = GRN_FALSE;

rb_grn_object_deconstruct(SELF(self), &object, &context,
NULL, NULL, NULL, NULL);

if (context && object) {
table_p = grn_obj_is_table(context, object);
}

return CBOOL2RVAL(table_p);
}

/*
* Checks whether the object is procedure or not.
*
* @overload procedure?
* @return [Boolean] `true` if the object is procedure, `false` otherwise.
*
* @since 5.0.1
*/
static VALUE
rb_grn_object_procedure_p (VALUE self)
{
grn_ctx *context;
grn_obj *object;
grn_bool procedure_p = GRN_FALSE;

rb_grn_object_deconstruct(SELF(self), &object, &context,
NULL, NULL, NULL, NULL);

if (context && object) {
procedure_p = grn_obj_is_proc(context, object);
}

return CBOOL2RVAL(procedure_p);
}

/*
* Checks whether the object is function procedure or not.
*
* @overload function_procedure?
* @return [Boolean] `true` if the object is function procedure,
* `false` otherwise.
*
* @since 5.0.1
*/
static VALUE
rb_grn_object_function_procedure_p (VALUE self)
{
grn_ctx *context;
grn_obj *object;
grn_bool function_procedure_p = GRN_FALSE;

rb_grn_object_deconstruct(SELF(self), &object, &context,
NULL, NULL, NULL, NULL);

if (context && object) {
function_procedure_p = grn_obj_is_function_proc(context, object);
}

return CBOOL2RVAL(function_procedure_p);
}

/*
* Checks whether the object is selector procedure or not.
*
* @overload selector_procedure?
* @return [Boolean] `true` if the object is selector procedure,
* `false` otherwise.
*
* @since 5.0.1
*/
static VALUE
rb_grn_object_selector_procedure_p (VALUE self)
{
grn_ctx *context;
grn_obj *object;
grn_bool selector_procedure_p = GRN_FALSE;

rb_grn_object_deconstruct(SELF(self), &object, &context,
NULL, NULL, NULL, NULL);

if (context && object) {
selector_procedure_p = grn_obj_is_selector_proc(context, object);
}

return CBOOL2RVAL(selector_procedure_p);
}

/*
* Checks whether the object is scorer procedure or not.
*
* @overload scorer_procedure?
* @return [Boolean] `true` if the object is scorer procedure,
* `false` otherwise.
*
* @since 5.0.1
*/
static VALUE
rb_grn_object_scorer_procedure_p (VALUE self)
{
grn_ctx *context;
grn_obj *object;
grn_bool scorer_procedure_p = GRN_FALSE;

rb_grn_object_deconstruct(SELF(self), &object, &context,
NULL, NULL, NULL, NULL);

if (context && object) {
scorer_procedure_p = grn_obj_is_scorer_proc(context, object);
}

return CBOOL2RVAL(scorer_procedure_p);
}

void
rb_grn_init_object (VALUE mGrn)
{
Expand Down Expand Up @@ -1537,4 +1665,12 @@ rb_grn_init_object (VALUE mGrn)
rb_define_method(rb_cGrnObject, "remove", rb_grn_object_remove, 0);

rb_define_method(rb_cGrnObject, "builtin?", rb_grn_object_builtin_p, 0);
rb_define_method(rb_cGrnObject, "table?", rb_grn_object_table_p, 0);
rb_define_method(rb_cGrnObject, "procedure?", rb_grn_object_procedure_p, 0);
rb_define_method(rb_cGrnObject, "function_procedure?",
rb_grn_object_function_procedure_p, 0);
rb_define_method(rb_cGrnObject, "selector_procedure?",
rb_grn_object_selector_procedure_p, 0);
rb_define_method(rb_cGrnObject, "scorer_procedure?",
rb_grn_object_scorer_procedure_p, 0);
}
6 changes: 6 additions & 0 deletions test/test-column.rb
Expand Up @@ -340,6 +340,12 @@ def test_builtin?
assert_not_predicate(@users_name_column, :builtin?)
end

def test_table?
assert do
not @users_name_column.table?
end
end

private
def assert_content_search(expected_records, term)
records = @bookmarks_index_content.search(term).records
Expand Down
24 changes: 24 additions & 0 deletions test/test-procedure.rb
Expand Up @@ -27,6 +27,30 @@ def test_builtins
:accept_nil => true)
end

def test_procedure?
assert do
Groonga["TokenBigram"].procedure?
end
end

def test_function_procedure?
assert do
Groonga["rand"].function_procedure?
end
end

def test_selector_procedure?
assert do
Groonga["between"].selector_procedure?
end
end

def test_scorer_procedure?
assert do
Groonga["scorer_tf_idf"].scorer_procedure?
end
end

private
def assert_equal_procedure(expected_name, id, options={})
procedure = Groonga::Context.default[id]
Expand Down
7 changes: 7 additions & 0 deletions test/test-table.rb
Expand Up @@ -630,6 +630,13 @@ def test_builtin?
assert_not_predicate(bookmarks, :builtin?)
end

def test_table?
bookmarks = Groonga::PatriciaTrie.create(:name => "Bookmarks")
assert do
bookmarks.table?
end
end

class OtherProcessTest < self
def test_create
by_other_process do
Expand Down

0 comments on commit 8681e69

Please sign in to comment.