db.close() should close, and db.destroy!() should destroy, it's a simple as that.
In addition, this makes is reasonable to allow the database objects to be garbage collected, and if the database object is destroyed, all the dependent objects are destroyed as well, so foo.destroy!() are not necessary. Signed-off-by: Felipe Contreras <felipe.contreras at gmail.com> --- bindings/ruby/database.c | 22 +++++++++++++++++++--- bindings/ruby/defs.h | 5 ++++- bindings/ruby/init.c | 1 + 3 files changed, 24 insertions(+), 4 deletions(-) diff --git a/bindings/ruby/database.c b/bindings/ruby/database.c index e84f726..8f2e16d 100644 --- a/bindings/ruby/database.c +++ b/bindings/ruby/database.c @@ -23,7 +23,7 @@ VALUE notmuch_rb_database_alloc (VALUE klass) { - return Data_Wrap_Struct (klass, NULL, NULL, NULL); + return Data_Wrap_Struct (klass, NULL, notmuch_database_destroy, NULL); } /* @@ -87,6 +87,23 @@ notmuch_rb_database_initialize (int argc, VALUE *argv, VALUE self) } /* + * call-seq: DB.destroy! => nil + * + * Destroy the notmuch database, freeing all resources allocated for it. + */ +VALUE +notmuch_rb_database_destroy (VALUE self) +{ + notmuch_database_t *db; + + Data_Get_Notmuch_Database (self, db); + notmuch_database_destroy (db); + DATA_PTR (self) = NULL; + + return Qnil; +} + +/* * call-seq: Notmuch::Database.open(path [, ahash]) {|db| ...} * * Identical to new, except that when it is called with a block, it yields with @@ -116,8 +133,7 @@ notmuch_rb_database_close (VALUE self) notmuch_database_t *db; Data_Get_Notmuch_Database (self, db); - notmuch_database_destroy (db); - DATA_PTR (self) = NULL; + notmuch_database_close (db); return Qnil; } diff --git a/bindings/ruby/defs.h b/bindings/ruby/defs.h index 5b44585..0ad198a 100644 --- a/bindings/ruby/defs.h +++ b/bindings/ruby/defs.h @@ -59,7 +59,7 @@ extern ID ID_db_mode; do { \ Check_Type ((obj), T_DATA); \ if (DATA_PTR ((obj)) == NULL) \ - rb_raise (rb_eRuntimeError, "database closed"); \ + rb_raise (rb_eRuntimeError, "database destroyed"); \ Data_Get_Struct ((obj), notmuch_database_t, (ptr)); \ } while (0) @@ -139,6 +139,9 @@ VALUE notmuch_rb_database_initialize (int argc, VALUE *argv, VALUE klass); VALUE +notmuch_rb_database_destroy (VALUE self); + +VALUE notmuch_rb_database_open (int argc, VALUE *argv, VALUE klass); VALUE diff --git a/bindings/ruby/init.c b/bindings/ruby/init.c index 663271d..fee04c3 100644 --- a/bindings/ruby/init.c +++ b/bindings/ruby/init.c @@ -215,6 +215,7 @@ Init_notmuch (void) 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_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 */ rb_define_method (notmuch_rb_cDatabase, "path", notmuch_rb_database_path, 0); /* in database.c */ rb_define_method (notmuch_rb_cDatabase, "version", notmuch_rb_database_version, 0); /* in database.c */ -- 1.8.4-fc