BitmaskAttribute
This gem adds helper methods to your class to facilitate the use of bitmask attributes.
Installation
Add this line to your application's Gemfile:
gem 'bitmask_attribute'
And then execute:
$ bundle
Or install it yourself as:
$ gem install bitmask_attribute
Usage
Include the concern in your class, and define which attribute is the bitmask attribute. This attribute is expected to be an integer type.
# has int attribute called actions
Class Foo
include BitmaskAttribute::Concern
# Hash of options to bit values
OPTIONS_HASH = { option_1: 1, option_2: 2, option_3: 4, option_4: 8 }
...
bitmask_attribute :actions, OPTIONS_HASH
You now get the following methods at your disposal:
foo_instance.action_present?(1)
-> returns boolean describing whether the actions attribute includes the input bit flag.
foo_instance.add_action(1)
-> adds the input bit flag to the mask
foo_instance.remove_action(1)
-> removes the input bit flag from the mask
foo.option_1 = true
or if using an active record model: foo.update(option_1: true)
-> adds the 1 bit to the bitmask attribute
foo_option_1 = false
or if using an active record model: foo.update(option_1: false)
-> removes the 1 bit from the bitmask attribute
Contributing
- Fork it ( https://github.com/[my-github-username]/bitmask_attribute/fork )
- Create your feature branch (
git checkout -b my-new-feature) - Commit your changes (
git commit -am 'Add some feature') - Push to the branch (
git push origin my-new-feature) - Create a new Pull Request