Class: BanditMask
- Inherits:
-
Object
- Object
- BanditMask
- 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
-
#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.
-
#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:
10 11 12 |
# File 'lib/banditmask.rb', line 10 def initialize(bitmask = nil) # :nodoc: @bitmask = bitmask || 0b0 end |
Instance Attribute Details
#bitmask ⇒ Object (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 |
.bits ⇒ Object
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 |
#bits ⇒ Object 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 |
#hash ⇒ Object
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
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_i ⇒ Object
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 |