Agnostic Bitmask Attribute
gem install aukan-bitmask
Usage:
flags = Bitmask.new({
:bit_ids => [:flag1, :flag2, :flag3],
:value => 0
})
# Setting the value of a bit
flags.set :flag2, true
flags.get :flag1 # false
flags.get :flag2 # true
flags.get :flag3 # false
flags.value # 2
flags.value.to_s(2) # "10"
Reseting the mask value:
flags.value = 5
flags.get :flag1 # true
flags.get :flag2 # false
flags.get :flag3 # true
Changing the bit ids:
flags.value = 5
flags.bit_ids = [:flag1, :flag3, :flag2] # Switching flag2 for flag3
flags.get :flag1 # true
flags.get :flag2 # true
flags.get :flag3 # false
Uses Bitmask to decorate a class attribute. Can be used on ActiveRecord or any ORM.
Usage:
class Something
attr_accessor :flags
include BitmaskAttribute
bitmask_attribute :flags, {
:bit_ids => [
:flag1, :flag2, :flag3
],
:default_value => 3
}
end
algo = Something.new
algo.flags_bitmask.set(:flag1, false)
algo.flags_bitmask.get(:flag1) # false
algo.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.