Class: OllamaChat::Switches::DatabaseSwitch

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

Overview

Manages boolean configuration states that are persisted in the database.

This class acts as a wrapper around a database attribute, allowing for toggling and setting of boolean values while ensuring that changes are immediately saved to the associated session.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from CheckSwitch

#off?, #show

Constructor Details

#initialize(chat:, msg:, attribute:, callbacks: {}) ⇒ DatabaseSwitch

Initializes a new DatabaseSwitch instance.

Parameters:

  • chat (OllamaChat::Chat)

    the chat instance providing session access

  • msg (Hash{Boolean => String})

    the message hash containing display messages

  • attribute (Symbol)

    the database attribute name to manage

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

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



155
156
157
158
159
160
# File 'lib/ollama_chat/switches.rb', line 155

def initialize(chat:, msg:, attribute:, callbacks: {})
  @chat      = chat
  @attribute = attribute
  @msg       = msg
  @callbacks = callbacks.to_h
end

Instance Attribute Details

#attributeObject (readonly)

Returns the value of attribute attribute.



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

def attribute
  @attribute
end

Instance Method Details

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

Updates the session attribute with the given value and optionally displays it.

Parameters:

  • value (Object)

    the value to be coerced to a boolean and saved

  • show (Boolean) (defaults to: false)

    whether to show the updated value

  • output (IO) (defaults to: STDOUT)

    the output stream to use when showing the value

Returns:

  • (String, nil, Boolean)

    the result of the display operation or the show flag



179
180
181
182
# File 'lib/ollama_chat/switches.rb', line 179

def set(value, show: false, output: STDOUT)
  @chat.session.update("#{attribute}": !!value)
  show && self.show(output:)
end

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

Toggles the value of a session attribute and optionally displays the new state.

Parameters:

  • show (Boolean) (defaults to: true)

    whether to show the updated state after toggling

Returns:

  • (String, nil, Boolean)

    the result of the display operation or the show flag



188
189
190
191
# File 'lib/ollama_chat/switches.rb', line 188

def toggle(show: true)
  @chat.session.update("#{attribute}": !value)
  show && self.show
end

#valueBoolean

Returns the current value of the attribute from the session.

Returns:

  • (Boolean)

    the value of the attribute



165
166
167
# File 'lib/ollama_chat/switches.rb', line 165

def value
  @chat.session.send(@attribute)
end