Class: Question

Inherits:
Object
  • Object
show all
Defined in:
lib/battleroom/models/question.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(evaluation_scope) ⇒ Question

Returns a new instance of Question.



7
8
9
10
11
12
13
# File 'lib/battleroom/models/question.rb', line 7

def initialize(evaluation_scope)
  @evaluation_scope = evaluation_scope
  @data = self.class.generate_question
  @variable_name = rotate_array(data[:possible_variable_names] || []).first
  @input_mechanism = 'readline'
  post_initialize
end

Instance Attribute Details

#answer_valueObject

Returns the value of attribute answer_value.



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

def answer_value
  @answer_value
end

#dataObject (readonly)

Returns the value of attribute data.



2
3
4
# File 'lib/battleroom/models/question.rb', line 2

def data
  @data
end

#data_structureObject

Returns the value of attribute data_structure.



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

def data_structure
  @data_structure
end

#data_structure_classObject

Returns the value of attribute data_structure_class.



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

def data_structure_class
  @data_structure_class
end

#evaluation_scopeObject

Returns the value of attribute evaluation_scope.



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

def evaluation_scope
  @evaluation_scope
end

#explanationObject

Returns the value of attribute explanation.



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

def explanation
  @explanation
end

#input_mechanismObject

Returns the value of attribute input_mechanism.



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

def input_mechanism
  @input_mechanism
end

#user_inputObject

Returns the value of attribute user_input.



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

def user_input
  @user_input
end

#variable_nameObject

Returns the value of attribute variable_name.



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

def variable_name
  @variable_name
end

#variable_valueObject

Returns the value of attribute variable_value.



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

def variable_value
  @variable_value
end

Class Method Details

.generate_questionObject

avoids immediate re-sampling of questions



20
21
22
23
24
25
# File 'lib/battleroom/models/question.rb', line 20

def self.generate_question
  # calls upon *class instance* variable assigned in the subclasses
  question = @questions.shift
  @questions.push(question)
  question
end

Instance Method Details

#congratulation_sequence(duration) ⇒ Object



27
28
29
30
31
# File 'lib/battleroom/models/question.rb', line 27

def congratulation_sequence(duration)
  print_congratulation
  sleep(duration)
  clear_display
end

#enter_evaluation_loop(&block) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/battleroom/models/question.rb', line 50

def enter_evaluation_loop(&block)
  answered_correctly = false
  until answered_correctly
    begin
      user_input = get_input
      abort('Goodbye!'.green) if user_input.match(/^(q|exit|!!!\s?)\z/i)
      if !naughty_input?(user_input) && yield(user_input)
        congratulation_sequence(1.6)
        answered_correctly = true
      end
    rescue SyntaxError => e
      handle_syntax_error_exceptions(e)
    end
  end
end

#get_inputObject



41
42
43
44
45
46
47
48
# File 'lib/battleroom/models/question.rb', line 41

def get_input
  if input_mechanism == 'readline'
    Readline.readline('> '.blue, true).chomp
  else
    Pry.start_without_pry_byebug(evaluation_scope)
    $input.chomp
  end
end

#handle_syntax_error_exceptions(error) ⇒ Object



33
34
35
36
37
38
39
# File 'lib/battleroom/models/question.rb', line 33

def handle_syntax_error_exceptions(error)
  if error.message.match /unexpected end-of-input/
    print_unexpected_end_of_input_explanation(error)
  elsif error.message.include?('unterminated string meets end of file')
    battleprint 'Blurg! You neglected to provide closing quotes for your string. Try again!'.red
  end
end

#post_initializeObject



15
16
17
# File 'lib/battleroom/models/question.rb', line 15

def post_initialize
  nil
end