From adaaa92e361c058e34becd9fa52e6bce6b3a3b9a Mon Sep 17 00:00:00 2001 From: Masafumi Yokoyama Date: Sat, 5 Mar 2016 22:49:42 +0900 Subject: [PATCH] Bind grn_obj_type_to_string() GitHub: #116 --- ext/groonga/rb-grn-object.c | 31 +++++++++++++++++++++++++++++++ test/test-database.rb | 6 ++++++ test/test-hash.rb | 7 ++++++- 3 files changed, 43 insertions(+), 1 deletion(-) diff --git a/ext/groonga/rb-grn-object.c b/ext/groonga/rb-grn-object.c index 2f20c25c..8d4a5848 100644 --- a/ext/groonga/rb-grn-object.c +++ b/ext/groonga/rb-grn-object.c @@ -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) { @@ -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); } diff --git a/test/test-database.rb b/test/test-database.rb index 638c4d03..f0183087 100644 --- a/test/test-database.rb +++ b/test/test-database.rb @@ -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 diff --git a/test/test-hash.rb b/test/test-hash.rb index 3a4b27ec..04c7d323 100644 --- a/test/test-hash.rb +++ b/test/test-hash.rb @@ -1,7 +1,7 @@ # -*- coding: utf-8 -*- # -# Copyright (C) 2014 Masafumi Yokoyama # Copyright (C) 2009-2014 Kouhei Sutou +# Copyright (C) 2014-2016 Masafumi Yokoyama # # This library is free software; you can redistribute it and/or # modify it under the terms of the GNU Lesser General Public @@ -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