Class: Playmo::Question

Inherits:
Object show all
Defined in:
lib/playmo/question.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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, options, &block)
  @question = question
  @answers  = []
  @recipe   = recipe
  @options  = 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

#answersObject

Returns the value of attribute answers.



9
10
11
# File 'lib/playmo/question.rb', line 9

def answers
  @answers
end

#blockObject

Returns the value of attribute block.



9
10
11
# File 'lib/playmo/question.rb', line 9

def block
  @block
end

#colorObject

Returns the value of attribute color.



9
10
11
# File 'lib/playmo/question.rb', line 9

def color
  @color
end

#optionsObject

Returns the value of attribute options.



9
10
11
# File 'lib/playmo/question.rb', line 9

def options
  @options
end

#questionObject

Returns the value of attribute question.



9
10
11
# File 'lib/playmo/question.rb', line 9

def question
  @question
end

#recipeObject

Returns the value of attribute recipe.



9
10
11
# File 'lib/playmo/question.rb', line 9

def recipe
  @recipe
end

#shellObject

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, options = {}, &block)
  @answers << Playmo::Answer.new(answer, options, @answers.size + 1, &block)
end

#has_answers?Boolean

Returns:

  • (Boolean)


46
47
48
# File 'lib/playmo/question.rb', line 46

def has_answers?
  @answers.size > 1
end

#renderObject 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 options[: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