Class: OllamaChat::StateSelectors::StateSelector

Inherits:
Object
  • Object
show all
Includes:
Term::ANSIColor
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

Instance Method Summary collapse

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



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/ollama_chat/state_selectors.rb', line 21

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



44
45
46
# File 'lib/ollama_chat/state_selectors.rb', line 44

def selected
  @selected
end

Instance Method Details

#allow_empty?TrueClass, FalseClass

The allow_empty? method checks if the switch is allowed to be empty.

Returns:

  • (TrueClass, FalseClass)

    true if the switch is allowed to be empty, false otherwise



65
66
67
# File 'lib/ollama_chat/state_selectors.rb', line 65

def allow_empty?
  !!@allow_empty
end

#choosenil

The choose method presents a menu to select from available states.

This method displays the available states to the user and allows them to select one. It handles the user’s choice by updating the selected state or exiting the chooser if the user selects ‘[EXIT]’ or cancels the selection.

Returns:

  • (nil)

    This method does not return a value; it updates the instance variable @selected based on user input.



94
95
96
97
98
99
100
101
102
# File 'lib/ollama_chat/state_selectors.rb', line 94

def choose
  states = @states + [ '[EXIT]' ]
  case chosen = OllamaChat::Utils::Chooser.choose(states)
  when '[EXIT]', nil
    STDOUT.puts "Exiting chooser."
  when
    @selected = chosen
  end
end

#off?TrueClass, FalseClass

The off? method checks if the current state is in the off set.

Returns:

  • (TrueClass, FalseClass)

    true if the selected state is in the off set, false otherwise



73
74
75
# File 'lib/ollama_chat/state_selectors.rb', line 73

def off?
  @off.member?(@selected)
end

#on?TrueClass, FalseClass

The on? method checks if the switch is in the on state, returning true if it is enabled and false if it is disabled.

Returns:

  • (TrueClass, FalseClass)

    true if the switch is on, false if it is off



82
83
84
# File 'lib/ollama_chat/state_selectors.rb', line 82

def on?
  !off?
end

#showObject

The show method outputs the current value of the state selector.

This method displays the name of the state selector along with its currently selected state in a formatted message to standard output.



108
109
110
# File 'lib/ollama_chat/state_selectors.rb', line 108

def show
  STDOUT.puts "#{@name} is #{bold(to_s)}."
end

#to_sString

The to_s method returns the string representation of the selected state.

Returns:

  • (String)

    the string representation of the currently selected state



116
117
118
# File 'lib/ollama_chat/state_selectors.rb', line 116

def to_s
  @selected.to_s
end