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.3.1'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(bitmask = nil) ⇒ BanditMask

:nodoc:



10
11
12
# File 'lib/banditmask.rb', line 10

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

Instance Attribute Details

#bitmaskObject (readonly)

Returns the value of attribute bitmask.



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

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


35
36
37
# File 'lib/banditmask.rb', line 35

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 }


23
24
25
# File 'lib/banditmask.rb', line 23

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>


72
73
74
75
# File 'lib/banditmask.rb', line 72

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


115
116
117
# File 'lib/banditmask.rb', line 115

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]


55
56
57
# File 'lib/banditmask.rb', line 55

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.



124
125
126
# File 'lib/banditmask.rb', line 124

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)


147
148
149
150
# File 'lib/banditmask.rb', line 147

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.



41
42
43
# File 'lib/banditmask.rb', line 41

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>


89
90
91
# File 'lib/banditmask.rb', line 89

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