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

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

Date: Tue, 29 Jun 2021 14:08:34 -0500

To: notmuch@notmuchmail.org

Cc: David Bremner, Felipe Contreras

From: Felipe Contreras


In order to use notmuch_config_get_pairs.

Signed-off-by: Felipe Contreras <felipe.contreras@gmail.com>
---
 bindings/ruby/database.c | 31 +++++++++++++++++++++++++++++++
 bindings/ruby/defs.h     |  4 ++++
 bindings/ruby/init.c     |  1 +
 test/T395-ruby.sh        |  8 ++++++++
 4 files changed, 44 insertions(+)

diff --git a/bindings/ruby/database.c b/bindings/ruby/database.c
index 89d9619e..571f7e24 100644
--- a/bindings/ruby/database.c
+++ b/bindings/ruby/database.c
@@ -520,3 +520,34 @@ notmuch_rb_database_query_create (int argc, VALUE *argv, VALUE self)
 
     return Data_Wrap_Notmuch_Object (notmuch_rb_cQuery, &notmuch_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 cba9fd69..f3371b5d 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 (int argc, VALUE *argv, VALUE self);
 
+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 7b4de86a..fad357dc 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 9a1df913..91c310c6 100755
--- a/test/T395-ruby.sh
+++ b/test/T395-ruby.sh
@@ -106,4 +106,12 @@ test_ruby <<EOF
 puts Notmuch::Database.open_with_config.path
 EOF
 
+test_begin_subtest "config"
+notmuch config list | grep -v '^built_with\.' > EXPECTED
+test_ruby <<"EOF"
+Notmuch::Database.open_with_config do |db|
+  db.config { |e| puts '%s=%s' % e }
+end
+EOF
+
 test_done
-- 
2.32.0
_______________________________________________
notmuch mailing list -- notmuch@notmuchmail.org
To unsubscribe send an email to notmuch-leave@notmuchmail.org

Thread: