lib/interactive/question.rb in interactive-0.0.0 vs lib/interactive/question.rb in interactive-0.1.0

- old
+ new

@@ -2,48 +2,30 @@ class Question attr_accessor :question, :options def initialize(&block) yield self + + @options = Interactive::Options.new(Array(@options)) + + raise ArgumentError, "question cannot be nil nor empty." if question.nil? || question.empty? + raise ArgumentError, "options cannot be empty." if options.empty? end def ask_and_wait_for_valid_response(&block) - resp = NullResponse.new - - while resp.invalid? do - puts "#{question} #{shortcuts_string}" + loop do + puts "#{question} #{options.shortcuts_string}" resp = Interactive::Response.new(options) - puts shortcuts_meanings if resp.invalid? + puts options.shortcuts_meanings if resp.invalid? yield resp + break unless resp.invalid? end end - class NullResponse - def invalid? - true - end - end - private def response STDIN.gets.chomp - end - - def shortcuts_meanings - options.inject("") { |accum, opt| "#{accum} #{opt[0]} -- #{opt}\n"} - end - - def options_first_chars - options.inject("") { |accum, opt| "#{accum}#{opt[0]}/" } - end - - def shortcuts_string - "[#{options_first_chars_without_last_slash(options_first_chars)}]" - end - - def options_first_chars_without_last_slash(options_first_chars) - options_first_chars[0..options_first_chars.length-2] end end end