{"id":15288794,"url":"https://github.com/shkm/enummer","last_synced_at":"2025-10-31T07:34:18.055Z","repository":{"id":42722521,"uuid":"435798157","full_name":"shkm/enummer","owner":"shkm","description":"🏳️‍🌈 Multi enums (aka flags) for Rails","archived":false,"fork":false,"pushed_at":"2025-07-30T08:17:56.000Z","size":71,"stargazers_count":41,"open_issues_count":4,"forks_count":5,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-10-19T19:54:53.723Z","etag":null,"topics":["activerecord","bitwise-enum","enums","flags","rails","ruby"],"latest_commit_sha":null,"homepage":"","language":"Ruby","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/shkm.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"MIT-LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2021-12-07T08:21:46.000Z","updated_at":"2025-09-05T15:14:09.000Z","dependencies_parsed_at":"2024-09-12T19:25:01.342Z","dependency_job_id":null,"html_url":"https://github.com/shkm/enummer","commit_stats":{"total_commits":33,"total_committers":4,"mean_commits":8.25,"dds":0.1515151515151515,"last_synced_commit":"0ece7e4a38b6ae0a8f5bbecdfd98d890e9246f93"},"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"purl":"pkg:github/shkm/enummer","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/shkm%2Fenummer","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/shkm%2Fenummer/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/shkm%2Fenummer/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/shkm%2Fenummer/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/shkm","download_url":"https://codeload.github.com/shkm/enummer/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/shkm%2Fenummer/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":281952845,"owners_count":26589144,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-10-31T02:00:07.401Z","response_time":57,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["activerecord","bitwise-enum","enums","flags","rails","ruby"],"created_at":"2024-09-30T15:53:11.582Z","updated_at":"2025-10-31T07:34:18.048Z","avatar_url":"https://github.com/shkm.png","language":"Ruby","readme":"# Enummer\n\n[![Gem](https://img.shields.io/gem/v/enummer?color=%2330336b)](https://rubygems.org/gems/enummer)\n[![Codecov](https://img.shields.io/codecov/c/github/shkm/enummer/main)](https://app.codecov.io/gh/shkm/enummer)\n[![Licence](https://img.shields.io/github/license/shkm/enummer?color=%2395afc0)](https://github.com/shkm/enummer/blob/main/MIT-LICENSE)\n[![Documentation](https://img.shields.io/badge/yard-docs-%23686de0)](https://www.rubydoc.info/github/shkm/enummer/main)\n\nEnummer is a lightweight answer for adding enums with multiple values to Rails, with a similar syntax to Rails' built-in `enum`.\n\nEnummer officially supports Ruby versions 2.7, 3.0 and 3.1 with SQLite, PostgreSQL, and MariaDB.\n\n## Installation\n\nAdd `gem \"enummer\"` to your Gemfile and `bundle`.\n\n## Usage\n\n### Setup\n\nCreate a migration for an integer that looks something like this:\n\n```ruby\nclass CreateUsers \u003c ActiveRecord::Migration[7.0]\n  def change\n    create_table :users do |t|\n      t.integer :permissions, default: 0, null: false\n    end\n  end\nend\n```\n\nNow set up enummer with the available values in your model:\n\n```ruby\nenummer permissions: %i[read write execute]\n```\n\nSimilar 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:\n\n```ruby\nenummer permissions: {\n  read: 0,\n  write: 1,\n  execute: 2\n}\n```\n\nThis makes it easier to add/remove entries without worrying about migrating historical data.\n\n### Scopes\n\nScopes will now be provided for `\u003coption\u003e` and `not_\u003coption\u003e`.\n\n```ruby\nUser.read\nUser.not_read\nUser.write\nUser.not_write\nUser.execute\nUser.not_execute\n```\n\nAdditionally, a `with_\u003cname\u003e` scope will be generated which returns all records that match all given options. E.g.:\n\n```ruby\nuser1 = User.create!(permissions: %i[read write execute])\nuser2 = User.create!(permissions: %i[read write])\nuser3 = User.create!(permissions: %i[read])\n\n# .where(permissions: ...) will generate an `IN` query, returning all records that have *any*\n# of those permissions.\nUser.where(permissions: %i[read write]) # =\u003e [user1, user2, user3]\n\n# .with_permissions will return only users that have at least all of the given set of permissions\nUser.with_permissions(%i[read write]) # =\u003e [user1, user2]\n```\n\n### Getter methods\n\nSimply calling the instance method for the column will return an array of options. Question mark methods are also provided.\n\n```ruby\nuser = User.last\n\nuser.permissions # =\u003e [:read, :write]\n\nuser.read? # =\u003e true\nuser.write? # =\u003e true\nuser.execute? # =\u003e false\n```\n\n### Setter methods\n\nOptions can be set with an array of symbols or via bang methods. Bang methods will additionally persist the changes.\n\n```ruby\nuser.update(permissions: %i[read write])\nuser.write!\n```\n\n## FAQ\n\n### Which data type should I use?\n\nThat depends on how many options you expect to store. [In PostgreSQL](https://www.postgresql.org/docs/9.1/datatype-numeric.html) you should be able to store `bytes * 8 - 1` of your data type:\n\n| Type     | Bytes | Values      |\n| -------- | ----- | ----------- |\n| smallint | 2     | 15          |\n| integer  | 4     | 31          |\n| bigint   | 8     | 65          |\n| numeric  | ???   | all of them |\n\n### How can I use it outside of Rails?\n\nlol stop\n\n## Contributing\n\nMake an issue / PR and we'll see.\n\n### Development\n\n```bash\n$ cd enummer\n$ bundle config set without 'postgres mysql'\n$ bundle install\n$ cd test/dummy\n$ RAILS_ENV=test DATABASE_URL=sqlite3:dummy_test bundle exec rails db:setup\n$ cd ../..\n$ RAILS_ENV=test DATABASE_URL=sqlite3:dummy_test bundle exec bin/test\n```\n\n## Alternatives\n\n- [flag_shih_tzu](https://github.com/pboling/flag_shih_tzu)\n- Lots of booleans\n- DB Arrays\n\n## License\n\nThe gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).\n","funding_links":[],"categories":["Ruby"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fshkm%2Fenummer","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fshkm%2Fenummer","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fshkm%2Fenummer/lists"}