Class: Bitmask
- Inherits:
-
Object
- Object
- Bitmask
- Defined in:
- lib/motion-bitmask/core.rb
Instance Method Summary collapse
- #<<(value) ⇒ Object
- #allowed_values ⇒ Object
-
#initialize(*args) ⇒ Bitmask
constructor
A new instance of Bitmask.
- #raw_value ⇒ Object
- #set?(value) ⇒ Boolean
- #values ⇒ Object
Constructor Details
#initialize(*args) ⇒ Bitmask
Returns a new instance of Bitmask.
3 4 5 6 7 8 |
# File 'lib/motion-bitmask/core.rb', line 3 def initialize(*args) @field = args.first.is_a?(Integer) ? args.shift : 0 @values = args.first.is_a?(Array) ? args.shift : [] = args.last.is_a?(Hash) ? args.pop : {} self end |
Instance Method Details
#<<(value) ⇒ Object
10 11 12 13 |
# File 'lib/motion-bitmask/core.rb', line 10 def <<(value) validate_value(value) @field |= 1 << @values.index(value)**2 end |
#allowed_values ⇒ Object
26 27 28 |
# File 'lib/motion-bitmask/core.rb', line 26 def allowed_values @values end |
#raw_value ⇒ Object
30 31 32 |
# File 'lib/motion-bitmask/core.rb', line 30 def raw_value @field end |
#set?(value) ⇒ Boolean
15 16 17 18 |
# File 'lib/motion-bitmask/core.rb', line 15 def set?(value) validate_value(value) @field & (1 << @values.index(value)**2) > 0 end |
#values ⇒ Object
20 21 22 23 24 |
# File 'lib/motion-bitmask/core.rb', line 20 def values return @values.reject do |value| !set?(value) end end |