Class: Quby::Questionnaires::Entities::Questions::DateQuestion

Inherits:
Quby::Questionnaires::Entities::Question show all
Defined in:
lib/quby/questionnaires/entities/questions/date_question.rb

Constant Summary collapse

POSSIBLE_COMPONENTS =
%i( day month year hour minute )
COMPONENT_KEYS =
Hash[POSSIBLE_COMPONENTS.zip %w( dd mm yyyy hh ii)]
COMPONENT_PLACEHOLDERS =
Hash[POSSIBLE_COMPONENTS.zip %w( DD MM YYYY hh mm)]
DEFAULT_COMPONENTS =
%i( day month year )

Constants inherited from Quby::Questionnaires::Entities::Question

Quby::Questionnaires::Entities::Question::MARKDOWN_ATTRIBUTES

Instance Attribute Summary collapse

Attributes inherited from Quby::Questionnaires::Entities::Question

#allow_blank_titles, #allow_duplicate_option_values, #as, #autocomplete, #col_span, #cols, #context_free_title, #default_invisible, #default_position, #dependencies, #depends_on, #description, #deselectable, #disallow_bulk, #display_modes, #extra_data, #group_maximum_answered, #group_minimum_answered, #hidden, #input_data, #key, #labels, #lines, #maximum, #minimum, #options, #parent, #parent_option_key, #question_group, #questionnaire, #row_span, #sbg_key, #score_header, #sets_textvar, #show_values, #size, #table, #title, #type, #unit, #validations

Attributes inherited from Item

#presentation, #raw_content, #switch_cycle

Instance Method Summary collapse

Methods inherited from Quby::Questionnaires::Entities::Question

#codebook_key, #codebook_output_range, #codebook_output_type, #expand_depends_on_input_keys, #hidden?, #html_id, #input_keys, #key_in_use?, #set_depends_on, #show_values_in_mode?, #subquestion?, #subquestions, #view_selector

Constructor Details

#initialize(key, options = {}) ⇒ DateQuestion

Returns a new instance of DateQuestion.



24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/quby/questionnaires/entities/questions/date_question.rb', line 24

def initialize(key, options = {})
  super

  @components = options[:components] || DEFAULT_COMPONENTS
  @required_components = options[:required_components] || @components
  @optional_components = @components - @required_components

  components.each do |component|
    component_key = options[:"#{component}_key"] || "#{key}_#{COMPONENT_KEYS[component]}"
    instance_variable_set("@#{component}_key", component_key.to_sym)
  end

  add_date_validation(options[:error_explanation])
end

Instance Attribute Details

#componentsArray<Symbol>

Returns date parts to show.

Returns:

  • (Array<Symbol>)

    date parts to show



22
23
24
# File 'lib/quby/questionnaires/entities/questions/date_question.rb', line 22

def components
  @components
end

#optional_componentsObject

Returns the value of attribute optional_components.



22
23
24
# File 'lib/quby/questionnaires/entities/questions/date_question.rb', line 22

def optional_components
  @optional_components
end

#required_componentsArray<Symbol>

Returns date parts that are required if the question is required or partly filled out.

Returns:

  • (Array<Symbol>)

    date parts that are required if the question is required or partly filled out.



22
# File 'lib/quby/questionnaires/entities/questions/date_question.rb', line 22

attr_accessor :components, :required_components, :optional_components

Instance Method Details

#add_date_validation(explanation) ⇒ Object



39
40
41
42
43
# File 'lib/quby/questionnaires/entities/questions/date_question.rb', line 39

def add_date_validation(explanation)
  @validations << {type: :valid_date,
                   subtype: :"valid_date_#{components.sort.join('_')}",
                   explanation: explanation}
end

#answer_keysObject



49
50
51
52
53
# File 'lib/quby/questionnaires/entities/questions/date_question.rb', line 49

def answer_keys
  components.map do |component|
    send("#{component}_key").to_sym
  end
end

#as_json(options = {}) ⇒ Object



62
63
64
65
66
67
# File 'lib/quby/questionnaires/entities/questions/date_question.rb', line 62

def as_json(options = {})
  component_keys = components.each_with_object({}) do |component, hash|
    hash["#{component}Key"] = send("#{component}_key")
  end
  super.merge(components: components).merge(component_keys)
end

#claimed_keysObject



45
46
47
# File 'lib/quby/questionnaires/entities/questions/date_question.rb', line 45

def claimed_keys
  [key] + answer_keys
end

#to_codebook(questionnaire, opts = {}) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
# File 'lib/quby/questionnaires/entities/questions/date_question.rb', line 69

def to_codebook(questionnaire, opts = {})
  output = []
  components.each do |component|
    output << "#{codebook_key(send("#{component}_key"), questionnaire, opts)} " \
    "#{type}_#{component} #{codebook_output_range}"
    output << "\"#{title}\"" unless title.blank?
    output << options.map(&:to_codebook).join("\n") unless options.blank?
    output << ""
  end
  output.join("\n")
end

#variable_descriptionsObject



55
56
57
58
59
60
# File 'lib/quby/questionnaires/entities/questions/date_question.rb', line 55

def variable_descriptions
  components.each_with_object(key => context_free_title) do |component, hash|
    key = send("#{component}_key")
    hash[key] = "#{context_free_title} (#{I18n.t component})"
  end.with_indifferent_access
end