Class: BanditMask

Inherits:
Object
  • Object
show all
Defined in:
lib/banditmask.rb,
lib/banditmask/version.rb,
lib/banditmask/banditry.rb

Defined Under Namespace

Modules: Banditry Classes: MethodCollisionError

Constant Summary collapse

VERSION =
'0.2.1'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(bitmask = 0b0) ⇒ BanditMask

:nodoc:


7
8
9
# File 'lib/banditmask.rb', line 7

def initialize(bitmask = 0b0) # :nodoc:
  @bitmask = bitmask
end

Instance Attribute Details

#bitmaskObject (readonly)

Returns the value of attribute bitmask


5
6
7
# File 'lib/banditmask.rb', line 5

def bitmask
  @bitmask
end

Class Method Details

.bit(name, value) ⇒ Object

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

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

32
33
34
# File 'lib/banditmask.rb', line 32

def self.bit(name, value)
  bits.update name => value
end

.bitsObject

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

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

MyMask.bits # => { :read => 1, :write => 2 }

20
21
22
# File 'lib/banditmask.rb', line 20

def self.bits
  @bits ||= {}
end

Instance Method Details

#<<(bit) ⇒ Object

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 BanditMask.bit.

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

mask = MyMask.new       # => #<MyMask:0x007f9ebd44ae40 @bitmask=0>
mask << :read << :write # => #<MyMask:0x007f9ebd44ae40 @bitmask=3>

69
70
71
72
# File 'lib/banditmask.rb', line 69

def <<(bit)
  @bitmask |= bit_value(bit)
  self
end

#==(other) ⇒ Object Also known as: eql?

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

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

class MyOtherMask < 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

112
113
114
# File 'lib/banditmask.rb', line 112

def ==(other)
  other.class == self.class && other.bitmask == bitmask
end

#bitsObject Also known as: to_a

Returns an array of names of the currently enabled bits.

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

mask = MyMask.new 0b01
mask.bits # => [:read]

52
53
54
# File 'lib/banditmask.rb', line 52

def bits
  self.class.bits.select { |bit, _| include? bit }.keys
end

#hashObject

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


121
122
123
# File 'lib/banditmask.rb', line 121

def hash
  [bitmask, self.class].hash
end

#include?(*bits) ⇒ Boolean

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 BanditMask.bit.

class MyMask < 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

Returns:

  • (Boolean)

Raises:

  • (ArgumentError)

144
145
146
147
# File 'lib/banditmask.rb', line 144

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_iObject

Returns integer value of current bitmask.


38
39
40
# File 'lib/banditmask.rb', line 38

def to_i
  bitmask
end

#|(bit) ⇒ Object

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 BanditMask.bit.

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

mask = MyMask.new 0b01
mask | :write # => #<MyMask:0x007f9e0bcf5d90 @bitmask=3>

86
87
88
# File 'lib/banditmask.rb', line 86

def |(bit)
  self.class.new bitmask | bit_value(bit)
end