Name: aukan-bitmask
Owner: Poll Everywhere
Description: Agnostic Bitmask Attribute
Created: 2016-03-07 22:03:01.0
Updated: 2018-04-24 21:21:10.0
Pushed: 2018-04-24 21:21:10.0
Homepage: null
Size: 28
Language: Ruby
GitHub Committers
User | Most Recent Commit | # Commits |
---|
Other Committers
User | Most Recent Commit | # Commits |
---|
Agnostic Bitmask Attribute
install aukan-bitmask
Usage:
s = Bitmask.new({
it_ids => [:flag1, :flag2, :flag3],
alue => 0
tting the value of a bit
s.set :flag2, true
s.get :flag1 # false
s.get :flag2 # true
s.get :flag3 # false
s.value # 2
s.value.to_s(2) # "10"
Reseting the mask value:
s.value = 5
s.get :flag1 # true
s.get :flag2 # false
s.get :flag3 # true
Changing the bit ids:
s.value = 5
s.bit_ids = [:flag1, :flag3, :flag2] # Switching flag2 for flag3
s.get :flag1 # true
s.get :flag2 # true
s.get :flag3 # false
Uses Bitmask to decorate a class attribute. Can be used on ActiveRecord or any ORM.
Usage:
s Something
tr_accessor :flags
clude BitmaskAttribute
tmask_attribute :flags, {
:bit_ids => [
:flag1, :flag2, :flag3
],
:default_value => 3
= Something.new
.flags_bitmask.set(:flag1, false)
.flags_bitmask.get(:flag1) # false
.flags_bitmask.get(:flag2) # true
To add accessor methods for each flag (flag1
/flag1?
/flag1=
) pass the option :accessors => true
.
If ActiveRecord is loaded, the “dirty” methods are emulated for the bitmask object:
flags_bitmask_changed?
flags_bitmask_was
If :accessors is enabled, they are emulated for each attribute:
flag1_changed?
flag1_was
ActiveRecord typecasting is used when setting values: common strings found in web forms like "1"
/"0"
convert to true
/false
respectively.