class BitAttrs::Bitset

Public Class Methods

new(flags_map, mask = 0) click to toggle source
# File lib/bit_attrs/bitset.rb, line 4
def initialize(flags_map, mask = 0)
  @flags_map = flags_map
  @mask = mask.to_i
end

Public Instance Methods

[](flag) click to toggle source
# File lib/bit_attrs/bitset.rb, line 15
def [](flag)
  i = @flags_map[flag.to_sym]
  bit = (@mask >> i) & 1

  bit == 1
end
[]=(flag, value) click to toggle source
# File lib/bit_attrs/bitset.rb, line 22
def []=(flag, value)
  i = @flags_map[flag.to_sym]

  if truthy?(value)
    @mask |= 1 << i
  else
    @mask &= ~(1 << i)
  end
end
each(&block) click to toggle source
# File lib/bit_attrs/bitset.rb, line 32
def each(&block)
  self.to_h.each &block
end
inspect() click to toggle source
# File lib/bit_attrs/bitset.rb, line 36
def inspect
  to_h.inspect
end
to_h() click to toggle source
# File lib/bit_attrs/bitset.rb, line 9
def to_h
  @flags_map.inject({}) do |memo, (flag, _i)|
    memo.merge! flag => self[flag]
  end
end
to_i() click to toggle source
# File lib/bit_attrs/bitset.rb, line 40
def to_i
  @mask
end

Private Instance Methods

truthy?(value) click to toggle source
# File lib/bit_attrs/bitset.rb, line 46
def truthy?(value)
  value == true || value == 'true' || value == '1'
end