ruby: db: reorganize initializer

In order to make it more extensible.

No functional changes.

Signed-off-by: Felipe Contreras <felipe.contreras@gmail.com>
This commit is contained in:
Felipe Contreras 2023-03-31 14:40:50 -06:00 committed by David Bremner
parent 239fdbbbf0
commit 95a4bf3817
3 changed files with 32 additions and 26 deletions

View file

@ -58,37 +58,49 @@ notmuch_rb_database_initialize (int argc, VALUE *argv, VALUE self)
notmuch_database_t *database; notmuch_database_t *database;
notmuch_status_t ret; notmuch_status_t ret;
path = NULL;
create = 0;
mode = NOTMUCH_DATABASE_MODE_READ_ONLY;
/* Check arguments */ /* Check arguments */
rb_scan_args (argc, argv, "02", &pathv, &hashv); rb_scan_args (argc, argv, "02", &pathv, &hashv);
if (!NIL_P (pathv)) { if (!NIL_P (pathv)) {
SafeStringValue (pathv); SafeStringValue (pathv);
path = RSTRING_PTR (pathv); path = RSTRING_PTR (pathv);
} else {
path = NULL;
} }
if (!NIL_P (hashv)) { if (!NIL_P (hashv)) {
Check_Type (hashv, T_HASH); VALUE rmode, rcreate;
create = RTEST (rb_hash_aref (hashv, ID2SYM (ID_db_create))); VALUE kwargs[2];
modev = rb_hash_aref (hashv, ID2SYM (ID_db_mode)); static ID keyword_ids[2];
if (NIL_P (modev))
mode = NOTMUCH_DATABASE_MODE_READ_ONLY; if (!keyword_ids[0]) {
else if (!FIXNUM_P (modev)) keyword_ids[0] = rb_intern_const ("mode");
rb_raise (rb_eTypeError, ":mode isn't a Fixnum"); keyword_ids[1] = rb_intern_const ("create");
else { }
mode = FIX2INT (modev);
switch (mode) { rb_get_kwargs (hashv, keyword_ids, 0, 2, kwargs);
case NOTMUCH_DATABASE_MODE_READ_ONLY:
case NOTMUCH_DATABASE_MODE_READ_WRITE: rmode = kwargs[0];
break; rcreate = kwargs[1];
default:
rb_raise ( rb_eTypeError, "Invalid mode"); if (rmode != Qundef) {
if (!FIXNUM_P (rmode))
rb_raise (rb_eTypeError, ":mode isn't a Fixnum");
else {
mode = FIX2INT (rmode);
switch (mode) {
case NOTMUCH_DATABASE_MODE_READ_ONLY:
case NOTMUCH_DATABASE_MODE_READ_WRITE:
break;
default:
rb_raise ( rb_eTypeError, "Invalid mode");
}
} }
} }
} else { if (rcreate != Qundef)
create = 0; create = RTEST (rcreate);
mode = NOTMUCH_DATABASE_MODE_READ_ONLY;
} }
rb_check_typeddata (self, &notmuch_rb_database_type); rb_check_typeddata (self, &notmuch_rb_database_type);

View file

@ -47,8 +47,6 @@ extern VALUE notmuch_rb_eUnbalancedFreezeThawError;
extern VALUE notmuch_rb_eUnbalancedAtomicError; extern VALUE notmuch_rb_eUnbalancedAtomicError;
extern ID ID_call; extern ID ID_call;
extern ID ID_db_create;
extern ID ID_db_mode;
/* RSTRING_PTR() is new in ruby-1.9 */ /* RSTRING_PTR() is new in ruby-1.9 */
#if !defined(RSTRING_PTR) #if !defined(RSTRING_PTR)

View file

@ -41,8 +41,6 @@ VALUE notmuch_rb_eUnbalancedFreezeThawError;
VALUE notmuch_rb_eUnbalancedAtomicError; VALUE notmuch_rb_eUnbalancedAtomicError;
ID ID_call; ID ID_call;
ID ID_db_create;
ID ID_db_mode;
const rb_data_type_t notmuch_rb_object_type = { const rb_data_type_t notmuch_rb_object_type = {
.wrap_struct_name = "notmuch_object", .wrap_struct_name = "notmuch_object",
@ -95,8 +93,6 @@ Init_notmuch (void)
VALUE mod; VALUE mod;
ID_call = rb_intern ("call"); ID_call = rb_intern ("call");
ID_db_create = rb_intern ("create");
ID_db_mode = rb_intern ("mode");
mod = rb_define_module ("Notmuch"); mod = rb_define_module ("Notmuch");