Class: Playmo::Question
- Inherits:
-
Thor::Shell::Basic
- Object
- Thor::Shell::Basic
- Playmo::Question
- Defined in:
- lib/playmo/question.rb
Instance Attribute Summary collapse
-
#answers ⇒ Object
Returns the value of attribute answers.
-
#caller ⇒ Object
Returns the value of attribute caller.
-
#choice ⇒ Object
Returns the value of attribute choice.
-
#question_text ⇒ Object
Returns the value of attribute question_text.
Instance Method Summary collapse
- #answer(arg) ⇒ Object
- #ask_question! ⇒ Object
- #has_answers? ⇒ Boolean
-
#initialize(arg, &block) ⇒ Question
constructor
A new instance of Question.
-
#render ⇒ Object
(also: #to_s)
Render question with answers.
- #set_caller(recipe) ⇒ Object
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
#answers ⇒ Object
Returns the value of attribute answers.
3 4 5 |
# File 'lib/playmo/question.rb', line 3 def answers @answers end |
#caller ⇒ Object
Returns the value of attribute caller.
3 4 5 |
# File 'lib/playmo/question.rb', line 3 def caller @caller end |
#choice ⇒ Object
Returns the value of attribute choice.
3 4 5 |
# File 'lib/playmo/question.rb', line 3 def choice @choice end |
#question_text ⇒ Object
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
28 29 30 |
# File 'lib/playmo/question.rb', line 28 def has_answers? @answers.size > 1 end |
#render ⇒ Object 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 |