Class: Playmo::Question
Instance Attribute Summary collapse
-
#answers ⇒ Object
Returns the value of attribute answers.
-
#block ⇒ Object
Returns the value of attribute block.
-
#color ⇒ Object
Returns the value of attribute color.
-
#options ⇒ Object
Returns the value of attribute options.
-
#question ⇒ Object
Returns the value of attribute question.
-
#recipe ⇒ Object
Returns the value of attribute recipe.
-
#shell ⇒ Object
Returns the value of attribute shell.
Instance Method Summary collapse
- #answer(answer, options = {}, &block) ⇒ Object
- #has_answers? ⇒ Boolean
-
#initialize(recipe, question, options, &block) ⇒ Question
constructor
A new instance of Question.
- #render ⇒ Object (also: #to_s)
Constructor Details
#initialize(recipe, question, options, &block) ⇒ Question
Returns a new instance of Question.
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/playmo/question.rb', line 11 def initialize(recipe, question, , &block) @question = question @answers = [] @recipe = recipe @options = @block = block @shell = Thor::Shell::Basic.new @color = Thor::Shell::Color.new if @options[:type] == :question instance_eval &block elsif @options[:type] == :ask && block.arity == 0 answer(nil, nil, &block) elsif @options[:type] == :ask && block.arity > 0 answer(nil, nil, &block) end # Do stuff with block =begin if block.arity > 0 # We have block with args #answer(nil, nil, &block) #@action = block else #instance_eval &block raise block.methods.inspect end # If block without answers was passed unless has_answers? answer(nil, nil, &block) end =end end |
Instance Attribute Details
#answers ⇒ Object
Returns the value of attribute answers.
9 10 11 |
# File 'lib/playmo/question.rb', line 9 def answers @answers end |
#block ⇒ Object
Returns the value of attribute block.
9 10 11 |
# File 'lib/playmo/question.rb', line 9 def block @block end |
#color ⇒ Object
Returns the value of attribute color.
9 10 11 |
# File 'lib/playmo/question.rb', line 9 def color @color end |
#options ⇒ Object
Returns the value of attribute options.
9 10 11 |
# File 'lib/playmo/question.rb', line 9 def @options end |
#question ⇒ Object
Returns the value of attribute question.
9 10 11 |
# File 'lib/playmo/question.rb', line 9 def question @question end |
#recipe ⇒ Object
Returns the value of attribute recipe.
9 10 11 |
# File 'lib/playmo/question.rb', line 9 def recipe @recipe end |
#shell ⇒ Object
Returns the value of attribute shell.
9 10 11 |
# File 'lib/playmo/question.rb', line 9 def shell @shell end |
Instance Method Details
#answer(answer, options = {}, &block) ⇒ Object
50 51 52 |
# File 'lib/playmo/question.rb', line 50 def answer(answer, = {}, &block) @answers << Playmo::Answer.new(answer, , @answers.size + 1, &block) end |
#has_answers? ⇒ Boolean
46 47 48 |
# File 'lib/playmo/question.rb', line 46 def has_answers? @answers.size > 1 end |
#render ⇒ Object Also known as: to_s
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/playmo/question.rb', line 54 def render shell.padding = 1 shell.say("\n") shell.say(color.set_color(question, :green, true)) shell.say(color.set_color(recipe.description, :white, false)) if has_answers? shell.say("\n") answers.each do |answer| shell.say("#{answer}") end end shell.say("\n") if [:type] == :ask && block.arity > 0 response = shell.ask('Enter value:') if block answer_action = block x = Proc.new { answer_action.call(response) } Playmo::Action.new(recipe, &x) end else choice = Playmo::Choice.new(self) answer = choice.get_answer if answer answer_action = answer.block Playmo::Action.new(recipe, &answer_action) end end end |