Class: Zenflow::Query
- Inherits:
-
Object
- Object
- Zenflow::Query
- Defined in:
- lib/zenflow/helpers/ask.rb
Class Method Summary collapse
- .ask_question(question, options = {}) ⇒ Object
- .build_error_message(response, options = {}) ⇒ Object
- .handle_response(response, options = {}) ⇒ Object
- .prompt_for_answer(question, options = {}) ⇒ Object
- .valid_response?(response, options = {}) ⇒ Boolean
Class Method Details
.ask_question(question, options = {}) ⇒ Object
18 19 20 21 22 23 24 25 26 |
# File 'lib/zenflow/helpers/ask.rb', line 18 def self.ask_question(question, ={}) if [:response].to_s.strip != "" response = [:response] else response = Zenflow::Query.prompt_for_answer(question, ) end Zenflow::LogToFile("Response: #{response}") response end |
.build_error_message(response, options = {}) ⇒ Object
57 58 59 60 61 62 63 64 65 66 |
# File 'lib/zenflow/helpers/ask.rb', line 57 def self.(response, ={}) if [:required] = "You must respond to this prompt." elsif [:error_message] = [:error_message] else = %{"#{response}" is not a valid response.} end "-----> #{} Try again.".red end |
.handle_response(response, options = {}) ⇒ Object
37 38 39 40 41 42 43 44 45 |
# File 'lib/zenflow/helpers/ask.rb', line 37 def self.handle_response(response, ={}) if !Zenflow::Query.valid_response?(response, ) raise Zenflow::Query.(response, ) end return [:default].downcase if response == "" return response.downcase if response == "Y" || response == "N" response end |
.prompt_for_answer(question, options = {}) ⇒ Object
28 29 30 31 32 33 34 35 |
# File 'lib/zenflow/helpers/ask.rb', line 28 def self.prompt_for_answer(question, ={}) prompt = ">> #{question} " prompt << "[#{[:options].join('/')}] " if [:options] prompt << "[#{[:default]}] " if [:default] && ![:options] Zenflow::LogToFile("Asked: #{prompt}") print prompt $stdin.gets.chomp end |
.valid_response?(response, options = {}) ⇒ Boolean
47 48 49 50 51 52 53 54 55 |
# File 'lib/zenflow/helpers/ask.rb', line 47 def self.valid_response?(response, ={}) response = normalize_response(response, [:default]) [:options].map!(&:downcase) if [:options] return false if [:options] && ![:options].include?(response) return false if [:validate] && [:validate].is_a?(Regexp) && !response[[:validate]] return false if [:required] && response == "" true end |