Class: Playmo::Choice

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

Constant Summary collapse

CAPTION =
"\nMake your choice"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(question, caller) ⇒ Choice

Returns a new instance of Choice.



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

def initialize(question, caller)
  @question = question
  @caller   = caller
  @padding  = 0
  @choice   = nil

  if @question.has_answers?
    @accepted_values = 1.upto(@question.answers.size).to_a.map { |value| value.to_s }
  else
    @accepted_values = %w/y n yes no/
  end
end

Instance Attribute Details

#accepted_valuesObject

Returns the value of attribute accepted_values.



4
5
6
# File 'lib/playmo/choice.rb', line 4

def accepted_values
  @accepted_values
end

#callerObject

Returns the value of attribute caller.



4
5
6
# File 'lib/playmo/choice.rb', line 4

def caller
  @caller
end

#choiceObject

Returns the value of attribute choice.



4
5
6
# File 'lib/playmo/choice.rb', line 4

def choice
  @choice
end

#questionObject

Returns the value of attribute question.



4
5
6
# File 'lib/playmo/choice.rb', line 4

def question
  @question
end

#user_inputObject

Returns the value of attribute user_input.



4
5
6
# File 'lib/playmo/choice.rb', line 4

def user_input
  @user_input
end

Instance Method Details

#make_choice!Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/playmo/choice.rb', line 19

def make_choice!
  until @accepted_values.include?(@user_input) do
    @user_input = ask render
    @user_input.downcase!
  end

  if @user_input

    if @question.has_answers?
      answer = @question.answers.find { |answer| answer.num.to_s == @user_input }
      @caller.send(answer.method_name)
    else
      answer = @question.answers.first
      @caller.send(answer.method_name) if %w/y yes/.include?(@user_input)
    end

  end
end

#renderObject Also known as: to_s



38
39
40
41
42
43
44
45
46
47
48
# File 'lib/playmo/choice.rb', line 38

def render
  if @question.has_answers?
    sentence = @accepted_values.to_sentence(
      :last_word_connector => ' or '
    )
  else
    sentence = "y/n"
  end

  CAPTION + " (#{sentence}):"
end