commit 00a67f9534fc4802dd5d9624d9c173c5b0560536 Author: Felipe Contreras Date: Tue Jun 29 14:08:33 2021 -0500 ruby: add new Database.open_with_config In order to make use of notmuch_database_open_with_config. Signed-off-by: Felipe Contreras diff --git a/bindings/ruby/database.c b/bindings/ruby/database.c index 9c3dbd96..f885c66c 100644 --- a/bindings/ruby/database.c +++ b/bindings/ruby/database.c @@ -118,6 +118,68 @@ notmuch_rb_database_open (int argc, VALUE *argv, VALUE klass) return rb_ensure (rb_yield, obj, notmuch_rb_database_close, obj); } +/* + * call-seq: Notmuch::Database.open_with_config([database_path:, mode:, config_path:, profile:]) [{|db| ... }] + * + * Opens a database with a configuration file. + * + */ +VALUE +notmuch_rb_database_open_with_config (int argc, VALUE *argv, VALUE klass) +{ + VALUE obj; + notmuch_database_t *db; + notmuch_status_t ret; + VALUE opts; + const char *database_path = NULL; + notmuch_database_mode_t mode = NOTMUCH_DATABASE_MODE_READ_ONLY; + const char *config_path = NULL; + const char *profile = NULL; + + rb_scan_args (argc, argv, ":", &opts); + + if (!NIL_P (opts)) { + VALUE rdatabase_path, rmode, rconfig_path, rprofile; + VALUE kwargs[4]; + static ID keyword_ids[4]; + + if (!keyword_ids[0]) { + keyword_ids[0] = rb_intern_const ("database_path"); + keyword_ids[1] = rb_intern_const ("mode"); + keyword_ids[2] = rb_intern_const ("config_path"); + keyword_ids[3] = rb_intern_const ("profile"); + } + + rb_get_kwargs (opts, keyword_ids, 0, 4, kwargs); + + rdatabase_path = kwargs[0]; + rmode = kwargs[1]; + rconfig_path = kwargs[2]; + rprofile = kwargs[3]; + + if (rdatabase_path != Qundef) + database_path = nm_str (rdatabase_path); + if (rmode != Qundef) + mode = FIX2INT (rmode); + if (rconfig_path != Qundef) + config_path = nm_str (rconfig_path); + if (rprofile != Qundef) + profile = nm_str (rprofile); + } + + ret = notmuch_database_open_with_config (database_path, mode, + config_path, profile, &db, + NULL); + notmuch_rb_status_raise (ret); + obj = notmuch_rb_database_alloc (klass); + DATA_PTR (obj) = db; + + if (!rb_block_given_p ()) + return obj; + + return rb_ensure (rb_yield, obj, notmuch_rb_database_close, obj); +} + /* * call-seq: DB.close => nil * diff --git a/bindings/ruby/defs.h b/bindings/ruby/defs.h index e2541e8f..5cc95ef2 100644 --- a/bindings/ruby/defs.h +++ b/bindings/ruby/defs.h @@ -56,6 +56,9 @@ extern ID ID_db_mode; # define RSTRING_PTR(v) (RSTRING((v))->ptr) #endif /* !defined (RSTRING_PTR) */ +/* Simple string helpers */ +#define nm_str(str) (str != Qnil ? RSTRING_PTR (str) : NULL) + extern const rb_data_type_t notmuch_rb_object_type; extern const rb_data_type_t notmuch_rb_database_type; extern const rb_data_type_t notmuch_rb_directory_type; @@ -166,6 +169,9 @@ notmuch_rb_database_initialize (int argc, VALUE *argv, VALUE klass); VALUE notmuch_rb_database_open (int argc, VALUE *argv, VALUE klass); +VALUE +notmuch_rb_database_open_with_config (int argc, VALUE *argv, VALUE klass); + VALUE notmuch_rb_database_close (VALUE self); diff --git a/bindings/ruby/init.c b/bindings/ruby/init.c index cd9f04cd..d81101c7 100644 --- a/bindings/ruby/init.c +++ b/bindings/ruby/init.c @@ -265,6 +265,7 @@ Init_notmuch (void) notmuch_rb_cDatabase = rb_define_class_under (mod, "Database", rb_cObject); rb_define_alloc_func (notmuch_rb_cDatabase, notmuch_rb_database_alloc); rb_define_singleton_method (notmuch_rb_cDatabase, "open", notmuch_rb_database_open, -1); /* in database.c */ + rb_define_singleton_method (notmuch_rb_cDatabase, "open_with_config", notmuch_rb_database_open_with_config, -1); /* in database.c */ rb_define_method (notmuch_rb_cDatabase, "initialize", notmuch_rb_database_initialize, -1); /* in database.c */ rb_define_method (notmuch_rb_cDatabase, "destroy!", notmuch_rb_database_destroy, 0); /* in database.c */ rb_define_method (notmuch_rb_cDatabase, "close", notmuch_rb_database_close, 0); /* in database.c */ diff --git a/test/T395-ruby.sh b/test/T395-ruby.sh index e828efed..9a1df913 100755 --- a/test/T395-ruby.sh +++ b/test/T395-ruby.sh @@ -100,4 +100,10 @@ q.search_threads.each do |t| end EOF +test_begin_subtest "open with config" +echo "$MAIL_DIR" > EXPECTED +test_ruby <