Sha256: 5ab105657184f54ff9c270b02b56595e226458e1cffcc31165715e3bc1aa5774

Contents?: true

Size: 999 Bytes

Versions: 6

Compression:

Stored size: 999 Bytes

Contents

# frozen_string_literal: true

require_relative 'question'

class HighlineWrapper
  class MultipleChoiceQuestion < Question
    class << self
      def ask(prompt, choices, options)
        index = ask_highline(format_options(prompt, choices)).to_i - 1

        return format_selection(choices, index, options[:with_index]) unless index == -1
        return recurse(prompt, choices, options) if options[:required]
        return return_empty_defaults(options) if options[:default].nil?

        return_defaults(choices, options)
      end

      private def return_defaults(choices, options)
        options[:default_index] = choices.index(options[:default])
        print_default_message(options) if options[:indicate_default_message]
        format_selection(choices, options[:default_index], options[:with_index])
      end

      private def print_default_message(options)
        puts "--- Default selected: #{options[:default_index] + 1}. #{options[:default]} ---"
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
highline_wrapper-1.3.1 lib/highline_wrapper/multiple_choice_question.rb
highline_wrapper-1.3.0 lib/highline_wrapper/multiple_choice_question.rb
highline_wrapper-1.2.2 lib/highline_wrapper/multiple_choice_question.rb
highline_wrapper-1.2.1 lib/highline_wrapper/multiple_choice_question.rb
highline_wrapper-1.2.0 lib/highline_wrapper/multiple_choice_question.rb
highline_wrapper-1.1.0 lib/highline_wrapper/multiple_choice_question.rb