class Banditry::BanditMask

Attributes

bitmask[R]

Public Class Methods

bit(name, value) click to toggle source

Maps name to value in the global list of defined bits.

class MyMask < Banditry::BanditMask
  bit :read, 0b001
  bit :write, 0b010
  bit :execute, 0b100
end
# File lib/banditry/bandit_mask.rb, line 34
def self.bit(name, value)
  bits.update name.to_sym => value
end
bits() click to toggle source

Returns a Hash mapping all defined names to their respective bits.

class MyMask < Banditry::BanditMask
  bit :read, 0b01
  bit :write, 0b10
end

MyMask.bits # => { :read => 1, :write => 2 }
# File lib/banditry/bandit_mask.rb, line 22
def self.bits
  @bits ||= {}
end

Public Instance Methods

<<(bit) click to toggle source

Enables the bit named bit. Returns self, so calls to << can be chained. (Think Array#<<.) Raises ArgumentError if bit does not correspond to a bit that was previously defined with Banditry::BanditMask.bit.

class MyMask < Banditry::BanditMask
  bit :read, 0b01
  bit :write, 0b10
end

mask = MyMask.new       # => #<MyMask:0x007f9ebd44ae40 @bitmask=0>
mask << :read << :write # => #<MyMask:0x007f9ebd44ae40 @bitmask=3>
# File lib/banditry/bandit_mask.rb, line 91
def <<(bit)
  @bitmask |= bit_value(bit)
  self
end
==(other) click to toggle source

Returns true if other is an instance of the same class as self and they have the same bitmask.

class MyMask < Banditry::BanditMask
  bit :read, 0b01
  bit :write, 0b10
end

class MyOtherMask < Banditry::BanditMask
  bit :chocolate, 0b01
  bit :vanilla, 0b10
end

a = MyMask.new 0b1
b = MyMask.new 0b1
c = MyMask.new 0b0
d = MyOtherMask.new 0b1

a == b # => true
a == c # => false
a == d # => false
# File lib/banditry/bandit_mask.rb, line 134
def ==(other)
  other.class == self.class && other.bitmask == bitmask
end
Also aliased as: eql?
bits() click to toggle source

Returns an array of names of the currently enabled bits.

class MyMask < Banditry::BanditMask
  bit :read, 0b01
  bit :write, 0b10
end

mask = MyMask.new 0b01
mask.bits # => [:read]
# File lib/banditry/bandit_mask.rb, line 54
def bits
  self.class.bits.select { |bit, _| include? bit }.keys
end
Also aliased as: to_a
each(&block) click to toggle source

Delegates to bits.

class MyMask < Banditry::BanditMask
  bit :read, 0b001
  bit :write, 0b010
  bit :execute, 0b100
end

mask = MyMask.new 0b101
mask.each do |bit|
  # => :read
  # => :execute
end
mask.each # => #<Enumerator: ...>
# File lib/banditry/bandit_mask.rb, line 74
def each(&block)
  bits.each(&block)
end
empty?() click to toggle source

Returns true if bitmask is zero (no bits are enabled) and false otherwise.

# File lib/banditry/bandit_mask.rb, line 143
def empty?
  bitmask.zero?
end
eql?(other)
Alias for: ==
hash() click to toggle source

Returns an object hash. Two Banditry::BanditMask objects have identical hashes if they have identical bitmasks and are instances of the same class.

# File lib/banditry/bandit_mask.rb, line 151
def hash
  [bitmask, self.class].hash
end
include?(*bits) click to toggle source

Returns true if every bit in bits is enabled and false otherwise. Raises ArgumentError if bits is empty or if any element in bits does not correspond to a bit that was previously defined with Banditry::BanditMask.bit.

class MyMask < Banditry::BanditMask
  bit :read, 0b001
  bit :write, 0b010
  bit :execute, 0b100
end

mask = MyMask.new 0b101

mask.include? :read           # => true
mask.include? :write          # => false
mask.include? :execute        # => true

mask.include? :read, :write   # => false
mask.include? :read, :execute # => true
# File lib/banditry/bandit_mask.rb, line 175
def include?(*bits)
  raise ArgumentError, 'wrong number of arguments (0 for 1+)' if bits.empty?
  bits.all? { |bit| bitmask & bit_value(bit) != 0 }
end
to_a()
Alias for: bits
to_i() click to toggle source

Returns integer value of current bitmask.

# File lib/banditry/bandit_mask.rb, line 40
def to_i
  bitmask
end
|(bit) click to toggle source

Returns a new instance with the current bitmask plus bit. Raises ArgumentError if bit does not correspond to a bit that was previously defined by Banditry::BanditMask.bit.

class MyMask < Banditry::BanditMask
  bit :read, 0b01
  bit :write, 0b10
end

mask = MyMask.new 0b01
mask | :write # => #<MyMask:0x007f9e0bcf5d90 @bitmask=3>
# File lib/banditry/bandit_mask.rb, line 108
def |(bit)
  self.class.new bitmask | bit_value(bit)
end

Private Instance Methods

bit_value(bit) click to toggle source

Returns the integer value for the bit named bit. Raises ArgumentError if bit has not been previously defined with Banditry::BanditMask.bit.

# File lib/banditry/bandit_mask.rb, line 185
def bit_value(bit)
  bit = bit.to_sym
  self.class.bits.fetch bit do
    raise ArgumentError, "undefined bit: #{bit.inspect}"
  end
end