Class: Quby::Questionnaires::Entities::Table

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

Instance Attribute Summary collapse

Attributes inherited from Item

#presentation, #raw_content, #switch_cycle

Instance Method Summary collapse

Methods inherited from Item

#as_json

Constructor Details

#initialize(options = {}) ⇒ Table

Returns a new instance of Table.



18
19
20
21
22
23
24
# File 'lib/quby/questionnaires/entities/table.rb', line 18

def initialize(options = {})
  @columns = options[:columns]
  @title = options[:title]
  @description = options[:description]
  @show_option_desc = options[:show_option_desc] || false
  @items = []
end

Instance Attribute Details

#columnsObject

Returns the value of attribute columns.



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

def columns
  @columns
end

#descriptionObject

Returns the value of attribute description.



13
14
15
# File 'lib/quby/questionnaires/entities/table.rb', line 13

def description
  @description
end

#itemsObject

Returns the value of attribute items.



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

def items
  @items
end

#show_option_descObject

Whether the question options in this table should show their descriptions



16
17
18
# File 'lib/quby/questionnaires/entities/table.rb', line 16

def show_option_desc
  @show_option_desc
end

#titleObject

Returns the value of attribute title.



12
13
14
# File 'lib/quby/questionnaires/entities/table.rb', line 12

def title
  @title
end

Instance Method Details

#item_tableObject



26
27
28
29
# File 'lib/quby/questionnaires/entities/table.rb', line 26

def item_table
  rows
  @item_table
end

#rowsObject

FIXME: code to be ashamed of rubocop:disable CyclomaticComplexity, Metrics/MethodLength



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/quby/questionnaires/entities/table.rb', line 33

def rows
  return @rows if @rows
  @item_table = [[]]
  @rows = [[[]]]
  filled_columns = 0
  filled_rows = 0
  row_items = 0

  skips = []
  items.each do |item|

    skips.delete_if do |row_span, skip_cols_at, skip_cols_length|
      next true if row_span == 1
      if skip_cols_length > 0 and filled_columns == skip_cols_at
        filled_columns += skip_cols_length
        skip_cols_length.times do
          @item_table[filled_rows] << nil
          @rows[filled_rows][row_items] << nil
        end
        if filled_columns >= columns and item != items.last
          filled_rows += 1
          filled_columns = 0
          row_items = 0
          @rows << [[]]
          @item_table << []
          skips.map! do |new_row_span, new_skip_cols_at, new_skip_cols_length|
            [new_row_span - 1, new_skip_cols_at, new_skip_cols_length]
          end
        end
        if filled_columns != 0 and item != items.last
          row_items += 1
          @rows[filled_rows] << []
        end
        next row_span - 1 == 1
      end
    end

    if item.is_a?(Text) || !([:check_box, :radio, :scale].include? item.type)
      if item.row_span > 1
        skips << [item.row_span, filled_columns, item.col_span]
      end

      @item_table[filled_rows] << item
      @rows[filled_rows][row_items] << item
      filled_columns += item.col_span

      if filled_columns >= columns and item != items.last
        filled_rows += 1
        filled_columns = 0
        row_items = 0
        @rows << [[]]
        @item_table << []
      end
      if filled_columns != 0 and item != items.last
        row_items += 1
        @rows[filled_rows] << []
      end

    else # is :check_box, :radio or :scale question
      if item.row_span > 1
        skips << [item.row_span, filled_columns, item.options.length]
      end

      @item_table[filled_rows] << item
      if item.options.length <= columns # multiple questions on one row
        item.options.each do |opt|
          @rows[filled_rows][row_items] << opt
          filled_columns += 1

          if filled_columns >= columns and item != items.last
            filled_rows += 1
            filled_columns = 0
            row_items = 0
            @rows << [[]]
            @item_table << []
          end
        end
        if filled_columns != 0 and item != items.last
          row_items += 1
          @rows[filled_rows] << []
        end
      else # one question's options split over multiple rows, ordered row wise
        opt_len = item.options.length
        col_len = (opt_len / columns.to_f).ceil
        (0...col_len).each do |j|
          (0...columns).each do |i|
            break if j + i * col_len >= opt_len
            @rows[filled_rows][row_items] << item.options[j + i * col_len]
            filled_columns += 1
            if filled_columns == columns
              filled_rows += 1
              filled_columns = 0
              @rows << [[]]
              @item_table << [item]
            end
          end
        end
      end
    end
  end
  @rows
end

#typeObject

rubocop:enable CyclomaticComplexity, Metrics/MethodLength



137
138
139
# File 'lib/quby/questionnaires/entities/table.rb', line 137

def type
  "table"
end