Sha256: 7429698444f7dc05d5b0dd1ab9f6eb913d370c1b9c548827f636140860e4f41d

Contents?: true

Size: 1.18 KB

Versions: 7

Compression:

Stored size: 1.18 KB

Contents

module Playmo
  class Question < Thor::Shell::Basic
    attr_accessor :answers, :choice, :question_text, :caller

    def initialize(arg, &block)
      @question_text, method_name = arg, nil
      @answers ||= []
      @padding   = 0

      # Question with no answers
      if arg.respond_to? :keys
        @question_text, method_name = arg.first.first, arg.first.last
        answer(nil => method_name)
      end

      # Multiple answers
      self.instance_eval(&block) if block_given?
    end

    def set_caller(recipe)
     @caller = recipe
    end

    def answer(arg)
      @answers << Playmo::Answer.new(arg.first.first, arg.first.last, @answers.size + 1)
    end

    def has_answers?
      @answers.size > 1
    end

    def ask_question!
      say render
    end

    # Render question with answers
    def render
      result = "\n#{@question_text}"

      if has_answers?
        result += ":\n\n"
        num     = 1
        @answers.each do |answer|
          result += answer.render
          num    += 1
        end
      else
        result += "\n"
      end

      # Create choice
      @choice = Playmo::Choice.new(self, @caller)

      result
    end

    alias :to_s :render
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
playmo-0.0.18 lib/playmo/question.rb
playmo-0.0.17 lib/playmo/question.rb
playmo-0.0.14 lib/playmo/question.rb
playmo-0.0.13 lib/playmo/question.rb
playmo-0.0.12 lib/playmo/question.rb
playmo-0.0.11 lib/playmo/question.rb
playmo-0.0.10 lib/playmo/question.rb