Class: FlowChat::Prompt
- Inherits:
-
Object
- Object
- FlowChat::Prompt
- Defined in:
- lib/flow_chat/prompt.rb
Instance Attribute Summary collapse
-
#user_input ⇒ Object
readonly
Returns the value of attribute user_input.
Instance Method Summary collapse
- #ask(msg, choices: nil, transform: nil, validate: nil, media: nil) ⇒ Object
-
#initialize(input) ⇒ Prompt
constructor
A new instance of Prompt.
- #say(message, media: nil) ⇒ Object
- #select(msg, choices, media: nil, error_message: "Invalid selection:") ⇒ Object
- #yes?(msg) ⇒ Boolean
Constructor Details
#initialize(input) ⇒ Prompt
Returns a new instance of Prompt.
5 6 7 |
# File 'lib/flow_chat/prompt.rb', line 5 def initialize(input) @user_input = input end |
Instance Attribute Details
#user_input ⇒ Object (readonly)
Returns the value of attribute user_input.
3 4 5 |
# File 'lib/flow_chat/prompt.rb', line 3 def user_input @user_input end |
Instance Method Details
#ask(msg, choices: nil, transform: nil, validate: nil, media: nil) ⇒ Object
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/flow_chat/prompt.rb', line 9 def ask(msg, choices: nil, transform: nil, validate: nil, media: nil) if user_input.present? input = user_input validation_error = validate.call(input) if validate.present? if validation_error.present? # Use config to determine whether to combine validation error with original message = if FlowChat::Config. [validation_error, msg].join("\n\n") else validation_error end prompt!(, choices: choices, media: media) end input = transform.call(input) if transform.present? return input end # Pass raw message and media separately to the renderer prompt! msg, choices: choices, media: media end |
#say(message, media: nil) ⇒ Object
32 33 34 35 |
# File 'lib/flow_chat/prompt.rb', line 32 def say(, media: nil) # Pass raw message and media separately to the renderer terminate! , media: media end |
#select(msg, choices, media: nil, error_message: "Invalid selection:") ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/flow_chat/prompt.rb', line 37 def select(msg, choices, media: nil, error_message: "Invalid selection:") raise ArgumentError, "choices must be an array or hash" unless choices.is_a?(Array) || choices.is_a?(Hash) normalized_choices = normalize_choices(choices) ask( msg, choices: choices, validate: lambda { |choice| unless normalized_choices.key?(choice.to_s) }, transform: lambda do |choice| choices = choices.keys if choices.is_a?(Hash) choices.index_by { |choice| choice.to_s }[choice.to_s] end, media: media ) end |
#yes?(msg) ⇒ Boolean
53 54 55 |
# File 'lib/flow_chat/prompt.rb', line 53 def yes?(msg) select(msg, ["Yes", "No"]) == "Yes" end |