Module: OllamaChat::Switches::PerformCallbacks

Included in:
DatabaseSwitch, Switch
Defined in:
lib/ollama_chat/switches.rb

Overview

A module that intercepts switch state changes to execute registered callbacks.

This module is designed to be prepended into switch classes. It captures the value before and after a change occurs, then triggers any proc associated with that specific transition in the @callbacks hash.

Instance Method Summary collapse

Instance Method Details

#perform_callbacks(before_value) ⇒ self (private)

Executes the callback proc associated with the current state transition.

Parameters:

  • before_value (Boolean)

    the state of the switch before the change

Returns:

  • (self)

    the current instance



83
84
85
86
# File 'lib/ollama_chat/switches.rb', line 83

def perform_callbacks(before_value)
  @callbacks[[ before_value, value ]]&.(before_value, value)
  self
end

#set(*a, **kw) ⇒ Object

Intercepts the set operation to trigger state-transition callbacks.

Parameters:

  • a (Array)

    positional arguments passed to the original set method

  • kw (Hash)

    keyword arguments passed to the original set method

Returns:

  • (Object)

    the result of the original set method



59
60
61
62
63
64
# File 'lib/ollama_chat/switches.rb', line 59

def set(*a, **kw)
  before_value = value
  result = super
  perform_callbacks(before_value)
  result
end

#toggle(**kw) ⇒ Object

Intercepts the toggle operation to trigger state-transition callbacks.

Parameters:

  • kw (Hash)

    keyword arguments passed to the original toggle method

Returns:

  • (Object)

    the result of the original toggle method



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

def toggle(**kw)
  before_value = value
  result = super
  perform_callbacks(before_value)
  result
end