Class: Gitter::Table

Inherits:
Object
  • Object
show all
Extended by:
ActionView::Helpers::OutputSafetyHelper, ActionView::Helpers::TagHelper
Defined in:
lib/gitter/table.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(title, x_axis, y_axis, data, opts = {}) ⇒ Table

axis: responds to each, yielding [key,title] or key

# data: hash from [x_key,y_key] to cell_array


62
63
64
65
66
67
68
69
70
# File 'lib/gitter/table.rb', line 62

def initialize title, x_axis, y_axis, data, opts = {}
  @title, @x_axis, @y_axis, @data, @opts = title, x_axis, y_axis, data, opts
  @cells = data.dup
  if label = opts[:show_sums]
    add_sums
    @x_axis = add_sum_label_to_axis @x_axis, label
    @y_axis = add_sum_label_to_axis @y_axis, label
  end
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



58
59
60
# File 'lib/gitter/table.rb', line 58

def data
  @data
end

#optsObject (readonly)

Returns the value of attribute opts.



58
59
60
# File 'lib/gitter/table.rb', line 58

def opts
  @opts
end

#titleObject (readonly)

Returns the value of attribute title.



58
59
60
# File 'lib/gitter/table.rb', line 58

def title
  @title
end

#x_axisObject (readonly)

Returns the value of attribute x_axis.



58
59
60
# File 'lib/gitter/table.rb', line 58

def x_axis
  @x_axis
end

#y_axisObject (readonly)

Returns the value of attribute y_axis.



58
59
60
# File 'lib/gitter/table.rb', line 58

def y_axis
  @y_axis
end

Class Method Details

.tag(tag, content, opts = {}) ⇒ Object



53
54
55
56
# File 'lib/gitter/table.rb', line 53

def self.tag tag, content, opts = {}
  opts = opts.merge(class: "#{opts[:class]} grid pivot")
   tag, raw(content), opts
end

Instance Method Details

#empty?Boolean

Returns:

  • (Boolean)


72
73
74
# File 'lib/gitter/table.rb', line 72

def empty?
  data.empty?
end

#html(opts = {}) ⇒ Object



93
94
95
96
97
98
99
100
# File 'lib/gitter/table.rb', line 93

def html opts = {}
  @html ||= begin
    h = rows.map do |row|
      Table.tag :tr, (row.map{|cell| cell.html} * "\n"), (opts[:tr_html]||{})
    end * "\n"
	Table.tag :table, h, (opts[:table_html]||{})
  end
end

#rowsObject



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/gitter/table.rb', line 76

def rows
  @rows ||= begin
    rows = []
    rows << x_header if x_header

    rows + (y_axis||[nil]).map do |y,y_title|
       row = (x_axis||[nil]).map do |x,x_title|
         cell = @cells[cell_key(x,y)]
         cell = yield cell, x, y if block_given?
         TableCell.new x, y, cell
end
       row.unshift TableHeaderCell.new(y_title||y) if y_axis
row
    end
  end 
end