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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from CheckSwitch

#off?, #show

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)

    the initial state of the switch (coerced to boolean)

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

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



108
109
110
111
112
# File 'lib/ollama_chat/switches.rb', line 108

def initialize(msg:, value:, callbacks: {})
  @value     = !!value
  @msg       = msg
  @callbacks = callbacks.to_h
end

Instance Attribute Details

#valueObject (readonly)

Returns the value of attribute value.



116
117
118
# File 'lib/ollama_chat/switches.rb', line 116

def value
  @value
end

Instance Method Details

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

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

Returns:

  • (String, nil, Boolean)

    the result of the display operation or the show flag



124
125
126
127
# File 'lib/ollama_chat/switches.rb', line 124

def set(value, show: false, output: STDOUT)
  @value = !!value
  show && self.show(output:)
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



133
134
135
136
# File 'lib/ollama_chat/switches.rb', line 133

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