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 feedback 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:) ⇒ void

The initialize method sets up the switch with a default value and message.

Parameters:

  • msg (Hash)

    a hash containing true and false messages

  • value (Object)

    the default state of the switch



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

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

Instance Attribute Details

#valueObject (readonly)

The value reader returns the current value of the attribute.



77
78
79
# File 'lib/ollama_chat/switches.rb', line 77

def value
  @value
end

Instance Method Details

#set(value, show: false) ⇒ Object

The set method assigns a boolean value to the instance variable @value and optionally displays it.

assigned value after setting

Parameters:

  • value (Object)

    the value to be converted to a boolean and

  • show (TrueClass, FalseClass) (defaults to: false)

    determines whether to display the



86
87
88
89
# File 'lib/ollama_chat/switches.rb', line 86

def set(value, show: false)
  @value = !!value
  show && self.show
end

#toggle(show: true) ⇒ Object

The toggle method switches the current value of the instance variable and optionally displays it.

value after toggling

Parameters:

  • show (TrueClass, FalseClass) (defaults to: true)

    determines whether to show the



96
97
98
99
# File 'lib/ollama_chat/switches.rb', line 96

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