Class: Quby::Answers::Services::AttributeCalculator
- Inherits:
-
Object
- Object
- Quby::Answers::Services::AttributeCalculator
- Defined in:
- lib/quby/answers/services/attribute_calculator.rb
Instance Attribute Summary collapse
-
#answer ⇒ Object
readonly
Returns the value of attribute answer.
-
#depends_on_lookup ⇒ Object
readonly
To check if a question or question option that is depended on is filled in, we can look up the question that fits the key in the depends_on_lookup hash.
-
#groups ⇒ Object
readonly
Returns the value of attribute groups.
-
#hidden ⇒ Object
readonly
Returns the value of attribute hidden.
-
#questionnaire ⇒ Object
readonly
Returns the value of attribute questionnaire.
-
#shown ⇒ Object
readonly
Returns the value of attribute shown.
Instance Method Summary collapse
- #init_flag_result ⇒ Object
-
#initialize(questionnaire, answer) ⇒ AttributeCalculator
constructor
A new instance of AttributeCalculator.
- #process_checkbox(question) ⇒ Object
- #process_option_hiding(option) ⇒ Object
- #process_option_showing(option) ⇒ Object
- #process_question(question) ⇒ Object
- #process_question_answer(question) ⇒ Object
- #process_question_groups(question) ⇒ Object
- #process_questions_that_are_invisible_by_default(question) ⇒ Object
- #process_radioish(question) ⇒ Object
Constructor Details
#initialize(questionnaire, answer) ⇒ AttributeCalculator
Returns a new instance of AttributeCalculator.
14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/quby/answers/services/attribute_calculator.rb', line 14 def initialize(questionnaire, answer) @questionnaire = questionnaire @answer = answer @hidden = [] @shown = [] @groups = {} @depends_on_lookup = {} init_flag_result questionnaire.questions.compact.each { |question| process_question(question) } end |
Instance Attribute Details
#answer ⇒ Object (readonly)
Returns the value of attribute answer.
7 8 9 |
# File 'lib/quby/answers/services/attribute_calculator.rb', line 7 def answer @answer end |
#depends_on_lookup ⇒ Object (readonly)
To check if a question or question option that is depended on is filled in, we can look up the question that fits the key in the depends_on_lookup hash.
12 13 14 |
# File 'lib/quby/answers/services/attribute_calculator.rb', line 12 def depends_on_lookup @depends_on_lookup end |
#groups ⇒ Object (readonly)
Returns the value of attribute groups.
8 9 10 |
# File 'lib/quby/answers/services/attribute_calculator.rb', line 8 def groups @groups end |
#hidden ⇒ Object (readonly)
Returns the value of attribute hidden.
8 9 10 |
# File 'lib/quby/answers/services/attribute_calculator.rb', line 8 def hidden @hidden end |
#questionnaire ⇒ Object (readonly)
Returns the value of attribute questionnaire.
7 8 9 |
# File 'lib/quby/answers/services/attribute_calculator.rb', line 7 def questionnaire @questionnaire end |
#shown ⇒ Object (readonly)
Returns the value of attribute shown.
8 9 10 |
# File 'lib/quby/answers/services/attribute_calculator.rb', line 8 def shown @shown end |
Instance Method Details
#init_flag_result ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/quby/answers/services/attribute_calculator.rb', line 26 def init_flag_result questionnaire.flags.each_value do |flag| flag.if_triggered_by answer.flags do flag.hides_questions.each do |question_key| @hidden.push question_key unless @shown.include?(question_key) end flag.shows_questions.each do |question_key| @shown.push question_key end end end end |
#process_checkbox(question) ⇒ Object
86 87 88 89 90 91 92 93 94 |
# File 'lib/quby/answers/services/attribute_calculator.rb', line 86 def process_checkbox(question) = question..select { |option| !option.inner_title? && answer.send(option.key) == 1 } .each do |option| process_option_hiding(option) process_option_showing(option) @depends_on_lookup[option.input_key] = true end end |
#process_option_hiding(option) ⇒ Object
96 97 98 99 100 |
# File 'lib/quby/answers/services/attribute_calculator.rb', line 96 def process_option_hiding(option) if option.hides_questions.present? @hidden.concat(option.hides_questions.reject { |key| @shown.include? key }).uniq end end |
#process_option_showing(option) ⇒ Object
102 103 104 105 106 107 |
# File 'lib/quby/answers/services/attribute_calculator.rb', line 102 def process_option_showing(option) if option.shows_questions.present? @hidden.delete_if { |key| option.shows_questions.include? key } @shown.concat(option.shows_questions).uniq end end |
#process_question(question) ⇒ Object
39 40 41 42 43 44 45 |
# File 'lib/quby/answers/services/attribute_calculator.rb', line 39 def process_question(question) return if question.hidden? process_questions_that_are_invisible_by_default(question) process_question_answer(question) process_question_groups(question) end |
#process_question_answer(question) ⇒ Object
53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/quby/answers/services/attribute_calculator.rb', line 53 def process_question_answer(question) case question.type when :radio, :scale, :select process_radioish(question) when :check_box process_checkbox(question) else if answer.send(question.key).present? @depends_on_lookup[question.key] = true end end end |
#process_question_groups(question) ⇒ Object
66 67 68 69 70 71 |
# File 'lib/quby/answers/services/attribute_calculator.rb', line 66 def process_question_groups(question) if question.question_group @groups[question.question_group] = [] unless @groups[question.question_group] @groups[question.question_group] << question.key end end |
#process_questions_that_are_invisible_by_default(question) ⇒ Object
47 48 49 50 51 |
# File 'lib/quby/answers/services/attribute_calculator.rb', line 47 def process_questions_that_are_invisible_by_default(question) if question.default_invisible && !@shown.include?(question.key) @hidden.push question.key end end |
#process_radioish(question) ⇒ Object
73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/quby/answers/services/attribute_calculator.rb', line 73 def process_radioish(question) if value = answer.send(question.key) selected_option = question..find { |option| option.key.to_s == value } if selected_option process_option_hiding(selected_option) process_option_showing(selected_option) # placeholder options of select boxes should count as unanswered for depends_on relations @depends_on_lookup[selected_option.input_key] = !selected_option.placeholder end end end |