Class: Bitmask

Inherits:
Object
  • Object
show all
Defined in:
lib/motion-bitmask/core.rb

Instance Method Summary collapse

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 : []
  @options   = 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_valuesObject



26
27
28
# File 'lib/motion-bitmask/core.rb', line 26

def allowed_values
  @values
end

#raw_valueObject



30
31
32
# File 'lib/motion-bitmask/core.rb', line 30

def raw_value
  @field
end

#set?(value) ⇒ Boolean

Returns:

  • (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

#valuesObject



20
21
22
23
24
# File 'lib/motion-bitmask/core.rb', line 20

def values
  return @values.reject do |value|
    !set?(value)
  end
end