Class: OllamaChat::Switches::Switch

Inherits:
Object
  • Object
show all
Includes:
CheckSwitch, PerformCallbacks
Defined in:
lib/ollama_chat/switches.rb

Overview

A switch class that manages boolean state with toggle and set functionality.

The Switch class provides a simple way to manage boolean configuration options with methods to toggle, set, and query the current state. It includes messaging capabilities to provide a message when the state changes.

Examples:

Creating and using a switch

switch = Switch.new(value: false, msg: { true => 'Enabled', false => 'Disabled' })
switch.toggle  # Turns the switch on
switch.value   # Returns true
switch.off?    # Returns false
switch.on?     # Returns true

Constant Summary collapse

UNSET =
Object.new.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from CheckSwitch

#off?, #show

Methods included from PerformCallbacks

#perform_callbacks

Constructor Details

#initialize(msg:, value:, callbacks: {}) ⇒ Switch

Initializes a new Switch instance.

Parameters:

  • msg (Hash{Boolean => String}) —

    a hash containing true and false messages

  • value (Object, Proc) —

    the initial state of the switch. A plain value is coerced to a boolean and wrapped in an internal closure; a callable is used directly and must be invokable both bare (read) and with an explicit boolean (write). If the callable responds to arity, it must return -1 (a TypeError is raised otherwise); callables without an arity method are accepted as-is.

  • callbacks (Hash{[Boolean, Boolean] => Proc}) (defaults to: {}) —

    optional mapping of state transitions to callback procs. Keys are [old_value, new_value].

Raises:

  • (TypeError) —

    if value is a callable that responds to arity and the arity is not -1



117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/ollama_chat/switches.rb', line 117

def initialize(msg:, value:, callbacks: {})
  if value.respond_to?(:call)
    if arity = value.ask_and_send(:arity)
      arity != -1 and raise TypeError, 'value Proc requires arity -1'
    end
    @value = value
  else
    @value = adhoc_closure(value)
  end
  @msg       = msg
  @callbacks = callbacks.to_h
end

Instance Attribute Details

#value ⇒ Object (readonly)



153
154
155
# File 'lib/ollama_chat/switches.rb', line 153

def value
  @value.()
end

Instance Method Details

#adhoc_closure(original_value) ⇒ Proc (private)

Wraps a plain (non-callable) value in a closure so that every switch state is stored and accessed uniformly via a Proc.

The returned closure reads the coerced (!!) boolean state when called with no argument (or the UNSET sentinel) and rebinds the captured original_value when called with an explicit value.

Parameters:

  • original_value (Object) —

    the initial value to be coerced to a boolean on read

Returns:

  • (Proc) —

    a closure that reads or writes the switch state



140
141
142
143
144
145
146
147
148
# File 'lib/ollama_chat/switches.rb', line 140

def adhoc_closure(original_value)
  -> new_value = UNSET {
    if new_value == UNSET
      !!original_value
    else
      original_value = new_value
    end
  }
end

#set(value, show: false, output: STDOUT) ⇒ Object

Assigns a boolean value to the switch and optionally displays the result.

Parameters:

  • value (Object) —

    the value to be coerced to a boolean and assigned

  • show (Boolean) (defaults to: false) —

    determines whether to display the status message

  • output (IO) (defaults to: STDOUT) —

    the output stream to write the message to



162
163
164
165
166
# File 'lib/ollama_chat/switches.rb', line 162

def set(value, show: false, output: STDOUT)
  @value.(value)
  show && self.show(output:)
  nil
end

#toggle(show: true) ⇒ String, ...

Toggles the current boolean value and optionally displays the result.

Parameters:

  • show (Boolean) (defaults to: true) —

    determines whether to show the value after toggling

Returns:

  • (String, nil, Boolean) —

    the result of the display operation or the show flag



172
173
174
175
# File 'lib/ollama_chat/switches.rb', line 172

def toggle(show: true)
  @value.(!@value.())
  show && self.show
end