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, #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

#hidden?, #html_id, #input_keys, #key_in_use?, #maximum, #minimum, #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
# 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
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

#answer_keysObject



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

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

#as_json(options = {}) ⇒ Object



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

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



37
38
39
# File 'lib/quby/questionnaires/entities/questions/date_question.rb', line 37

def claimed_keys
  [key] + answer_keys
end