Class: Quby::Compiler::DSL::InfoBlockBuilder

Inherits:
Base
  • Object
show all
Defined in:
lib/quby/compiler/dsl/info_block_builder.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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, panel:, default_question_options:, custom_methods:, **options) ⇒ InfoBlockBuilder

Returns a new instance of InfoBlockBuilder.



14
15
16
17
18
19
20
21
# File 'lib/quby/compiler/dsl/info_block_builder.rb', line 14

def initialize(key, panel:, default_question_options:, custom_methods:, **options)
  @info_block = Entities::InfoBlock.new(key: , panel:, **options)
  @default_question_options = default_question_options
  @custom_methods = custom_methods
  check_key_uniqueness(key, questionnaire:)
  questionnaire.fields.info_block_keys << key
  panel.items << @info_block
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_sym, *args, **kwargs, &block) ⇒ Object



47
48
49
50
51
52
53
# File 'lib/quby/compiler/dsl/info_block_builder.rb', line 47

def method_missing(method_sym, *args, **kwargs, &block)
  if @custom_methods.key? method_sym
    instance_exec(*args, **kwargs, &@custom_methods[method_sym])
  else
    super
  end
end

Instance Attribute Details

#default_question_optionsObject (readonly)

Returns the value of attribute default_question_options.



9
10
11
# File 'lib/quby/compiler/dsl/info_block_builder.rb', line 9

def default_question_options
  @default_question_options
end

#info_blockObject (readonly)

Returns the value of attribute info_block.



9
10
11
# File 'lib/quby/compiler/dsl/info_block_builder.rb', line 9

def info_block
  @info_block
end

Instance Method Details

#html(value, key: fallback_key) ⇒ Object



28
29
30
# File 'lib/quby/compiler/dsl/info_block_builder.rb', line 28

def html(value, key: fallback_key)
  info_block.items << Entities::Text.new('', html_content: value.to_s, key:)
end

#info_html(value) ⇒ Object



23
24
25
26
# File 'lib/quby/compiler/dsl/info_block_builder.rb', line 23

def info_html(value)
  raise "Can only add one info_html item per info_block" if info_block.html.present?
  info_block.html = value
end

#question(key, **options, &block) ⇒ Object



36
37
38
39
40
41
42
43
44
45
# File 'lib/quby/compiler/dsl/info_block_builder.rb', line 36

def question(key, **options, &block)
  options = default_question_options.merge(options).merge(questionnaire:)

  check_question_keys_uniqueness key, options, questionnaire

  question = QuestionBuilder.build(key, **options, &block)

  questionnaire.register_question(question)
  info_block.items << question
end

#text(key:, html: nil, md: nil, **options) ⇒ Object



32
33
34
# File 'lib/quby/compiler/dsl/info_block_builder.rb', line 32

def text(key:, html: nil, md: nil, **options)
  info_block.items << Entities::Text.new(key:, md:, html_content: html, **options)
end