forked from aukan/aukan-bitmask
-
Notifications
You must be signed in to change notification settings - Fork 0
/
README.txt
79 lines (56 loc) · 1.14 KB
/
README.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
aukan-bitmask
=============
Agnostic Bitmask Attribute
# Installation
``` sh
gem install aukan-bitmask
```
# Bitmask
Usage:
```rb
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:
```rb
flags.value = 5
flags.get :flag1 # true
flags.get :flag2 # false
flags.get :flag3 # true
```
Changing the bit ids:
```rb
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
```
# BitmaskAttribute
Uses Bitmask to decorate a class attribute. Can be used on ActiveRecord or any ORM.
Usage:
```rb
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
```