Class: Quby::Compiler::DSL::Questions::SelectQuestionBuilder

Inherits:
Base
  • Object
show all
Includes:
CompareVisibilityRule
Defined in:
lib/quby/compiler/dsl/questions/select_question_builder.rb

Instance Attribute Summary

Attributes inherited from Base

#questionnaire

Instance Method Summary collapse

Methods included from CompareVisibilityRule

#compare_value

Methods inherited from Base

#build, #context_description, #context_free_title, #default_position, #depends_on, #description, #hidden, #presentation, #title, #validates_presence_of_answer

Methods inherited from Base

build

Methods included from Helpers

#check_key_uniqueness, #check_question_keys_uniqueness, #image_alt, #image_tag, #video_tag

Constructor Details

#initialize(key, **options, &block) ⇒ SelectQuestionBuilder

Returns a new instance of SelectQuestionBuilder.



10
11
12
13
14
# File 'lib/quby/compiler/dsl/questions/select_question_builder.rb', line 10

def initialize(key, **options, &block)
  super
  @question = Entities::Questions::SelectQuestion.new(key, options)
  @current_optgroup = nil
end

Instance Method Details

#optgroup(key, label:, &block) ⇒ Object



16
17
18
19
20
21
# File 'lib/quby/compiler/dsl/questions/select_question_builder.rb', line 16

def optgroup(key, label: , &block)
  raise "Cannot nest optgroups" if @current_optgroup
  @question.options << @current_optgroup = Entities::QuestionOptgroup.new(key, label:)
  instance_eval(&block)
  @current_optgroup = nil
end

#option(key, options = {}, &block) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/quby/compiler/dsl/questions/select_question_builder.rb', line 23

def option(key, options = {}, &block)
  question_option = Entities::QuestionOption.new(key, @question, options)
  if @questionnaire.key_in_use?(question_option.input_key) || @question.key_in_use?(question_option.input_key)
    fail "#{questionnaire.key}:#{@question.key}:#{question_option.key}: " \
          "A question or option with input key #{question_option.input_key} is already defined."
  end

  if @current_optgroup
    @current_optgroup.options << question_option
  else
    @question.options << question_option
  end
end