Class: Plutonium::Lib::BitFlags
- Inherits:
-
Object
- Object
- Plutonium::Lib::BitFlags
- Defined in:
- lib/plutonium/lib/bit_flags.rb
Overview
The BitFlags class provides a convenient way to work with bit flags. It allows setting, checking, and extracting flags using a more readable interface. It supports both symbols and strings as flags.
Instance Attribute Summary collapse
-
#flags ⇒ Array<Symbol>
readonly
An array of all defined flags.
-
#indices ⇒ Array<Integer>
readonly
An array of all defined bit values.
Instance Method Summary collapse
-
#[](*flags) ⇒ Integer
Returns the bit value for the given flags.
-
#bits(*flags) ⇒ Integer
Calculates the combined bit value for the given flags.
-
#extract(value) ⇒ Array<Symbol>
Extracts the flags that are set in the given value.
-
#initialize(*flags) ⇒ BitFlags
constructor
Initializes a new BitFlags object with the given flags.
-
#set!(value, *flags) ⇒ Integer
Sets the specified flags in the given value.
-
#set?(value, *flags) ⇒ Boolean
Checks if the given value has all the specified flags set.
-
#sum ⇒ Integer
Calculates the sum of all bit values.
Constructor Details
#initialize(*flags) ⇒ BitFlags
Initializes a new BitFlags object with the given flags.
26 27 28 29 30 31 |
# File 'lib/plutonium/lib/bit_flags.rb', line 26 def initialize(*flags) @flags = normalize_flags(flags).uniq.freeze @indices = @flags.each_index.map { |index| 1 << index }.freeze @map = @flags.zip(@indices).to_h.freeze @all_bits = calculate_all_bits end |
Instance Attribute Details
#flags ⇒ Array<Symbol> (readonly)
Returns An array of all defined flags.
16 17 18 |
# File 'lib/plutonium/lib/bit_flags.rb', line 16 def flags @flags end |
#indices ⇒ Array<Integer> (readonly)
Returns An array of all defined bit values.
19 20 21 |
# File 'lib/plutonium/lib/bit_flags.rb', line 19 def indices @indices end |
Instance Method Details
#[](*flags) ⇒ Integer
Returns the bit value for the given flags.
77 78 79 |
# File 'lib/plutonium/lib/bit_flags.rb', line 77 def [](*flags) bits(*flags) end |
#bits(*flags) ⇒ Integer
Calculates the combined bit value for the given flags.
87 88 89 90 |
# File 'lib/plutonium/lib/bit_flags.rb', line 87 def bits(*flags) normalized_flags = normalize_flags(flags) normalized_flags.sum { |flag| @map[flag] || 0 } end |
#extract(value) ⇒ Array<Symbol>
Extracts the flags that are set in the given value.
66 67 68 69 |
# File 'lib/plutonium/lib/bit_flags.rb', line 66 def extract(value) value &= @all_bits @map.select { |_, bit| value & bit != 0 }.keys end |
#set!(value, *flags) ⇒ Integer
Sets the specified flags in the given value.
54 55 56 57 58 |
# File 'lib/plutonium/lib/bit_flags.rb', line 54 def set!(value, *flags) normalized_flags = normalize_flags(flags) bits_to_set = bits(*normalized_flags) value | bits_to_set end |
#set?(value, *flags) ⇒ Boolean
Checks if the given value has all the specified flags set.
40 41 42 43 44 45 |
# File 'lib/plutonium/lib/bit_flags.rb', line 40 def set?(value, *flags) normalized_flags = normalize_flags(flags) return false if normalized_flags.any? { |flag| !@map.key?(flag) } check = bits(*normalized_flags) value & check == check end |
#sum ⇒ Integer
Calculates the sum of all bit values.
95 96 97 |
# File 'lib/plutonium/lib/bit_flags.rb', line 95 def sum @all_bits end |