From: Felipe Contreras <felipe.contreras@gmail.com>
To: notmuch@notmuchmail.org
Subject: [PATCH] ruby: db: reorganize initializer
Date: Fri, 31 Mar 2023 14:40:50 -0600 [thread overview]
Message-ID: <20230331204050.174030-1-felipe.contreras@gmail.com> (raw)
In order to make it more extensible.
No functional changes.
Signed-off-by: Felipe Contreras <felipe.contreras@gmail.com>
---
bindings/ruby/database.c | 52 ++++++++++++++++++++++++----------------
bindings/ruby/defs.h | 2 --
bindings/ruby/init.c | 4 ----
3 files changed, 32 insertions(+), 26 deletions(-)
diff --git a/bindings/ruby/database.c b/bindings/ruby/database.c
index b6de1254..67be3b23 100644
--- a/bindings/ruby/database.c
+++ b/bindings/ruby/database.c
@@ -58,37 +58,49 @@ notmuch_rb_database_initialize (int argc, VALUE *argv, VALUE self)
notmuch_database_t *database;
notmuch_status_t ret;
+ path = NULL;
+ create = 0;
+ mode = NOTMUCH_DATABASE_MODE_READ_ONLY;
+
/* Check arguments */
rb_scan_args (argc, argv, "02", &pathv, &hashv);
if (!NIL_P (pathv)) {
SafeStringValue (pathv);
path = RSTRING_PTR (pathv);
- } else {
- path = NULL;
}
if (!NIL_P (hashv)) {
- Check_Type (hashv, T_HASH);
- create = RTEST (rb_hash_aref (hashv, ID2SYM (ID_db_create)));
- modev = rb_hash_aref (hashv, ID2SYM (ID_db_mode));
- if (NIL_P (modev))
- mode = NOTMUCH_DATABASE_MODE_READ_ONLY;
- else if (!FIXNUM_P (modev))
- rb_raise (rb_eTypeError, ":mode isn't a Fixnum");
- else {
- mode = FIX2INT (modev);
- switch (mode) {
- case NOTMUCH_DATABASE_MODE_READ_ONLY:
- case NOTMUCH_DATABASE_MODE_READ_WRITE:
- break;
- default:
- rb_raise ( rb_eTypeError, "Invalid mode");
+ VALUE rmode, rcreate;
+ VALUE kwargs[2];
+ static ID keyword_ids[2];
+
+ if (!keyword_ids[0]) {
+ keyword_ids[0] = rb_intern_const ("mode");
+ keyword_ids[1] = rb_intern_const ("create");
+ }
+
+ rb_get_kwargs (hashv, keyword_ids, 0, 2, kwargs);
+
+ rmode = kwargs[0];
+ rcreate = kwargs[1];
+
+ 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 {
- create = 0;
- mode = NOTMUCH_DATABASE_MODE_READ_ONLY;
+ if (rcreate != Qundef)
+ create = RTEST (rcreate);
}
rb_check_typeddata (self, ¬much_rb_database_type);
diff --git a/bindings/ruby/defs.h b/bindings/ruby/defs.h
index e2541e8f..042e0a89 100644
--- a/bindings/ruby/defs.h
+++ b/bindings/ruby/defs.h
@@ -48,8 +48,6 @@ extern VALUE notmuch_rb_eUnbalancedFreezeThawError;
extern VALUE notmuch_rb_eUnbalancedAtomicError;
extern ID ID_call;
-extern ID ID_db_create;
-extern ID ID_db_mode;
/* RSTRING_PTR() is new in ruby-1.9 */
#if !defined(RSTRING_PTR)
diff --git a/bindings/ruby/init.c b/bindings/ruby/init.c
index cd9f04cd..691e1475 100644
--- a/bindings/ruby/init.c
+++ b/bindings/ruby/init.c
@@ -43,8 +43,6 @@ VALUE notmuch_rb_eUnbalancedFreezeThawError;
VALUE notmuch_rb_eUnbalancedAtomicError;
ID ID_call;
-ID ID_db_create;
-ID ID_db_mode;
const rb_data_type_t notmuch_rb_object_type = {
.wrap_struct_name = "notmuch_object",
@@ -101,8 +99,6 @@ Init_notmuch (void)
VALUE mod;
ID_call = rb_intern ("call");
- ID_db_create = rb_intern ("create");
- ID_db_mode = rb_intern ("mode");
mod = rb_define_module ("Notmuch");
--
2.40.0
next reply other threads:[~2023-03-31 20:41 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-03-31 20:40 Felipe Contreras [this message]
2023-05-29 11:04 ` [PATCH] ruby: db: reorganize initializer David Bremner
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
List information: https://notmuchmail.org/
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20230331204050.174030-1-felipe.contreras@gmail.com \
--to=felipe.contreras@gmail.com \
--cc=notmuch@notmuchmail.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this public inbox
https://yhetil.org/notmuch.git/
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).