Class: Playmo::Question

Inherits:
Thor::Shell::Basic
  • Object
show all
Defined in:
lib/playmo/question.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(arg, &block) ⇒ Question

Returns a new instance of Question.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/playmo/question.rb', line 5

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

Instance Attribute Details

#answersObject

Returns the value of attribute answers.



3
4
5
# File 'lib/playmo/question.rb', line 3

def answers
  @answers
end

#callerObject

Returns the value of attribute caller.



3
4
5
# File 'lib/playmo/question.rb', line 3

def caller
  @caller
end

#choiceObject

Returns the value of attribute choice.



3
4
5
# File 'lib/playmo/question.rb', line 3

def choice
  @choice
end

#question_textObject

Returns the value of attribute question_text.



3
4
5
# File 'lib/playmo/question.rb', line 3

def question_text
  @question_text
end

Instance Method Details

#answer(arg) ⇒ Object



24
25
26
# File 'lib/playmo/question.rb', line 24

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

#ask_question!Object



32
33
34
# File 'lib/playmo/question.rb', line 32

def ask_question!
  say render
end

#has_answers?Boolean

Returns:

  • (Boolean)


28
29
30
# File 'lib/playmo/question.rb', line 28

def has_answers?
  @answers.size > 1
end

#renderObject Also known as: to_s

Render question with answers



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/playmo/question.rb', line 37

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

#set_caller(recipe) ⇒ Object



20
21
22
# File 'lib/playmo/question.rb', line 20

def set_caller(recipe)
 @caller = recipe
end