Module: OptionSet
- Defined in:
- lib/option_set/option_set.rb,
lib/option_set.rb,
lib/option_set/version.rb
Overview
The OptionSet module provides a way to define and manipulate bit flag options. It allows for defining methods to check, set, and toggle options represented by bit flags.
Defined Under Namespace
Classes: Error
Constant Summary collapse
- VERSION =
"0.1.0"
Instance Attribute Summary collapse
-
#value ⇒ Integer
readonly
The current bit value.
Class Method Summary collapse
-
.included(base) ⇒ void
Called when this module is included in another class.
Instance Method Summary collapse
-
#add(option) ⇒ self
Add an option to the set.
-
#clear ⇒ self
Clear all options in the set.
-
#contains?(option) ⇒ Boolean
Check if the option set contains the specified option.
-
#initialize(value = 0) ⇒ Object
Initialize a new option set.
-
#remove(option) ⇒ self
Remove an option from the set.
-
#toggle(option) ⇒ self
Toggle an option in the set.
-
#values ⇒ Array<Symbol>
Get the symbolic names of all set options.
Instance Attribute Details
#value ⇒ Integer (readonly)
Returns The current bit value.
121 122 123 |
# File 'lib/option_set/option_set.rb', line 121 def value @value end |
Class Method Details
.included(base) ⇒ void
This method returns an undefined value.
Called when this module is included in another class
30 31 32 |
# File 'lib/option_set/option_set.rb', line 30 def self.included(base) base.extend(ClassMethods) end |
Instance Method Details
#add(option) ⇒ self
Add an option to the set
143 144 145 146 147 |
# File 'lib/option_set/option_set.rb', line 143 def add(option) option = option.value if option.is_a?(self.class) @value |= option self end |
#clear ⇒ self
Clear all options in the set
172 173 174 175 |
# File 'lib/option_set/option_set.rb', line 172 def clear @value = 0 self end |
#contains?(option) ⇒ Boolean
Check if the option set contains the specified option
134 135 136 137 |
# File 'lib/option_set/option_set.rb', line 134 def contains?(option) option = option.value if option.is_a?(self.class) (@value & option) == option end |
#initialize(value = 0) ⇒ Object
Initialize a new option set
126 127 128 |
# File 'lib/option_set/option_set.rb', line 126 def initialize(value = 0) @value = value end |
#remove(option) ⇒ self
Remove an option from the set
153 154 155 156 157 |
# File 'lib/option_set/option_set.rb', line 153 def remove(option) option = option.value if option.is_a?(self.class) @value &= ~option self end |
#toggle(option) ⇒ self
Toggle an option in the set
163 164 165 166 167 |
# File 'lib/option_set/option_set.rb', line 163 def toggle(option) option = option.value if option.is_a?(self.class) @value ^= option self end |
#values ⇒ Array<Symbol>
Get the symbolic names of all set options
180 181 182 183 184 |
# File 'lib/option_set/option_set.rb', line 180 def values option_values = self.class.send(:option_values) option_values.select { |k, _| contains?(k) } .values.map(&:upcase) end |