Class: OllamaChat::Switches::Switch
- Inherits:
-
Object
- Object
- OllamaChat::Switches::Switch
- 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.
Constant Summary collapse
- UNSET =
Object.new.freeze
Instance Attribute Summary collapse
- #value ⇒ Object readonly
Instance Method Summary collapse
-
#adhoc_closure(original_value) ⇒ Proc
private
Wraps a plain (non-callable) value in a closure so that every switch state is stored and accessed uniformly via a
Proc. -
#initialize(msg:, value:, callbacks: {}) ⇒ Switch
constructor
Initializes a new Switch instance.
-
#set(value, show: false, output: STDOUT) ⇒ Object
Assigns a boolean value to the switch and optionally displays the result.
-
#toggle(show: true) ⇒ String, ...
Toggles the current boolean value and optionally displays the result.
Methods included from CheckSwitch
Methods included from PerformCallbacks
Constructor Details
#initialize(msg:, value:, callbacks: {}) ⇒ Switch
Initializes a new Switch instance.
117 118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/ollama_chat/switches.rb', line 117 def initialize(msg:, value:, callbacks: {}) if value.respond_to?(:call) if arity = value.ask_and_send(:arity) arity != -1 and raise TypeError, 'value Proc requires arity -1' end @value = value else @value = adhoc_closure(value) end @msg = msg @callbacks = callbacks.to_h end |
Instance Attribute Details
#value ⇒ Object (readonly)
153 154 155 |
# File 'lib/ollama_chat/switches.rb', line 153 def value @value.() end |
Instance Method Details
#adhoc_closure(original_value) ⇒ Proc (private)
Wraps a plain (non-callable) value in a closure so that every switch
state is stored and accessed uniformly via a Proc.
The returned closure reads the coerced (!!) boolean state when called
with no argument (or the UNSET sentinel) and rebinds the captured
original_value when called with an explicit value.
140 141 142 143 144 145 146 147 148 |
# File 'lib/ollama_chat/switches.rb', line 140 def adhoc_closure(original_value) -> new_value = UNSET { if new_value == UNSET !!original_value else original_value = new_value end } end |
#set(value, show: false, output: STDOUT) ⇒ Object
Assigns a boolean value to the switch and optionally displays the result.
162 163 164 165 166 |
# File 'lib/ollama_chat/switches.rb', line 162 def set(value, show: false, output: STDOUT) @value.(value) show && self.show(output:) nil end |
#toggle(show: true) ⇒ String, ...
Toggles the current boolean value and optionally displays the result.
172 173 174 175 |
# File 'lib/ollama_chat/switches.rb', line 172 def toggle(show: true) @value.(!@value.()) show && self.show end |