Re: [PATCH 2/3] ruby: add db.config

2021-06-03 Thread Felipe Contreras
On Thu, Jun 3, 2021 at 10:29 PM Felipe Contreras
 wrote:

> --- a/test/T395-ruby.sh
> +++ b/test/T395-ruby.sh
> @@ -88,4 +88,11 @@ test_ruby <  puts Notmuch::Database.open_with_config.path
>  EOF
>
> +test_begin_subtest "config"
> +notmuch config list | grep -v '^built_with\.' > EXPECTED
> +test_ruby <<"EOF"
> +config_db = Notmuch::Database.open_with_config
> +config_db.config { |e| puts '%s=%s' % e }
> +EOF
> +

I just noticed this might a little more idiomatic:

  Notmuch::Database.open_with_config do |db|
db.config { |e| puts '%s=%s' % e }
  end

-- 
Felipe Contreras
___
notmuch mailing list -- notmuch@notmuchmail.org
To unsubscribe send an email to notmuch-le...@notmuchmail.org


[PATCH 2/3] ruby: add db.config

2021-06-03 Thread Felipe Contreras
In order to use notmuch_config_get_pairs.

Signed-off-by: Felipe Contreras 
---
 bindings/ruby/database.c | 31 +++
 bindings/ruby/defs.h |  4 
 bindings/ruby/init.c |  1 +
 test/T395-ruby.sh|  7 +++
 4 files changed, 43 insertions(+)

diff --git a/bindings/ruby/database.c b/bindings/ruby/database.c
index bc0c22cb..934cbfe8 100644
--- a/bindings/ruby/database.c
+++ b/bindings/ruby/database.c
@@ -479,3 +479,34 @@ notmuch_rb_database_query_create (VALUE self, VALUE qstrv)
 
 return Data_Wrap_Notmuch_Object (notmuch_rb_cQuery, 
¬much_rb_query_type, query);
 }
+
+/*
+ * call-seq: DB.config(prefix) {|key, value| block} => nil
+ *
+ * Calls +block+ once for each key/value pair.
+ *
+ */
+VALUE
+notmuch_rb_database_config (int argc, VALUE *argv, VALUE self)
+{
+VALUE prefix;
+notmuch_database_t *db;
+notmuch_config_pairs_t *list;
+const char *cprefix;
+
+Data_Get_Notmuch_Database (self, db);
+
+rb_scan_args (argc, argv, "01", &prefix);
+
+cprefix = prefix != Qnil ? RSTRING_PTR (prefix) : "";
+
+list = notmuch_config_get_pairs (db, cprefix);
+for (; notmuch_config_pairs_valid (list); 
notmuch_config_pairs_move_to_next (list)) {
+   const char *key = notmuch_config_pairs_key (list);
+   const char *value = notmuch_config_pairs_value (list);
+   rb_yield (rb_ary_new3(2, nm_str_new (key), nm_str_new (value)));
+}
+notmuch_config_pairs_destroy (list);
+
+return Qnil;
+}
diff --git a/bindings/ruby/defs.h b/bindings/ruby/defs.h
index 78239229..f31e563b 100644
--- a/bindings/ruby/defs.h
+++ b/bindings/ruby/defs.h
@@ -57,6 +57,7 @@ extern ID ID_db_mode;
 
 /* Simple string helpers */
 #define nm_str(str) (str != Qnil ? RSTRING_PTR (str) : NULL)
+#define nm_str_new(str) (str ? rb_str_new_cstr (str) : Qnil)
 
 extern const rb_data_type_t notmuch_rb_object_type;
 extern const rb_data_type_t notmuch_rb_database_type;
@@ -182,6 +183,9 @@ notmuch_rb_database_get_all_tags (VALUE self);
 VALUE
 notmuch_rb_database_query_create (VALUE self, VALUE qstrv);
 
+VALUE
+notmuch_rb_database_config (int argc, VALUE *argv, VALUE self);
+
 /* directory.c */
 VALUE
 notmuch_rb_directory_destroy (VALUE self);
diff --git a/bindings/ruby/init.c b/bindings/ruby/init.c
index 27c7eba3..4b2f8655 100644
--- a/bindings/ruby/init.c
+++ b/bindings/ruby/init.c
@@ -277,6 +277,7 @@ Init_notmuch (void)
  notmuch_rb_database_find_message_by_filename, 1); /* in 
database.c */
 rb_define_method (notmuch_rb_cDatabase, "all_tags", 
notmuch_rb_database_get_all_tags, 0); /* in database.c */
 rb_define_method (notmuch_rb_cDatabase, "query", 
notmuch_rb_database_query_create, 1); /* in database.c */
+rb_define_method (notmuch_rb_cDatabase, "config", 
notmuch_rb_database_config, -1); /* in database.c */
 
 /*
  * Document-class: Notmuch::Directory
diff --git a/test/T395-ruby.sh b/test/T395-ruby.sh
index 27ac4cc8..a7b09589 100755
--- a/test/T395-ruby.sh
+++ b/test/T395-ruby.sh
@@ -88,4 +88,11 @@ test_ruby < EXPECTED
+test_ruby <<"EOF"
+config_db = Notmuch::Database.open_with_config
+config_db.config { |e| puts '%s=%s' % e }
+EOF
+
 test_done
-- 
2.32.0.rc2
___
notmuch mailing list -- notmuch@notmuchmail.org
To unsubscribe send an email to notmuch-le...@notmuchmail.org