Skip to content

Commit

Permalink
Bind grn_obj_type_to_string()
Browse files Browse the repository at this point in the history
GitHub: #116
  • Loading branch information
Masafumi Yokoyama committed Mar 5, 2016
1 parent ec3375a commit adaaa92
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 1 deletion.
31 changes: 31 additions & 0 deletions ext/groonga/rb-grn-object.c
Original file line number Diff line number Diff line change
Expand Up @@ -1718,6 +1718,34 @@ rb_grn_object_key_accessor_p (VALUE self)
return CBOOL2RVAL(key_accessor_p);
}

/**
* Returns the object type string.
*
* @example Groonga::Database
* Groonga::Database.create(:path => "/path/to/db") do |database|
* p database.type_string # => "db"
* end
*
* @example Groonga::Hash
* users = Groonga::Hash.create(:name => "Users")
* p users.type_string # => "table:hash_key"
*
* @overload type_string
* @return [String] The object type string.
*
* @since 6.0.0
*/
static VALUE
rb_grn_object_type_to_string (VALUE self)
{
grn_obj *object;

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

return rb_str_new_cstr(grn_obj_type_to_string(object->header.type));
}

void
rb_grn_init_object (VALUE mGrn)
{
Expand Down Expand Up @@ -1767,4 +1795,7 @@ rb_grn_init_object (VALUE mGrn)
rb_grn_object_accessor_p, 0);
rb_define_method(rb_cGrnObject, "key_accessor?",
rb_grn_object_key_accessor_p, 0);

rb_define_method(rb_cGrnObject, "type_string",
rb_grn_object_type_to_string, 0);
}
6 changes: 6 additions & 0 deletions test/test-database.rb
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,12 @@ def test_reindex
terms.collect(&:_key).sort)
end

def test_type_string
Groonga::Database.create(:path => @database_path.to_s) do |database|
assert_equal("db", database.type_string)
end
end

class RemoveTest < self
setup :setup_database

Expand Down
7 changes: 6 additions & 1 deletion test/test-hash.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# -*- coding: utf-8 -*-
#
# Copyright (C) 2014 Masafumi Yokoyama <myokoym@gmail.com>
# Copyright (C) 2009-2014 Kouhei Sutou <kou@clear-code.com>
# Copyright (C) 2014-2016 Masafumi Yokoyama <yokoyama@clear-code.com>
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
Expand Down Expand Up @@ -443,4 +443,9 @@ def test_each_without_block
user_names = users.each.collect(&:key)
assert_equal(["Alice", "Bob", "Carl"], user_names)
end

def test_type_string
users = Groonga::Hash.create(:name => "Users")
assert_equal("table:hash_key", users.type_string)
end
end

0 comments on commit adaaa92

Please sign in to comment.