Class: OllamaChat::Switches::Switch

Inherits:
Object
  • Object
show all
Includes:
CheckSwitch
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:) ⇒ 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)



65
66
67
68
# File 'lib/ollama_chat/switches.rb', line 65

def initialize(msg:, value:)
  @value = !!value
  @msg   = msg
end

Instance Attribute Details

#valueObject (readonly)

Returns the value of attribute value.



72
73
74
# File 'lib/ollama_chat/switches.rb', line 72

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



80
81
82
83
# File 'lib/ollama_chat/switches.rb', line 80

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



89
90
91
92
# File 'lib/ollama_chat/switches.rb', line 89

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