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.

Instance Attribute Summary

Attributes included from Common

#allow_empty, #default, #name, #off, #states

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

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>)

    the list of valid states this selector can have

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

    the default state (retrieved from the session)

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

    the list of states that should be considered "off"

  • allow_empty (Boolean) (defaults to: false)

    whether the selector is allowed to be empty



188
189
190
191
192
193
194
195
196
# File 'lib/ollama_chat/state_selectors.rb', line 188

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

Instance Method Details

#selectedObject

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

Returns:

  • (Object)

    the currently selected state value



201
202
203
# File 'lib/ollama_chat/state_selectors.rb', line 201

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



211
212
213
214
215
216
217
218
# File 'lib/ollama_chat/state_selectors.rb', line 211

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