Class: Banditry::BanditMask
- Inherits:
-
Object
- Object
- Banditry::BanditMask
- Includes:
- Enumerable
- Defined in:
- lib/banditry/bandit_mask.rb
Instance Attribute Summary collapse
-
#bitmask ⇒ Object
readonly
Returns the value of attribute bitmask.
Class Method Summary collapse
-
.bit(name, value) ⇒ Object
Maps
name
tovalue
in the global list of defined bits. -
.bits ⇒ Object
Returns a Hash mapping all defined names to their respective bits.
Instance Method Summary collapse
-
#<<(bit) ⇒ Object
Enables the bit named
bit
. -
#==(other) ⇒ Object
(also: #eql?)
Returns
true
ifother
is an instance of the same class asself
and they have the same bitmask. -
#bits ⇒ Object
(also: #to_a)
Returns an array of names of the currently enabled bits.
-
#each(&block) ⇒ Object
Delegates to #bits.
-
#empty? ⇒ Boolean
Returns
true
if bitmask is zero (no bits are enabled) andfalse
otherwise. -
#hash ⇒ Object
Returns an object hash.
-
#include?(*bits) ⇒ Boolean
Returns true if every bit in
bits
is enabled and false otherwise. -
#initialize(bitmask = nil) ⇒ BanditMask
constructor
:nodoc:.
-
#to_i ⇒ Object
Returns integer value of current bitmask.
-
#|(bit) ⇒ Object
Returns a new instance with the current bitmask plus
bit
.
Constructor Details
#initialize(bitmask = nil) ⇒ BanditMask
:nodoc:
9 10 11 |
# File 'lib/banditry/bandit_mask.rb', line 9 def initialize(bitmask = nil) # :nodoc: @bitmask = bitmask || 0b0 end |
Instance Attribute Details
#bitmask ⇒ Object (readonly)
Returns the value of attribute bitmask.
7 8 9 |
# File 'lib/banditry/bandit_mask.rb', line 7 def bitmask @bitmask end |
Class Method Details
.bit(name, value) ⇒ Object
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
34 35 36 |
# File 'lib/banditry/bandit_mask.rb', line 34 def self.bit(name, value) bits.update name.to_sym => value end |
.bits ⇒ Object
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 }
22 23 24 |
# File 'lib/banditry/bandit_mask.rb', line 22 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 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>
91 92 93 94 |
# File 'lib/banditry/bandit_mask.rb', line 91 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 < 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
134 135 136 |
# File 'lib/banditry/bandit_mask.rb', line 134 def ==(other) other.class == self.class && other.bitmask == bitmask end |
#bits ⇒ Object Also known as: to_a
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]
54 55 56 |
# File 'lib/banditry/bandit_mask.rb', line 54 def bits self.class.bits.select { |bit, _| include? bit }.keys end |
#each(&block) ⇒ Object
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: ...>
74 75 76 |
# File 'lib/banditry/bandit_mask.rb', line 74 def each(&block) bits.each(&block) end |
#empty? ⇒ Boolean
Returns true
if bitmask is zero (no bits are enabled) and false
otherwise.
143 144 145 |
# File 'lib/banditry/bandit_mask.rb', line 143 def empty? bitmask.zero? end |
#hash ⇒ Object
Returns an object hash. Two Banditry::BanditMask objects have identical hashes if they have identical bitmasks and are instances of the same class.
151 152 153 |
# File 'lib/banditry/bandit_mask.rb', line 151 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 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
175 176 177 178 |
# 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_i ⇒ Object
Returns integer value of current bitmask.
40 41 42 |
# File 'lib/banditry/bandit_mask.rb', line 40 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 Banditry::BanditMask.bit.
class MyMask < Banditry::BanditMask
bit :read, 0b01
bit :write, 0b10
end
mask = MyMask.new 0b01
mask | :write # => #<MyMask:0x007f9e0bcf5d90 @bitmask=3>
108 109 110 |
# File 'lib/banditry/bandit_mask.rb', line 108 def |(bit) self.class.new bitmask | bit_value(bit) end |