Class: Quby::Compiler::Entities::Questions::DateQuestion

Inherits:
Quby::Compiler::Entities::Question show all
Defined in:
lib/quby/compiler/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::Compiler::Entities::Question

Quby::Compiler::Entities::Question::MARKDOWN_ATTRIBUTES

Instance Attribute Summary collapse

Attributes inherited from Quby::Compiler::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, #title_question, #type, #unit, #validations

Attributes inherited from Item

#presentation, #raw_content, #switch_cycle

Instance Method Summary collapse

Methods inherited from Quby::Compiler::Entities::Question

#context_free_title_or_title, #expand_depends_on_input_keys, #hidden?, #html_id, #input_keys, #key_in_use?, #maximum, #minimum, #set_depends_on, #show_values_in_mode?, #subquestion?, #subquestions, #title_question?, #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
38
# File 'lib/quby/compiler/entities/questions/date_question.rb', line 24

def initialize(key, options = {})
  super

  # only possible order we support.
  @components = options[:components] ? POSSIBLE_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/compiler/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/compiler/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/compiler/entities/questions/date_question.rb', line 22

attr_accessor :components, :required_components, :optional_components

Instance Method Details

#add_date_validation(explanation) ⇒ Object



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

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

#answer_keysObject



51
52
53
54
55
# File 'lib/quby/compiler/entities/questions/date_question.rb', line 51

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

#as_json(options = {}) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/quby/compiler/entities/questions/date_question.rb', line 57

def as_json(options = {})
  super.merge(
    type: 'date_parts',
    as: 'date_parts',
    dateParts: components.map { |component|
      {
        part: component,
        key: send("#{component}_key")
      }
    }
  )
end

#claimed_keysObject



47
48
49
# File 'lib/quby/compiler/entities/questions/date_question.rb', line 47

def claimed_keys
  [key] + answer_keys
end