Class: KtCommon::ConsoleIO
- Inherits:
-
Object
- Object
- KtCommon::ConsoleIO
- Includes:
- KtCmdLine
- Defined in:
- lib/ktcommon/consoleio.rb
Overview
The ConsoleIO class provides methods for reading from and writing to the console.
Instance Attribute Summary collapse
-
#answers ⇒ Object
readonly
Returns the value of attribute answers.
Instance Method Summary collapse
-
#ask(questions) ⇒ Object
Ask questions and store the answers.
-
#initialize ⇒ ConsoleIO
constructor
ConsoleIO class constructor.
Methods included from KtCmdLine
Constructor Details
#initialize ⇒ ConsoleIO
ConsoleIO class constructor
30 31 32 33 |
# File 'lib/ktcommon/consoleio.rb', line 30 def initialize() $LOG.debug "ConsoleIO::new" @answers = {} end |
Instance Attribute Details
#answers ⇒ Object (readonly)
Returns the value of attribute answers.
24 25 26 |
# File 'lib/ktcommon/consoleio.rb', line 24 def answers @answers end |
Instance Method Details
#ask(questions) ⇒ Object
Ask questions and store the answers. Questions will be an array of hashes. Each element in the array is a question. Each question hash can have the following keys: - :param - this will be used as the key to the answer hash - :q - The question to be displayed. No punctuation will be added so include it if needed. - :desc - Description text to be displayed before showing the question (optional)
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/ktcommon/consoleio.rb', line 44 def ask(questions) $LOG.debug "ConsoleIO::ask" raise ArgumentError.new("Expecting Array of Hashes. [questions] not an Array.") if questions.class != Array raise ArgumentError.new("Expecting Array of Hashes. [questions] is empty.") if questions.empty? qs = [] questions.each do |ques| raise ArgumentError.new("Expecting Hash.") if ques.class != Hash raise ArgumentError.new("Missing key :param") if !ques.has_key? :param raise ArgumentError.new("Missing key :q") if !ques.has_key? :q qs.clear if(!ques[:desc].nil?) # If there is a :desc key, if(ques[:desc].class == Array) # If it is an array, ques[:desc].each {|d| qs << d} # add each element to the text to display. else qs << ques[:desc] # otherwise add :desc to text to display. end end qs << ques[:q] # Add the actual question to text to display. @answers[ques[:param]] = getInput(qs) # Ask the question and store the answer. end @answers # Return the answers hash for easy usage. end |