From 8aaa28b37ad2afc202be510da97c5f4ab0dda473 Mon Sep 17 00:00:00 2001 From: Gabriel Nagy Date: Wed, 7 Aug 2024 13:03:10 +0300 Subject: [PATCH 1/3] Allow initializing enummer with hash Similar to the Rails `enum` behavior, allow initializing an `enummer` with a hash where the values correspond to the indices of the bits that map to the flag. This is a more robust and backwards-compatible approach to enums, as the developer doesn't have to worry about migrating historical data if an enum value is removed. --- README.md | 12 ++++++++++++ lib/enummer/enummer_type.rb | 21 ++++++--------------- lib/enummer/extension.rb | 22 ++++++++++++++-------- test/dummy/app/models/user.rb | 6 +++++- test/enummer_test.rb | 5 +++-- 5 files changed, 40 insertions(+), 26 deletions(-) diff --git a/README.md b/README.md index e36972c..0b4ce6d 100644 --- a/README.md +++ b/README.md @@ -33,6 +33,18 @@ Now set up enummer with the available values in your model: enummer permissions: %i[read write execute] ``` +Similar to `enum`, enummer can also be initialized with a hash, where the numeric index represents the position of the bit that maps to the flag: + +``` ruby +enummer permissions: { + read: 0, + write: 1, + execute: 2 +} +``` + +This makes it easier to add/remove entries without worrying about migrating historical data. + ### Scopes Scopes will now be provided for `