Class: OllamaChat::StateSelectors::DatabaseStateSelector

Inherits:
Object
  • Object
show all
Includes:
Common
Defined in:
lib/ollama_chat/state_selectors.rb

Overview

A state selector that manages configurable states by reading from and writing to a chat session's attribute.

Constant Summary

Constants included from Feedback

Feedback::PREFIXES

Instance Attribute Summary

Attributes included from Common

#allow_empty, #default, #name

Attributes included from Utils::Chooser

#current_search_state Stores the

Instance Method Summary collapse

Methods included from Common

#allow_empty?, #choose, #off?, #on?, #show, #to_s

Methods included from Utils::Chooser

#choose_entry, #choose_with_state

Methods included from Feedback

#feedback

Constructor Details

#initialize(chat:, attribute:, name:, states:, default: nil, off: nil, allow_empty: false) ⇒ DatabaseStateSelector

Initializes a new DatabaseStateSelector.

Parameters:

  • chat (OllamaChat::Chat) —

    the chat instance to interact with

  • attribute (Symbol) —

    the attribute name in the session to manage

  • name (String) —

    the name of the state selector for display purposes

  • states (Array<String>, Proc) —

    the list of valid states or a lambda ->(chat) { [...] } that resolves them lazily

  • default (String, nil) (defaults to: nil) —

    the default state (retrieved from the session)

  • off (Array<String>, Proc, nil) (defaults to: nil) —

    the list of "off" states or a lambda ->(chat) { [...] } that resolves them lazily

  • allow_empty (Boolean) (defaults to: false) —

    whether the selector is allowed to be empty



195
196
197
198
199
200
201
202
203
# File 'lib/ollama_chat/state_selectors.rb', line 195

def initialize(chat:, attribute:, name:, states:, default: nil, off: nil, allow_empty: false)
  @chat        = chat
  @attribute   = attribute
  @name        = name
  @states      = states
  @off         = off
  @default      = @chat.session.send(@attribute)
  @allow_empty  = allow_empty
end

Instance Method Details

#off ⇒ Array<String>?

The off reader returns the "off" states for this selector.

If the initial off-states were provided as a lambda, it is re-evaluated on every access, mirroring the #states behavior.

Returns:

  • (Array<String>, nil) —

    the list of "off" states



222
223
224
# File 'lib/ollama_chat/state_selectors.rb', line 222

def off
  @off.ask_and_send_or_self(:call, @chat)
end

#selected ⇒ Object

The selected reader returns the current value of the attribute from the chat session.

Returns:

  • (Object) —

    the currently selected state value



229
230
231
# File 'lib/ollama_chat/state_selectors.rb', line 229

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

#selected=(value) ⇒ Object

The selected= method sets the attribute value in the chat session.

Parameters:

  • value (Object) —

    the value to be converted to a string and set as the attribute

Raises:

  • (ArgumentError) —

    if the provided value is not one of the valid states



239
240
241
242
243
244
245
246
# File 'lib/ollama_chat/state_selectors.rb', line 239

def selected=(value)
  value = value.to_s
  unless allow_empty?
    states.member?(value) or raise ArgumentError,
      "value has to be one of #{states.to_a * ', '}."
  end
  @chat.session.update("#@attribute": value)
end

#states ⇒ Array<String>

The states reader returns the valid states for this selector.

If the initial states were provided as a lambda, it is re-evaluated on every access, so the returned set always reflects the current model's capabilities without explicit invalidation.

Returns:

  • (Array<String>) —

    the list of valid states



212
213
214
# File 'lib/ollama_chat/state_selectors.rb', line 212

def states
  @states.ask_and_send_or_self(:call, @chat)
end