- db = create ? notmuch_database_create(path) : notmuch_database_open(path, mode);
- if (!db)
- rb_raise(notmuch_rb_eDatabaseError, "failed to open database");
+ Check_Type(self, T_DATA);
+ DATA_PTR(self) = create ? notmuch_database_create(path) : notmuch_database_open(path, mode);
+ if (!DATA_PTR(self))
+ rb_raise(notmuch_rb_eDatabaseError, "Failed to open database");
+
+ return self;
+}
+
+/*
+ * call-seq: Notmuch::Database.open(path [, ahash]) {|db| ...}
+ *
+ * Identical to new, except that when it is called with a block, it yields with
+ * the new instance and closes it, and returns the result which is returned from
+ * the block.
+ */
+VALUE
+notmuch_rb_database_open(int argc, VALUE *argv, VALUE klass)
+{
+ VALUE obj;
+
+ obj = rb_class_new_instance(argc, argv, klass);
+ if (!rb_block_given_p())
+ return obj;