Class: Quby::Compiler::DSL::PanelBuilder

Inherits:
Base
  • Object
show all
Defined in:
lib/quby/compiler/dsl/panel_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(title, default_question_options: {}, **options) ⇒ PanelBuilder

Returns a new instance of PanelBuilder.



11
12
13
14
15
16
# File 'lib/quby/compiler/dsl/panel_builder.rb', line 11

def initialize(title, default_question_options: {}, **options)
  @panel = Entities::Panel.new({title: title, items: [], **options})
  @default_question_options = default_question_options
  @questionnaire = options[:questionnaire]
  @custom_methods = options[:custom_methods] || {}
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



81
82
83
84
85
86
87
# File 'lib/quby/compiler/dsl/panel_builder.rb', line 81

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

#questionnaireObject (readonly)

Returns the value of attribute questionnaire.



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

def questionnaire
  @questionnaire
end

Instance Method Details

#buildObject



18
19
20
# File 'lib/quby/compiler/dsl/panel_builder.rb', line 18

def build
  @panel
end

#default_question_options(**options) ⇒ Object



46
47
48
# File 'lib/quby/compiler/dsl/panel_builder.rb', line 46

def default_question_options(**options)
  @default_question_options = @default_question_options.merge(options)
end

#html(value, key: fallback_key) ⇒ Object

deprecated for translated questionnaires, use ‘text html: ..` instead.



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

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

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



71
72
73
74
75
76
77
78
79
# File 'lib/quby/compiler/dsl/panel_builder.rb', line 71

def info_block(key, **options, &block)
  info_block_builder = DSL::InfoBlockBuilder.new \
    key,
    panel: @panel,
    default_question_options: @default_question_options,
    custom_methods: @custom_methods,
    **options
  info_block_builder.instance_eval(&block)
end

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



50
51
52
53
54
55
56
57
58
59
# File 'lib/quby/compiler/dsl/panel_builder.rb', line 50

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

  check_question_keys_uniqueness key, options, @questionnaire

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

  @questionnaire.register_question(question)
  @panel.items << question
end

#raw_html(value = "", key: fallback_key) ⇒ Object

Deprecated for quby2.



37
38
39
# File 'lib/quby/compiler/dsl/panel_builder.rb', line 37

def raw_html(value="", key: fallback_key)
  @panel.items << Entities::Text.new(key:, raw_content: value.to_s)
end

#respond_to_missing?(method_name, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


89
90
91
# File 'lib/quby/compiler/dsl/panel_builder.rb', line 89

def respond_to_missing?(method_name, include_private = false)
  @custom_methods.key?(method_name) || super
end

#table(**options, &block) ⇒ Object



61
62
63
64
65
66
67
68
69
# File 'lib/quby/compiler/dsl/panel_builder.rb', line 61

def table(**options, &block)
  table_builder = TableBuilder.new \
    @panel,
    questionnaire: @panel.questionnaire,
    default_question_options: @default_question_options,
    custom_methods: @custom_methods,
    **options
  table_builder.instance_eval(&block) if block
end

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

value deprecated, used named md attr instead.



27
28
29
# File 'lib/quby/compiler/dsl/panel_builder.rb', line 27

def text(value=nil, key: fallback_key, md: value, html: nil, **options)
  @panel.items << Entities::Text.new(key:, md:, html_content: html, **options)
end

#title(value) ⇒ Object



22
23
24
# File 'lib/quby/compiler/dsl/panel_builder.rb', line 22

def title(value)
  @panel.title = value
end

#video(*urls, key: fallback_key, **options) ⇒ Object



41
42
43
44
# File 'lib/quby/compiler/dsl/panel_builder.rb', line 41

def video(*urls, key: fallback_key, **options)
  video_html = video_tag(*urls, **options)
  @panel.items << Entities::Text.new(key:, raw_content: video_html)
end