Class: Questions::Answer
- Inherits:
-
Object
- Object
- Questions::Answer
- Defined in:
- lib/questions/answer.rb
Overview
Represents an answer.
An answer has an indicator which will be used to detect which answer is selected by user. Indicators are the first few letters of its instruction. User can identify an indicator by its surrounded square brackets. After that the rest of the instruction will be displayed.
Answers can be active or inactive. Only active ansers will be displayed, inactive ones will be hidden. Answers can be special, then the indicator is in uppercase letters set.
Constant Summary collapse
- SPECIAL_ENDINGS =
[:all]
Instance Attribute Summary collapse
-
#indicator(first_chars = 1) ⇒ Object
Gets indicator.
-
#instruction ⇒ Object
readonly
Gets instruction.
Instance Method Summary collapse
-
#active? ⇒ Boolean
(also: #true?)
Is answer active?.
-
#inactive? ⇒ Boolean
(also: #false?)
Is answer inactive?.
-
#indicator_hash ⇒ Object
Gets indicator hash.
-
#initialize(*answer_hash) ⇒ Answer
constructor
Instantiates new Answer object.
-
#special? ⇒ Boolean
Returns ‘true` if answer is special.
-
#to_s ⇒ Object
Gets string representation.
Constructor Details
#initialize(*answer_hash) ⇒ Answer
Instantiates new Answer object.
Only the first key value pair will be used.
32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/questions/answer.rb', line 32 def initialize(*answer_hash) hash = answer_hash.first hash = {hash => true} if hash.is_a? Symbol unless [Hash, Symbol].any? { |class_name| hash.is_a? class_name } raise ArgumentError, "Parameter has to be a key-value-pair or a symbol" end array = hash.first @instruction = array[0] @active = array[1] end |
Instance Attribute Details
#indicator(first_chars = 1) ⇒ Object
Gets indicator
102 103 104 105 106 107 108 |
# File 'lib/questions/answer.rb', line 102 def indicator(first_chars=1) return nil if inactive? return @indicator if @indicator indicator = instruction.to_s[0...first_chars] indicator.upcase! if special? indicator.to_sym end |
#instruction ⇒ Object (readonly)
Gets instruction
50 51 52 |
# File 'lib/questions/answer.rb', line 50 def instruction @instruction end |
Instance Method Details
#active? ⇒ Boolean Also known as: true?
Is answer active?
59 60 61 |
# File 'lib/questions/answer.rb', line 59 def active? @active end |
#inactive? ⇒ Boolean Also known as: false?
Is answer inactive?
71 72 73 |
# File 'lib/questions/answer.rb', line 71 def inactive? !active? end |
#indicator_hash ⇒ Object
Gets indicator hash
120 121 122 123 |
# File 'lib/questions/answer.rb', line 120 def indicator_hash return nil if inactive? {indicator => instruction} end |
#special? ⇒ Boolean
Returns ‘true` if answer is special. Special answers are answers which ends with one of the SPECIAL_ENDINGS, `false` otherwise.
111 112 113 |
# File 'lib/questions/answer.rb', line 111 def special? SPECIAL_ENDINGS.any? { |ending| instruction.to_s =~ /#{ending}$/ } end |
#to_s ⇒ Object
Gets string representation
130 131 132 133 134 135 136 |
# File 'lib/questions/answer.rb', line 130 def to_s return nil if inactive? indicator = indicator() instruction_without_indicator = instruction.to_s[indicator.length..-1] humanized = instruction_without_indicator.gsub("_", " ") "[#{indicator}]#{humanized}" end |