polleverywhere/aukan-bitmask

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

UserMost Recent Commit# Commits

Other Committers

UserEmailMost Recent Commit# Commits

README

aukan-bitmask

Agnostic Bitmask Attribute

Installation

install aukan-bitmask

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

BitmaskAttribute

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.

ActiveRecord Integration

If ActiveRecord is loaded, the “dirty” methods are emulated for the bitmask object:

If :accessors is enabled, they are emulated for each attribute:

ActiveRecord typecasting is used when setting values: common strings found in web forms like "1"/"0" convert to true/false respectively.


This work is supported by the National Institutes of Health's National Center for Advancing Translational Sciences, Grant Number U24TR002306. This work is solely the responsibility of the creators and does not necessarily represent the official views of the National Institutes of Health.