Class: OllamaChat::StateSelectors::StateSelector

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

Overview

A state selector that manages configurable states with selection and display capabilities.

Instance Attribute Summary collapse

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(name:, states:, default: nil, off: nil, allow_empty: false) ⇒ StateSelector

Initializes a new StateSelector with the given configuration.

Parameters:

  • 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 to select (must be one of states)

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

    The list of states that should be considered "off"

Raises:

  • (ArgumentError)

    If states is empty or default is not in states



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/ollama_chat/state_selectors.rb', line 132

def initialize(name:, states:, default: nil, off: nil, allow_empty: false)
  @name        = name.to_s
  @states      = Set.new(states.map(&:to_s))
  @allow_empty = allow_empty
  unless allow_empty
    states.empty? and raise ArgumentError, 'states cannot be empty'
  end
  if default
    @default  = default.to_s
    unless allow_empty?
      states.member?(@default) or raise ArgumentError,
        "default has to be one of #{states.to_a * ', '}."
    end
    @selected = @default
  else
    @selected = states.first
  end
  @off = Array(off)
end

Instance Attribute Details

#selectedObject

The selected reader returns the currently selected state of the switch.

Returns:

  • (Object)

    the currently selected state value



155
156
157
# File 'lib/ollama_chat/state_selectors.rb', line 155

def selected
  @selected
end