Class: OllamaChat::StateSelectors::StateSelector
- Inherits:
-
Object
- Object
- OllamaChat::StateSelectors::StateSelector
- 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
-
#selected ⇒ Object
The selected reader returns the currently selected state of the switch.
Instance Method Summary collapse
-
#allow_empty? ⇒ TrueClass, FalseClass
The allow_empty? method checks if the switch is allowed to be empty.
-
#choose ⇒ nil
The choose method presents a menu to select from available states.
-
#initialize(name:, states:, default: nil, off: nil, allow_empty: false) ⇒ StateSelector
constructor
Initializes a new StateSelector with the given configuration.
-
#off? ⇒ TrueClass, FalseClass
The off? method checks if the current state is in the off set.
-
#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.
-
#show ⇒ Object
The show method outputs the current value of the state selector.
-
#to_s ⇒ String
The to_s method returns the string representation of the selected state.
Constructor Details
#initialize(name:, states:, default: nil, off: nil, allow_empty: false) ⇒ StateSelector
Initializes a new StateSelector with the given configuration.
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
#selected ⇒ Object
The selected reader returns the currently selected state of the switch.
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.
65 66 67 |
# File 'lib/ollama_chat/state_selectors.rb', line 65 def allow_empty? !!@allow_empty end |
#choose ⇒ nil
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.
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.
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.
82 83 84 |
# File 'lib/ollama_chat/state_selectors.rb', line 82 def on? !off? end |
#show ⇒ Object
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_s ⇒ String
The to_s method returns the string representation of the selected state.
116 117 118 |
# File 'lib/ollama_chat/state_selectors.rb', line 116 def to_s @selected.to_s end |