class Quby::Questionnaires::Entities::Fields

Attributes

answer_keys[R]

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.

input_keys[R]

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.

option_hash[R]

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.

question_hash[R]

Public Class Methods

new(questionnaire) click to toggle source
# 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

Public Instance Methods

add(question) click to toggle source
# 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)

  @question_hash[question.key] = question
  @input_keys.merge(new_input_keys)
  @answer_keys.merge(new_answer_keys)
  question.options.each do |option|
    @option_hash[option.input_key] = option
  end
end
as_json() click to toggle source
# File lib/quby/questionnaires/entities/fields.rb, line 76
def as_json
  question_hash
end
description_for_variable(key) click to toggle source

returns a human readable string description given a key of a question, question component (date components, checkbox options), score, flag or textvar

# File lib/quby/questionnaires/entities/fields.rb, line 67
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) click to toggle source

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.

# File lib/quby/questionnaires/entities/fields.rb, line 53
def expand_input_keys(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) click to toggle source
# File lib/quby/questionnaires/entities/fields.rb, line 46
def key_in_use?(key)
  @question_hash.key?(key) || input_keys.include?(key.to_sym)
end

Private Instance Methods

score_descriptions() click to toggle source
# File lib/quby/questionnaires/entities/fields.rb, line 93
def score_descriptions
  @score_variable_descriptions ||=
    @questionnaire.score_schemas.values.map do |score_schema|
      score_schema.subscore_schemas.map do |subschema|
        [subschema.export_key, "#{score_schema.label} #{subschema.label}"]
      end
    end.flatten(1).to_h.with_indifferent_access
end
variable_description(key) click to toggle source

warning, will contain a result even if option/answer key clashes exist for a given key

# File lib/quby/questionnaires/entities/fields.rb, line 83
def variable_description(key)
  @question_variable_descriptions ||= @questionnaire.questions
                                                    .map(&:variable_descriptions)
                                                    .reduce({}, &:merge!)
  @question_variable_descriptions[key] ||
    score_descriptions[key] ||
    @questionnaire.flags[key]&.variable_description ||
    @questionnaire.textvars[key]&.description
end