class TTY::Prompt::Choice
An immutable representation of a single choice option from select menu
@api public
Attributes
disabled[R]
The text to display for disabled choice
@api public
key[R]
The keyboard key to activate this choice
@api public
name[R]
The label name
@api public
Public Class Methods
convert_array(val)
click to toggle source
Convert an array into choice
@param [Array<Object>]
@return [Choice]
@api public
# File lib/tty/prompt/choice.rb, line 48 def self.convert_array(val) name, value, options = *val if name.is_a?(Hash) convert_hash(name) elsif val.size == 1 new(name.to_s, name.to_s) else new(name.to_s, value, **(options || {})) end end
convert_hash(val)
click to toggle source
Convert a hash into choice
@param [Hash<Symbol,Object>]
@return [Choice]
@api public
# File lib/tty/prompt/choice.rb, line 66 def self.convert_hash(val) if val.key?(:name) && val.key?(:value) new(val[:name].to_s, val[:value], **val) elsif val.key?(:name) new(val[:name].to_s, val[:name].to_s, **val) else new(val.keys.first.to_s, val.values.first) end end
from(val)
click to toggle source
Create choice from value
@examples
Choice.from(:foo) # => <TTY::Prompt::Choice @key=nil @name="foo" @value="foo" @disabled=false> Choice.from([:foo, 1]) # => <TTY::Prompt::Choice @key=nil @name="foo" @value=1 @disabled=false> Choice.from({name: :foo, value: 1, key: "f"} # => <TTY::Prompt::Choice @key="f" @name="foo" @value=1 @disabled=false>
@param [Object] val
the value to be converted
@raise [ArgumentError]
@return [Choice]
@api public
# File lib/tty/prompt/choice.rb, line 28 def self.from(val) case val when Choice val when Array convert_array(val) when Hash convert_hash(val) else new(val, val) end end
new(name, value, **options)
click to toggle source
Create a Choice instance
@api public
# File lib/tty/prompt/choice.rb, line 94 def initialize(name, value, **options) @name = name @value = value @key = options[:key] @disabled = options[:disabled].nil? ? false : options[:disabled] freeze end
Public Instance Methods
==(other)
click to toggle source
Object equality comparison
@return [Boolean]
@api public
# File lib/tty/prompt/choice.rb, line 128 def ==(other) return false unless other.is_a?(self.class) name == other.name && value == other.value && key == other.key end
disabled?()
click to toggle source
Check if this choice is disabled
@return [Boolean]
@api public
# File lib/tty/prompt/choice.rb, line 107 def disabled? !!@disabled end
to_s()
click to toggle source
Object string representation
@return [String]
@api public
# File lib/tty/prompt/choice.rb, line 141 def to_s name.to_s end
value()
click to toggle source
Read value and evaluate
@api public
# File lib/tty/prompt/choice.rb, line 114 def value case @value when Proc @value.call else @value end end