Skip to content

Commit

Permalink
Add Groonga::Config#each
Browse files Browse the repository at this point in the history
To bind the following functions:

  * grn_config_cursor_open()
  * grn_config_cursor_next()
  * grn_config_cursor_get_key()
  * grn_config_cursor_get_value()

GitHub: #116
  • Loading branch information
Masafumi Yokoyama committed Mar 5, 2016
1 parent ef9271a commit 3731d9b
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 0 deletions.
57 changes: 57 additions & 0 deletions ext/groonga/rb-grn-config.c
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,61 @@ rb_grn_config_delete (VALUE self, VALUE rb_key)
return Qnil;
}

/*
* Passes all key/value of the config to block in order.
*
* @example Shows all keys and all values of the config
* context.config["rroonga.key1"] = "value1"
* context.config["rroonga.key2"] = "value2"
* keys = []
* values = []
* context.config.each do |key, value|
* keys << key
* values << value
* end
* p keys # => ["rroonga.key1", "rroonga.key2"]
* p values # => ["value1", "value2"]
*
* @overload each
* @yield [key, value]
*
* @since 6.0.0
*/
static VALUE
rb_grn_config_each (VALUE self)
{
grn_ctx *context = NULL;
grn_obj *cursor;
VALUE rb_context;

RETURN_ENUMERATOR(self, 0, NULL);

rb_context = rb_iv_get(self, "@context");
context = rb_grn_context_ensure(&rb_context);

cursor = grn_config_cursor_open(context);
while (grn_config_cursor_next(context, cursor) == GRN_TRUE) {
const char *key;
uint32_t key_size;
const char *value;
uint32_t value_size;
VALUE rb_key, rb_value, rb_key_value;

key_size = grn_config_cursor_get_key(context, cursor, &key);
rb_key = rb_str_new(key, key_size);
value_size = grn_config_cursor_get_value(context, cursor, &value);
rb_value = rb_str_new(value, value_size);

rb_key_value = rb_ary_new();
rb_ary_push(rb_key_value, rb_key);
rb_ary_push(rb_key_value, rb_value);

rb_yield(rb_key_value);
}

return Qnil;
}

void
rb_grn_init_config (VALUE mGrn)
{
Expand All @@ -180,4 +235,6 @@ rb_grn_init_config (VALUE mGrn)
rb_define_method(cGrnConfig, "[]=", rb_grn_config_set, 2);

rb_define_method(cGrnConfig, "delete", rb_grn_config_delete, 1);

rb_define_method(cGrnConfig, "each", rb_grn_config_each, 0);
}
10 changes: 10 additions & 0 deletions test/test-config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,14 @@ class ConfigTest < Test::Unit::TestCase
context.config.delete("rroonga.key")
assert_nil(context.config["rroonga.key"])
end

test "#each" do
context.config["rroonga.key1"] = "value1"
context.config["rroonga.key2"] = "value2"
assert_equal([
["rroonga.key1", "value1"],
["rroonga.key2", "value2"],
],
context.config.each.to_a)
end
end

0 comments on commit 3731d9b

Please sign in to comment.