Class: Quby::Questionnaires::Entities::Fields
- Inherits:
-
Object
- Object
- Quby::Questionnaires::Entities::Fields
- Defined in:
- lib/quby/questionnaires/entities/fields.rb
Instance Attribute Summary collapse
-
#answer_keys ⇒ Object
readonly
An
answer_key
is a key that will exist in the values hash of an answer. -
#input_keys ⇒ Object
readonly
An
input_key
is a key that uniquely identifies a single <input> tag. -
#option_hash ⇒ Object
readonly
hash of all options from input_key to the QuestionOption.
-
#question_hash ⇒ Object
readonly
Returns the value of attribute question_hash.
Instance Method Summary collapse
- #add(question) ⇒ Object
- #as_json ⇒ Object
- #check_key_clashes(new_answer_keys, new_input_keys) ⇒ Object
-
#description_for_variable(key) ⇒ Object
returns a human readable string description given a key of a question, question component (date components, checkbox options), score, flag or textvar.
-
#expand_input_keys(keys) ⇒ Object
Given a list of question and option keys returns a list of input-keys.
-
#initialize(questionnaire) ⇒ Fields
constructor
A new instance of Fields.
- #key_in_use?(key) ⇒ Boolean
Constructor Details
#initialize(questionnaire) ⇒ Fields
Returns a new instance of Fields.
26 27 28 29 30 31 32 |
# File 'lib/quby/questionnaires/entities/fields.rb', line 26 def initialize(questionnaire) @question_hash = HashWithIndifferentAccess.new @option_hash = HashWithIndifferentAccess.new @answer_keys = Set.new @input_keys = Set.new @questionnaire = questionnaire end |
Instance Attribute Details
#answer_keys ⇒ Object (readonly)
An answer_key
is a key that will exist in the values hash of an answer. This means that answer keys for radio’s will be just the question key, and answer keys for checkboxes will be the keys of all the options. These are the POST parameters when submitting the form, and so they must be globally unique or we won’t know which question the received data belongs to.
19 20 21 |
# File 'lib/quby/questionnaires/entities/fields.rb', line 19 def answer_keys @answer_keys end |
#input_keys ⇒ Object (readonly)
An input_key
is a key that uniquely identifies a single <input> tag. For radios, every radio option will have its own input key. This is needed because option keys must be globally unique so that they can be targeted by :depends_on relations.
24 25 26 |
# File 'lib/quby/questionnaires/entities/fields.rb', line 24 def input_keys @input_keys end |
#option_hash ⇒ Object (readonly)
hash of all options from input_key to the QuestionOption. Used by including applications to lookup the definition of e.g. a check_box question.
13 14 15 |
# File 'lib/quby/questionnaires/entities/fields.rb', line 13 def option_hash @option_hash end |
#question_hash ⇒ Object (readonly)
Returns the value of attribute question_hash.
9 10 11 |
# File 'lib/quby/questionnaires/entities/fields.rb', line 9 def question_hash @question_hash end |
Instance Method Details
#add(question) ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/quby/questionnaires/entities/fields.rb', line 34 def add(question) new_answer_keys = Set.new(question.answer_keys) new_input_keys = Set.new(question.input_keys) # This is probably the best place to ensure that keys don't collide. However,our current set of questionnaires # does have a few collisions between +v_1+ option +a9+ and its subquestion +v_1_a9+, so we have excluded # those questionnaires from this check through @questionnaire.check_key_clashes. check_key_clashes(new_answer_keys, new_input_keys) if @questionnaire.check_key_clashes @question_hash[question.key] = question @input_keys.merge(new_input_keys) @answer_keys.merge(new_answer_keys) question..each do |option| @option_hash[option.input_key] = option end end |
#as_json ⇒ Object
91 92 93 |
# File 'lib/quby/questionnaires/entities/fields.rb', line 91 def as_json question_hash end |
#check_key_clashes(new_answer_keys, new_input_keys) ⇒ Object
51 52 53 54 55 56 57 58 59 |
# File 'lib/quby/questionnaires/entities/fields.rb', line 51 def check_key_clashes(new_answer_keys, new_input_keys) if @answer_keys.intersect?(new_answer_keys) fail "Duplicate answer keys: #{@answer_keys.intersection(new_answer_keys).inspect}" end if @input_keys.intersect?(new_input_keys) fail "Duplicate input keys: #{@input_keys.intersection(new_input_keys).inspect}" end end |
#description_for_variable(key) ⇒ Object
returns a human readable string description given a key of a question, question component (date components, checkbox options), score, flag or textvar
82 83 84 85 86 87 88 89 |
# File 'lib/quby/questionnaires/entities/fields.rb', line 82 def description_for_variable(key) # for questionnaires where we do not check_key_clashes we cannot reliably retrace the variable keys, # since they contain conflicts between option keys and question keys # in order to be safe we return a string explaining the issue return "No description due to question/option key clash" if option_hash.key?(key) && question_hash.key?(key) variable_description(key) end |
#expand_input_keys(keys) ⇒ Object
Given a list of question and option keys returns a list of input-keys. If a given key is a question-key, adds the question.input_keys If a given key is an option-input-key it adds the given key. Raises an error if a key is not defined.
68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/quby/questionnaires/entities/fields.rb', line 68 def (keys) keys.reduce([]) do |ikeys, key| if question_hash.key?(key) ikeys += question_hash[key].input_keys elsif input_keys.include?(key.to_sym) ikeys << key else fail Entities::Questionnaire::UnknownInputKey, "Unknown input key #{key}" end end end |
#key_in_use?(key) ⇒ Boolean
61 62 63 |
# File 'lib/quby/questionnaires/entities/fields.rb', line 61 def key_in_use?(key) @question_hash.key?(key) || input_keys.include?(key.to_sym) end |