Class: Questions::Answer
- Inherits:
-
Object
- Object
- Questions::Answer
- Defined in:
- lib/questions/answer.rb
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.
24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/questions/answer.rb', line 24 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
94 95 96 97 98 99 100 |
# File 'lib/questions/answer.rb', line 94 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
42 43 44 |
# File 'lib/questions/answer.rb', line 42 def instruction @instruction end |
Instance Method Details
#active? ⇒ Boolean Also known as: true?
Is answer active?
51 52 53 |
# File 'lib/questions/answer.rb', line 51 def active? @active end |
#inactive? ⇒ Boolean Also known as: false?
Is answer inactive?
63 64 65 |
# File 'lib/questions/answer.rb', line 63 def inactive? !active? end |
#indicator_hash ⇒ Object
Gets indicator hash
112 113 114 115 |
# File 'lib/questions/answer.rb', line 112 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.
103 104 105 |
# File 'lib/questions/answer.rb', line 103 def special? SPECIAL_ENDINGS.any? { |ending| instruction.to_s =~ /#{ending}$/ } end |
#to_s ⇒ Object
Gets string representation
122 123 124 125 126 127 |
# File 'lib/questions/answer.rb', line 122 def to_s return nil if inactive? instruction_without_indicator = instruction.to_s[indicator.length..-1] humanized = instruction_without_indicator.gsub("_", " ") "[#{indicator}]#{humanized}" end |