Sha256: ac5aa8faca280d0922e3458fd9ccf3f009cd6377d6d3384692b68456054c28f6

Contents?: true

Size: 1.46 KB

Versions: 5

Compression:

Stored size: 1.46 KB

Contents

# frozen_string_literal: true

require_relative 'question'

class HighlineWrapper
  class CheckboxQuestion < Question
    class << self
      def ask(prompt, choices, options)
        indices = ask_highline(format_options(prompt, choices))

        return format_multiple_selections(choices, indices, options[:with_indexes]) unless indices.empty?
        return recurse(prompt, choices, options) if options[:required]
        return return_empty_defaults(options) if options[:defaults].empty?

        return_defaults(choices, options)
      end

      private def ask_highline(prompt)
        indices = []
        super(prompt).to_s.split(',').each { |i| indices << i.strip.to_i - 1 }
        indices
      end

      private def return_defaults(choices, options)
        options[:default_indexes] = options[:defaults].map { |d| choices.index(d) }
        print_default_message(options, choices) if options[:indicate_default_message]
        format_multiple_selections(choices, options[:default_indexes], options[:with_indexes])
      end

      private def print_default_message(options, choices)
        defaults = options[:default_indexes].map { |i| "#{i + 1}. #{choices[i]}".strip }.join(', ')
        puts "--- Defaults selected: #{defaults} ---"
      end

      private def format_multiple_selections(choices, indices, with_indexes)
        selected = []
        indices.each { |index| selected << format_selection(choices, index, with_indexes) }
        selected
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

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