Class: BanditMask
- Inherits:
-
Object
- Object
- BanditMask
- Defined in:
- lib/banditmask.rb,
lib/banditmask/version.rb,
lib/banditmask/banditry.rb
Defined Under Namespace
Modules: Banditry
Constant Summary collapse
- VERSION =
'0.2.0'
Instance Attribute Summary collapse
-
#bitmask ⇒ Object
readonly
Returns the value of attribute bitmask.
Class Method Summary collapse
-
.bit(name, value) ⇒ Object
Maps
nametovaluein 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. -
#bits ⇒ Object
(also: #to_a)
Returns an array of names of the currently enabled bits.
-
#include?(bit) ⇒ Boolean
Returns true if
bitis among the currently enabled bits. -
#initialize(bitmask = 0b0) ⇒ BanditMask
constructor
:nodoc:.
-
#to_i ⇒ Object
Returns integer value of current bitmask.
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
#bitmask ⇒ Object (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 |
.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 }
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 declared with BanditMask.bit.
class BanditMask
bit :read, 0b01
bit :write, 0b10
end
mask = BanditMask.new
mask << :read << :write
69 70 71 72 |
# File 'lib/banditmask.rb', line 69 def <<(bit) @bitmask |= bit_value(bit) self end |
#bits ⇒ Object Also known as: to_a
Returns an array of names of the currently enabled bits.
class BanditMask
bit :read, 0b01
bit :write, 0b10
end
mask = BanditMask.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 |
#include?(bit) ⇒ Boolean
Returns true if bit is among the currently enabled bits. Returns false otherwise. Raises ArgumentError if bit does not correspond to a bit that was previously declared with BanditMask.bit.
class BanditMask
bit :read, 0b01
bit :write, 0b10
end
mask = BanditMask.new 0b01
mask.include? :read # => true
mask.include? :write # => false
87 88 89 |
# File 'lib/banditmask.rb', line 87 def include?(bit) bitmask & bit_value(bit) != 0 end |
#to_i ⇒ Object
Returns integer value of current bitmask.
38 39 40 |
# File 'lib/banditmask.rb', line 38 def to_i bitmask end |