Class: Quby::Questionnaires::Entities::Panel

Inherits:
Item
  • Object
show all
Defined in:
lib/quby/questionnaires/entities/panel.rb

Instance Attribute Summary collapse

Attributes inherited from Item

#presentation, #raw_content, #switch_cycle

Instance Method Summary collapse

Methods inherited from Item

#to_codebook

Constructor Details

#initialize(options = {}) ⇒ Panel

Returns a new instance of Panel.



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

def initialize(options = {})
  @questionnaire = options[:questionnaire]
  @title = options[:title]
  @key = options[:key]
  @items = options[:items] || []
end

Instance Attribute Details

#itemsObject

Returns the value of attribute items.



8
9
10
# File 'lib/quby/questionnaires/entities/panel.rb', line 8

def items
  @items
end

#keyObject

Returns the value of attribute key.



9
10
11
# File 'lib/quby/questionnaires/entities/panel.rb', line 9

def key
  @key
end

#questionnaireObject (readonly)

Returns the value of attribute questionnaire.



10
11
12
# File 'lib/quby/questionnaires/entities/panel.rb', line 10

def questionnaire
  @questionnaire
end

#titleObject

Returns the value of attribute title.



7
8
9
# File 'lib/quby/questionnaires/entities/panel.rb', line 7

def title
  @title
end

Instance Method Details

#as_json(options = {}) ⇒ Object



19
20
21
# File 'lib/quby/questionnaires/entities/panel.rb', line 19

def as_json(options = {})
  super.merge(title: title, index: index, items: json_items)
end

#indexObject



23
24
25
# File 'lib/quby/questionnaires/entities/panel.rb', line 23

def index
  @questionnaire.panels.index(self)
end

#json_itemsObject



47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/quby/questionnaires/entities/panel.rb', line 47

def json_items
  items.map do |item|
    case item
    when Text
      { type: 'html', html: item.html }
    when Question
      next if item.table # things inside a table are added to the table, AND ALSO to the panel. skip them.
      { type: 'question', key: item.key }
    when Table
      { type: "table" }
    end
  end.compact
end

#nextObject



27
28
29
30
31
32
33
34
35
# File 'lib/quby/questionnaires/entities/panel.rb', line 27

def next
  this_panel_index = index

  if this_panel_index < @questionnaire.panels.size
    return @questionnaire.panels[this_panel_index + 1]
  else
    nil
  end
end

#prevObject



37
38
39
40
41
42
43
44
45
# File 'lib/quby/questionnaires/entities/panel.rb', line 37

def prev
  this_panel_index = index

  if this_panel_index > 0
    return @questionnaire.panels[this_panel_index - 1]
  else
    nil
  end
end

#validationsObject



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/quby/questionnaires/entities/panel.rb', line 61

def validations
  vals = {}
  items.each do |item|
    if item.is_a? Question
      item.options.each do |opt|
        if opt.questions
          opt.questions.each do |q|
            vals[q.key] = q.validations
          end
        end
      end
      vals[item.key] = item.validations
    end
  end
  vals
end