Class: Questions::Answers
- Inherits:
-
Array
- Object
- Array
- Questions::Answers
- Defined in:
- lib/questions/answers.rb
Instance Method Summary collapse
-
#<<(arg) ⇒ Answers
Adds answer.
-
#[](indicator) ⇒ Answer
Gets answer by indicator.
-
#has_unique_indicators? ⇒ Boolean
Returns true if indicators are unique, false otherwise.
-
#indicators ⇒ Object
Gets all indicators of answers.
-
#initialize(*args) ⇒ Answers
constructor
Instantiates a new Answers object.
-
#to_s ⇒ Object
Returns all answers as choice string.
-
#update_indicators_for_uniqueness! ⇒ Object
Updates indicators to get all unique.
Constructor Details
Instance Method Details
#<<(arg) ⇒ Answers
Adds answer. Answer can be an instance of Questions::Answer, Symbol, a Hash or an Array. Chaining is supported.
74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/questions/answers.rb', line 74 def <<(arg) case arg when Answer then super arg when Symbol then super Answer.new(arg) when Array arg.each { |arg| self << arg } # call as Answer or Symbol when Hash self << convert_hash_to_answers(select_symbols(arg)) # call as Array end self end |
#[](indicator) ⇒ Answer
Gets answer by indicator.
94 95 96 97 |
# File 'lib/questions/answers.rb', line 94 def [](indicator) update_indicators_for_uniqueness! select { |answer| answer.indicator == indicator }.first end |
#has_unique_indicators? ⇒ Boolean
Returns true if indicators are unique, false otherwise.
137 138 139 |
# File 'lib/questions/answers.rb', line 137 def has_unique_indicators? indicators == indicators.uniq end |
#indicators ⇒ Object
Gets all indicators of answers.
124 125 126 |
# File 'lib/questions/answers.rb', line 124 def indicators map(&:indicator) end |
#to_s ⇒ Object
Returns all answers as choice string.
150 151 152 153 |
# File 'lib/questions/answers.rb', line 150 def to_s update_indicators_for_uniqueness! map(&:to_s).compact.join(", ") end |
#update_indicators_for_uniqueness! ⇒ Object
Updates indicators to get all unique.
108 109 110 111 112 113 114 115 |
# File 'lib/questions/answers.rb', line 108 def update_indicators_for_uniqueness! return if has_unique_indicators? each_with_index do |answer, i| other_indicators = first(i).map(&:indicator) answer.indicator = free_indicator_of(answer, used_indicators: other_indicators) end end |