{"id":13879676,"url":"https://github.com/amiel/bitmasker","last_synced_at":"2025-04-23T17:28:49.192Z","repository":{"id":6052389,"uuid":"7277491","full_name":"amiel/bitmasker","owner":"amiel","description":"Bitmasker allows you to store many boolean values as one integer field in the database.","archived":false,"fork":false,"pushed_at":"2019-12-03T18:17:49.000Z","size":60,"stargazers_count":7,"open_issues_count":3,"forks_count":8,"subscribers_count":3,"default_branch":"master","last_synced_at":"2024-10-03T12:33:03.702Z","etag":null,"topics":["hacktoberfest"],"latest_commit_sha":null,"homepage":"https://github.com/amiel/bitmasker","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/amiel.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"MIT-LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2012-12-21T18:32:12.000Z","updated_at":"2021-09-10T00:12:30.000Z","dependencies_parsed_at":"2022-08-30T16:20:41.296Z","dependency_job_id":null,"html_url":"https://github.com/amiel/bitmasker","commit_stats":null,"previous_names":[],"tags_count":8,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/amiel%2Fbitmasker","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/amiel%2Fbitmasker/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/amiel%2Fbitmasker/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/amiel%2Fbitmasker/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/amiel","download_url":"https://codeload.github.com/amiel/bitmasker/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":250479905,"owners_count":21437455,"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","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":["hacktoberfest"],"created_at":"2024-08-06T08:02:28.576Z","updated_at":"2025-04-23T17:28:49.171Z","avatar_url":"https://github.com/amiel.png","language":"Ruby","funding_links":[],"categories":["Ruby"],"sub_categories":[],"readme":"Bitmasker\n=========\n\nBitmasker allows you to store many boolean values as one integer field in the database.\n\nSynopsis\n--------\n\n\n```ruby\n  class User \u003c ActiveRecord::Base\n    has_bitmask_attributes :notifications do |config|\n      config.attribute :send_weekly_newsletter,    0b0001\n      config.attribute :send_monthly_newsletter,   0b0010, true\n    end\n  end\n```\n\n[![Code Climate](https://codeclimate.com/github/amiel/bitmasker.png)](https://codeclimate.com/github/amiel/bitmasker)\n[![Build Status](https://travis-ci.org/amiel/bitmasker.png)](https://travis-ci.org/amiel/bitmasker)\n\n\nExamples\n--------\n\n```ruby\n  # in migration\n  t.integer :notifications_mask\n\n  # in app/models/user.rb\n  class User \u003c ActiveRecord::Base\n    has_bitmask_attributes :notifications do |config|\n      config.attribute :send_weekly_newsletter,    0b0001\n      config.attribute :send_monthly_newsletter,   0b0010, true\n      config.attribute :send_daily_update,         0b0100\n      config.accessible\n      # config.field_name :notifications_mask # \u003c- default functionality\n    end\n  end\n```\n\nThis will define the following methods:\n\n* `User#notifications` -- returns a BitmaskAttributes object representing all values\n* `User#send_weekly_newsletter?` -- predicate\n* `User#send_weekly_newsletter` -- works just like the predicate, makes it easy to use actionview form helpers\n* `User#send_weekly_newsletter=(value)` -- just give it a boolean value (also takes \"0\" and \"1\" or \"t\" and \"f\" just like activerecord does for boolean fields)\n* `User#send_monthly_newsletter?`\n* `User#send_monthly_newsletter`\n* `User#send_monthly_newsletter=(value)`\n\nthe call to `config.accessible` calls `attr_accessible :send_weekly_newsletter, :send_monthly_newsletter` in your model.\n\n### Scopes\n\nBitmasker also sets up a few useful scopes:\n\n* `User#with_notifications`\n* `User#without_notifications`\n* `User#with_any_notifications`\n\n#### Scopes Examples\n\n```ruby\n# Users that want weekly newsletter\nUser.with_notifications(:send_weekly_newsletter)\n\n# Users that want monthly AND weekly newsletters\nUser.with_notifications(:send_monthly_newsletter, :send_weekly_newsletter)\n\n# Users that want monthly OR weekly newsletters\nUser.with_any_notifications(:send_monthly_newsletter, :send_weekly_newsletter)\n```\n\n\nView Example\n------------\n\n```erb\n  # in your view\n  \u003c% form_for @user do |f| %\u003e\n    Monthly Newsletter: \u003c%= f.check_box :send_monthly_newsletter? %\u003e\n    or\n    Monthly Newsletter\n    Yes: \u003c%= f.radio_button :send_monthly_newsletter, 'true' %\u003e\n    No: \u003c%= f.radio_button :send_monthly_newsletter, 'false' %\u003e\n  \u003c% end %\u003e\n```\n\n\nConfig Options\n--------------\n\n`config.attribute(name, mask, default = false)`\n\nSets up a binary attribute. Defines three functions: name, name?, and name=(true|false)\n* `name`    a symbol, Bitmasker will define\n* `mask`    example: 0b0000001, must be a power of 2\n* `default`   set to true if you want the attribute to default to true\n\n`config.accessible`\nif you are using attr_accessible in your model and you want to mass-assign your bitmask attributes, you will want to call this\n\n`config.field_name(name)`\n* `name`    name of the field name in the database where all this info is stored, should be an integer\n\n\nUpdating from `has_bitmask_attributes`\n--------------------------------------\n\nIf you used the `method_format` feature from `has_bitmask_attributes`, you will need to change\nyour configuration as `method_format` has been removed.\n\n### Before\n\n```ruby\n  # in app/models/user.rb\n  class User \u003c ActiveRecord::Base\n    has_bitmask_attributes :notifications do |config|\n      config.attribute :weekly_newsletter,    0b0001\n      config.attribute :monthly_newsletter,   0b0010, true\n      config.method_format 'send_%s'\n    end\n  end\n```\n\n### After\n\n```ruby\n  # in app/models/user.rb\n  class User \u003c ActiveRecord::Base\n    has_bitmask_attributes :notifications do |config|\n      config.attribute :send_weekly_newsletter,    0b0001\n      config.attribute :send_monthly_newsletter,   0b0010, true\n    end\n  end\n```\n\n\nCopyright (c) 2012 Amiel Martin, released under the MIT license\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Famiel%2Fbitmasker","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Famiel%2Fbitmasker","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Famiel%2Fbitmasker/lists"}