Class: Questions::Answers
- Inherits:
-
Array
- Object
- Array
- Questions::Answers
- Defined in:
- lib/questions/answers.rb
Overview
Answers manages many answers. Each item is a Answer object in this collection.
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.
75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/questions/answers.rb', line 75 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 << AnswersHelper.convert_hash_to_answers(AnswersHelper.select_symbols(arg)) # call as Array end self end |
#[](indicator) ⇒ Answer
Gets answer by indicator.
95 96 97 98 |
# File 'lib/questions/answers.rb', line 95 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.
138 139 140 141 |
# File 'lib/questions/answers.rb', line 138 def has_unique_indicators? indicators = indicators() indicators == indicators.uniq end |
#indicators ⇒ Object
Gets all indicators of answers.
125 126 127 |
# File 'lib/questions/answers.rb', line 125 def indicators map(&:indicator) end |
#to_s ⇒ Object
Returns all answers as choice string.
152 153 154 155 |
# File 'lib/questions/answers.rb', line 152 def to_s update_indicators_for_uniqueness! map(&:to_s).compact.join(", ") end |
#update_indicators_for_uniqueness! ⇒ Object
Updates indicators to get all unique.
109 110 111 112 113 114 115 116 |
# File 'lib/questions/answers.rb', line 109 def update_indicators_for_uniqueness! return if has_unique_indicators? each_with_index do |answer, num| other_indicators = first(num).map(&:indicator) answer.indicator = AnswersHelper.free_indicator_of(answer, used_indicators: other_indicators) end end |