Class: TTY::Prompt::Choice
- Inherits:
-
Object
- Object
- TTY::Prompt::Choice
- Defined in:
- lib/tty/prompt/choice.rb
Overview
An immutable representation of a single choice option from select menu
Instance Attribute Summary collapse
-
#disabled ⇒ Object
readonly
The text to display for disabled choice.
-
#key ⇒ Object
readonly
The keyboard key to activate this choice.
-
#name ⇒ Object
readonly
The label name.
Class Method Summary collapse
-
.convert_array(val) ⇒ Choice
Convert an array into choice.
-
.convert_hash(val) ⇒ Choice
Convert a hash into choice.
-
.from(val) ⇒ Choice
Create choice from value.
Instance Method Summary collapse
-
#==(other) ⇒ Boolean
Object equality comparison.
-
#disabled? ⇒ Boolean
Check if this choice is disabled.
-
#initialize(name, value, **options) ⇒ Choice
constructor
Create a Choice instance.
-
#to_s ⇒ String
Object string representation.
-
#value ⇒ Object
Read value and evaluate.
Constructor Details
#initialize(name, value, **options) ⇒ Choice
Create a Choice instance
95 96 97 98 99 100 101 |
# File 'lib/tty/prompt/choice.rb', line 95 def initialize(name, value, **) @name = name @value = value @key = [:key] @disabled = [:disabled].nil? ? false : [:disabled] freeze end |
Instance Attribute Details
#disabled ⇒ Object (readonly)
The text to display for disabled choice
90 91 92 |
# File 'lib/tty/prompt/choice.rb', line 90 def disabled @disabled end |
#key ⇒ Object (readonly)
The keyboard key to activate this choice
85 86 87 |
# File 'lib/tty/prompt/choice.rb', line 85 def key @key end |
#name ⇒ Object (readonly)
The label name
80 81 82 |
# File 'lib/tty/prompt/choice.rb', line 80 def name @name end |
Class Method Details
.convert_array(val) ⇒ Choice
Convert an array into choice
49 50 51 52 53 54 55 56 57 58 |
# File 'lib/tty/prompt/choice.rb', line 49 def self.convert_array(val) name, value, = *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, **( || {})) end end |
.convert_hash(val) ⇒ Choice
Convert a hash into choice
67 68 69 70 71 72 73 74 75 |
# File 'lib/tty/prompt/choice.rb', line 67 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 |
Instance Method Details
#==(other) ⇒ Boolean
Object equality comparison
129 130 131 132 133 134 135 |
# File 'lib/tty/prompt/choice.rb', line 129 def ==(other) return false unless other.is_a?(self.class) name == other.name && value == other.value && key == other.key end |
#disabled? ⇒ Boolean
Check if this choice is disabled
108 109 110 |
# File 'lib/tty/prompt/choice.rb', line 108 def disabled? !!@disabled end |
#to_s ⇒ String
Object string representation
142 143 144 |
# File 'lib/tty/prompt/choice.rb', line 142 def to_s name.to_s end |
#value ⇒ Object
Read value and evaluate
115 116 117 118 119 120 121 122 |
# File 'lib/tty/prompt/choice.rb', line 115 def value case @value when Proc @value.call else @value end end |