Class: Rubypivot::TableBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/rubypivot/table.rb

Overview

Create HTML table from array having row (tr) and cell (td) attribute control rubypivot main gem does not include this, maybe won’t be using

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data_array, options = {}) ⇒ TableBuilder

Returns a new instance of TableBuilder.

Raises:

  • (StandardError)


10
11
12
13
14
15
16
17
18
19
20
# File 'lib/rubypivot/table.rb', line 10

def initialize(data_array, options = {})
  raise StandardError, "Data source must be an two dimension array" if !data_array.is_a?(Array) || !data_array.first.is_a?(Array)
  @options = options
  @data_array = data_array
  @attributes = []
  @data_array.each do |line|
    @attributes << [options[:tr_class]]
  end
  @x_size = @data_array.first.size
  @y_size = @data_array.size
end

Instance Attribute Details

#optionsObject

Returns the value of attribute options.



8
9
10
# File 'lib/rubypivot/table.rb', line 8

def options
  @options
end

#x_sizeObject (readonly)

Returns the value of attribute x_size.



9
10
11
# File 'lib/rubypivot/table.rb', line 9

def x_size
  @x_size
end

#y_sizeObject (readonly)

Returns the value of attribute y_size.



9
10
11
# File 'lib/rubypivot/table.rb', line 9

def y_size
  @y_size
end

Instance Method Details

#build(options = {}) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/rubypivot/table.rb', line 22

def build(options = {})
  tr_classes(options[:tr_class]) if options[:tr_class]
  res = open
  @data_array.each_with_index do |line, y|
    tr = HtmlTag.new("tr", class: @attributes[y][0])
    res << tr.open
    line.each_with_index do |td_data, x|
      res << HtmlTag.new("td", class: @attributes[y][x + 1]).build{ td_data }
    end
    res << tr.close + "\n"
  end
  # res << "\n"
  res << close
  res
end

#close(options = {}) ⇒ Object



44
45
46
# File 'lib/rubypivot/table.rb', line 44

def close(options = {})
  "</table>\n"
end

#column_attributes(klass, x) ⇒ Object



88
89
90
91
92
93
# File 'lib/rubypivot/table.rb', line 88

def column_attributes(klass, x)
  return unless klass
  0.upto(@attributes.size - 1) do |y|
    @attributes[y][x + 1] = klass
  end
end

#openObject



38
39
40
41
42
# File 'lib/rubypivot/table.rb', line 38

def open
  res = HtmlTag.new('table', @options).open
  res << "\n"
  res 
end

#range_check(y) ⇒ Object

Raises:

  • (StandardError)


48
49
50
51
52
53
54
55
56
57
58
# File 'lib/rubypivot/table.rb', line 48

def range_check(y)
  if y.is_a?(Symbol)
    if y == :bottom
      return @y_size - 1
    else
      return 0
    end
  end
  raise StandardError, "Class set out of range: #{y} > #{@y_size}" if y > @y_size
  y
end

#row_attributes(klass, y) ⇒ Object



80
81
82
83
84
85
86
# File 'lib/rubypivot/table.rb', line 80

def row_attributes(klass, y)
  return unless klass
  y = range_check(y)
  1.upto(@x_size) do |pos|
    @attributes[y][pos] = klass
  end
end

#set_class(klass, y, x) ⇒ Object

Raises:

  • (StandardError)


73
74
75
76
77
78
# File 'lib/rubypivot/table.rb', line 73

def set_class(klass, y, x)
  return unless klass
  y = range_check(y)
  raise StandardError, "Class set out of range: #{} > #{@x_size}" if x >= @x_size
  @attributes[y][x + 1] = klass
end

#tr_class(klass, y) ⇒ Object



60
61
62
63
64
# File 'lib/rubypivot/table.rb', line 60

def tr_class(klass, y)
  return unless klass
  y = range_check(y)
  @attributes[y][0] = klass
end

#tr_classes(klass) ⇒ Object



66
67
68
69
70
71
# File 'lib/rubypivot/table.rb', line 66

def tr_classes(klass)
  return unless klass
  0.upto(@attributes.size - 1) do |y|
    @attributes[y][0] = klass
  end
end