Class: OllamaChat::Switches::DatabaseSwitch
- Inherits:
-
Object
- Object
- OllamaChat::Switches::DatabaseSwitch
- 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
-
#attribute ⇒ Object
readonly
Returns the value of attribute attribute.
Instance Method Summary collapse
-
#initialize(chat:, msg:, attribute:, callbacks: {}) ⇒ DatabaseSwitch
constructor
Initializes a new DatabaseSwitch instance.
-
#set(value, show: false, output: STDOUT) ⇒ Object
Updates the session attribute with the given value and optionally displays it.
-
#toggle(show: true) ⇒ String, ...
Toggles the value of a session attribute and optionally displays the new state.
-
#value ⇒ Boolean
Returns the current value of the attribute from the session.
Methods included from CheckSwitch
Methods included from PerformCallbacks
Constructor Details
#initialize(chat:, msg:, attribute:, callbacks: {}) ⇒ DatabaseSwitch
Initializes a new DatabaseSwitch instance.
194 195 196 197 198 199 |
# File 'lib/ollama_chat/switches.rb', line 194 def initialize(chat:, msg:, attribute:, callbacks: {}) @chat = chat @attribute = attribute @msg = msg @callbacks = callbacks.to_h end |
Instance Attribute Details
#attribute ⇒ Object (readonly)
Returns the value of attribute attribute.
210 211 212 |
# File 'lib/ollama_chat/switches.rb', line 210 def attribute @attribute end |
Instance Method Details
#set(value, show: false, output: STDOUT) ⇒ Object
Updates the session attribute with the given value and optionally displays it.
217 218 219 220 221 |
# File 'lib/ollama_chat/switches.rb', line 217 def set(value, show: false, output: STDOUT) @chat.session.update("#{attribute}": !!value) show && self.show(output:) nil end |
#toggle(show: true) ⇒ String, ...
Toggles the value of a session attribute and optionally displays the new state.
227 228 229 230 |
# File 'lib/ollama_chat/switches.rb', line 227 def toggle(show: true) @chat.session.update("#{attribute}": !value) show && self.show end |
#value ⇒ Boolean
Returns the current value of the attribute from the session.
204 205 206 |
# File 'lib/ollama_chat/switches.rb', line 204 def value @chat.session.send(@attribute) end |