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.

Examples:

Basic usage

class Permissions
  include OptionSet
  define_options :read, :write, :delete
end

perms = Permissions.new
perms.read!       # Set read flag
perms.write = true # Set write flag
perms.read?       # => true
perms.contains?(Permissions::DELETE) # => false
perms.values      # => ["READ", "WRITE"]opt

Using a hash with custom bit values

class Features
  include OptionSet
  define_options basic: 1, premium: 2, enterprise: 4
end

Defined Under Namespace

Classes: Error

Constant Summary collapse

VERSION =
"0.1.0"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#valueInteger (readonly)

Returns The current bit value.

Returns:

  • (Integer)

    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

Parameters:

  • base (Class)

    The class including this module



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

Parameters:

  • option (Integer, self)

    The bit value to add

Returns:

  • (self)

    Returns self for method chaining



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

#clearself

Clear all options in the set

Returns:

  • (self)

    Returns self for method chainin



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

Parameters:

  • option (Integer, self)

    The bit value to check

Returns:

  • (Boolean)

    true if the option is set, false otherwise



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

Parameters:

  • value (Integer) (defaults to: 0)

    Initial bit value (default: 0)



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

Parameters:

  • option (Integer, self)

    The bit value to remove

Returns:

  • (self)

    Returns self for method chaining



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

Parameters:

  • option (Integer)

    The bit value to toggle

Returns:

  • (self)

    Returns self for method chaining



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

#valuesArray<Symbol>

Get the symbolic names of all set options

Returns:

  • (Array<Symbol>)

    Array of uppercase option names that are enabled



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