# frozen_string_literal: true module Quby module Compiler module Entities module Questions class DateQuestion < Question 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 ) # For optionally giving year, month and day fields of dates their own keys POSSIBLE_COMPONENTS.each do |component| attr_accessor "#{component}_key".to_sym end # @!attribute [r] components # @return [Array] date parts to show # @!attribute [r] required_components # @return [Array] date parts that are required if the question is required or partly filled out. attr_accessor :components, :required_components, :optional_components 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 def add_date_validation(explanation) @validations << {type: :valid_date, subtype: :"valid_date_#{components.sort.join('_')}", explanation: explanation, requiredParts: required_components} end def claimed_keys [key] + answer_keys end def answer_keys components.map do |component| send("#{component}_key").to_sym end end def variable_descriptions components.each_with_object(key => context_free_title_or_title) do |component, hash| key = send("#{component}_key") hash[key] = "#{context_free_title_or_title} (#{I18n.t component})" end.with_indifferent_access end def as_json(options = {}) super.merge( type: 'date_parts', as: 'date_parts', dateParts: components.map { |component| { part: component, key: send("#{component}_key") } } ) end end end end end end