https://github.com/amiel/bitmasker
Bitmasker allows you to store many boolean values as one integer field in the database.
https://github.com/amiel/bitmasker
hacktoberfest
Last synced: about 1 year ago
JSON representation
Bitmasker allows you to store many boolean values as one integer field in the database.
- Host: GitHub
- URL: https://github.com/amiel/bitmasker
- Owner: amiel
- License: mit
- Created: 2012-12-21T18:32:12.000Z (over 13 years ago)
- Default Branch: master
- Last Pushed: 2019-12-03T18:17:49.000Z (over 6 years ago)
- Last Synced: 2024-10-03T12:33:03.702Z (over 1 year ago)
- Topics: hacktoberfest
- Language: Ruby
- Homepage: https://github.com/amiel/bitmasker
- Size: 58.6 KB
- Stars: 7
- Watchers: 3
- Forks: 8
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- License: MIT-LICENSE
Awesome Lists containing this project
README
Bitmasker
=========
Bitmasker allows you to store many boolean values as one integer field in the database.
Synopsis
--------
```ruby
class User < ActiveRecord::Base
has_bitmask_attributes :notifications do |config|
config.attribute :send_weekly_newsletter, 0b0001
config.attribute :send_monthly_newsletter, 0b0010, true
end
end
```
[](https://codeclimate.com/github/amiel/bitmasker)
[](https://travis-ci.org/amiel/bitmasker)
Examples
--------
```ruby
# in migration
t.integer :notifications_mask
# in app/models/user.rb
class User < ActiveRecord::Base
has_bitmask_attributes :notifications do |config|
config.attribute :send_weekly_newsletter, 0b0001
config.attribute :send_monthly_newsletter, 0b0010, true
config.attribute :send_daily_update, 0b0100
config.accessible
# config.field_name :notifications_mask # <- default functionality
end
end
```
This will define the following methods:
* `User#notifications` -- returns a BitmaskAttributes object representing all values
* `User#send_weekly_newsletter?` -- predicate
* `User#send_weekly_newsletter` -- works just like the predicate, makes it easy to use actionview form helpers
* `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)
* `User#send_monthly_newsletter?`
* `User#send_monthly_newsletter`
* `User#send_monthly_newsletter=(value)`
the call to `config.accessible` calls `attr_accessible :send_weekly_newsletter, :send_monthly_newsletter` in your model.
### Scopes
Bitmasker also sets up a few useful scopes:
* `User#with_notifications`
* `User#without_notifications`
* `User#with_any_notifications`
#### Scopes Examples
```ruby
# Users that want weekly newsletter
User.with_notifications(:send_weekly_newsletter)
# Users that want monthly AND weekly newsletters
User.with_notifications(:send_monthly_newsletter, :send_weekly_newsletter)
# Users that want monthly OR weekly newsletters
User.with_any_notifications(:send_monthly_newsletter, :send_weekly_newsletter)
```
View Example
------------
```erb
# in your view
<% form_for @user do |f| %>
Monthly Newsletter: <%= f.check_box :send_monthly_newsletter? %>
or
Monthly Newsletter
Yes: <%= f.radio_button :send_monthly_newsletter, 'true' %>
No: <%= f.radio_button :send_monthly_newsletter, 'false' %>
<% end %>
```
Config Options
--------------
`config.attribute(name, mask, default = false)`
Sets up a binary attribute. Defines three functions: name, name?, and name=(true|false)
* `name` a symbol, Bitmasker will define
* `mask` example: 0b0000001, must be a power of 2
* `default` set to true if you want the attribute to default to true
`config.accessible`
if you are using attr_accessible in your model and you want to mass-assign your bitmask attributes, you will want to call this
`config.field_name(name)`
* `name` name of the field name in the database where all this info is stored, should be an integer
Updating from `has_bitmask_attributes`
--------------------------------------
If you used the `method_format` feature from `has_bitmask_attributes`, you will need to change
your configuration as `method_format` has been removed.
### Before
```ruby
# in app/models/user.rb
class User < ActiveRecord::Base
has_bitmask_attributes :notifications do |config|
config.attribute :weekly_newsletter, 0b0001
config.attribute :monthly_newsletter, 0b0010, true
config.method_format 'send_%s'
end
end
```
### After
```ruby
# in app/models/user.rb
class User < ActiveRecord::Base
has_bitmask_attributes :notifications do |config|
config.attribute :send_weekly_newsletter, 0b0001
config.attribute :send_monthly_newsletter, 0b0010, true
end
end
```
Copyright (c) 2012 Amiel Martin, released under the MIT license