Class: Quby::Questionnaires::DSL::PanelBuilder

Inherits:
Base
  • Object
show all
Defined in:
lib/quby/questionnaires/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, options = {}) ⇒ PanelBuilder

Returns a new instance of PanelBuilder.



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

def initialize(title, options = {})
  @panel = Entities::Panel.new(options.merge(title: title, items: []))
  @default_question_options = 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, &block) ⇒ Object



66
67
68
69
70
71
72
# File 'lib/quby/questionnaires/dsl/panel_builder.rb', line 66

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

Instance Attribute Details

#questionnaireObject (readonly)

Returns the value of attribute questionnaire.



10
11
12
# File 'lib/quby/questionnaires/dsl/panel_builder.rb', line 10

def questionnaire
  @questionnaire
end

#title(value) ⇒ Object (readonly)

Returns the value of attribute title.



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

def title
  @title
end

Instance Method Details

#buildObject



19
20
21
# File 'lib/quby/questionnaires/dsl/panel_builder.rb', line 19

def build
  @panel
end

#default_question_options(options = {}) ⇒ Object



44
45
46
# File 'lib/quby/questionnaires/dsl/panel_builder.rb', line 44

def default_question_options(options = {})
  @default_question_options = @default_question_options.merge(options)
end

#html(value) ⇒ Object



31
32
33
# File 'lib/quby/questionnaires/dsl/panel_builder.rb', line 31

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

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



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

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



35
36
37
# File 'lib/quby/questionnaires/dsl/panel_builder.rb', line 35

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)


74
75
76
# File 'lib/quby/questionnaires/dsl/panel_builder.rb', line 74

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

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



59
60
61
62
63
64
# File 'lib/quby/questionnaires/dsl/panel_builder.rb', line 59

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

#text(value, options = {}) ⇒ Object



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

def text(value, options = {})
  @panel.items << Entities::Text.new(value.to_s, options)
end

#video(*urls, **options) ⇒ Object



39
40
41
42
# File 'lib/quby/questionnaires/dsl/panel_builder.rb', line 39

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