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_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



68
69
70
71
72
73
74
# File 'lib/quby/compiler/dsl/panel_builder.rb', line 68

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



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

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

#html(value) ⇒ Object



30
31
32
# File 'lib/quby/compiler/dsl/panel_builder.rb', line 30

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

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



47
48
49
50
51
52
53
54
55
56
# File 'lib/quby/compiler/dsl/panel_builder.rb', line 47

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) ⇒ Object



34
35
36
# File 'lib/quby/compiler/dsl/panel_builder.rb', line 34

def raw_html(value)
  @panel.items << Entities::Text.new('', raw_content: value.to_s)
end

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

Returns:

  • (Boolean)


76
77
78
# File 'lib/quby/compiler/dsl/panel_builder.rb', line 76

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

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



58
59
60
61
62
63
64
65
66
# File 'lib/quby/compiler/dsl/panel_builder.rb', line 58

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, options = {}) ⇒ Object



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

def text(value, options = {})
  @panel.items << Entities::Text.new(value.to_s, 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, **options) ⇒ Object



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

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