Sha256: 49aedf1e710ff24be9b6442918faad6252256225108074b79871b71cadff15ff

Contents?: true

Size: 1.18 KB

Versions: 4

Compression:

Stored size: 1.18 KB

Contents

module Playmo
  class Choice
    CAPTION = "Your choice"
    attr_accessor :question, :shell, :color, :user_input

    def initialize(question)
      @question = question
      @shell    = Thor::Shell::Basic.new
      @color    = Thor::Shell::Color.new
    end

    def accepted_values
      if question.has_answers?
        1.upto(question.answers.size).to_a.map { |value| value.to_s }
      else
        %w/y n yes no/
      end
    end

    def get_answer
      shell.padding = 1
      answer = nil

      until accepted_values.include?(@user_input) do
        @user_input = shell.ask(render)
        @user_input.downcase!
      end

      if @user_input
        if question.has_answers?
          answer = question.answers.find { |answer| answer.num.to_s == @user_input }
        else
          answer = question.answers.first if %w/y yes/.include?(@user_input)
        end
      end

      answer
    end

    def render
      if question.has_answers?
        sentence = accepted_values.to_sentence(
          :last_word_connector => ' or '
        )
      else
        sentence = "y/n"
      end

      color.set_color("#{CAPTION} (#{sentence}):", :white, true)
    end

    alias :to_s :render
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
playmo-0.1.10 lib/playmo/choice.rb
playmo-0.1.9 lib/playmo/choice.rb
playmo-0.1.8 lib/playmo/choice.rb
playmo-0.1.7 lib/playmo/choice.rb