Class: Prompts::SelectPrompt
- Defined in:
- lib/prompts/select_prompt.rb
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(options: nil, **kwargs) ⇒ SelectPrompt
constructor
A new instance of SelectPrompt.
-
#options(options) ⇒ Object
standard:disable Style/TrivialAccessors.
-
#prepare_content ⇒ Object
standard:enable Style/TrivialAccessors.
Methods inherited from Prompt
#ask, #content, #default, #hint, #label, #prepend_content
Constructor Details
#initialize(options: nil, **kwargs) ⇒ SelectPrompt
Returns a new instance of SelectPrompt.
11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/prompts/select_prompt.rb', line 11 def initialize(options: nil, **kwargs) super(**kwargs) @options = .is_a?(Array) ? .to_h { |item| [item, item] } : @default = if (index = @options.keys.index(@default)) index + 1 end @instructions = "Enter the number of your choice" @hint ||= "Type your response and press Enter ⏎" @validations << ->(choice) { "Invalid choice." if !choice.to_i.between?(1, @options.size) } end |
Class Method Details
.ask(options: nil, **kwargs) {|instance| ... } ⇒ Object
5 6 7 8 9 |
# File 'lib/prompts/select_prompt.rb', line 5 def self.ask(options: nil, **kwargs) instance = new(options: , **kwargs) yield instance if block_given? instance.ask end |
Instance Method Details
#options(options) ⇒ Object
standard:disable Style/TrivialAccessors
24 25 26 |
# File 'lib/prompts/select_prompt.rb', line 24 def () @options = end |
#prepare_content ⇒ Object
standard:enable Style/TrivialAccessors
29 30 31 32 33 34 35 |
# File 'lib/prompts/select_prompt.rb', line 29 def prepare_content super @options.each_with_index do |(key, value), index| @content.paragraph Fmt("%{prefix}|>faint|>bold %{option}", prefix: "#{index + 1}.", option: value) end @content end |