Class: Greentable::Table

Inherits:
Object
  • Object
show all
Defined in:
lib/greentable/greentable_table.rb

Overview

class Column

attr_accessor :attributes, :html, :tag

end

Instance Method Summary collapse

Constructor Details

#initialize(parent, records, opts) ⇒ Table

Returns a new instance of Table.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/greentable/greentable_table.rb', line 9

def initialize(parent, records, opts)
  @parent = parent
  @records = records
  @opts_tr = opts.delete(:tr) || {}
  @opts_td = opts.delete(:td) || {}
  @opts_th = opts.delete(:th) || {}
  @opts = opts

  @tr_attributes = []

  @th_attributes = []
  @th_html = []

  @td_attributes = []
  @td_html = []

end

Instance Method Details

#greencolumn(th = nil, opts = {}, &block) ⇒ Object

def current_row

@rows_attributes[@counter.rows]

end



39
40
41
42
43
44
45
46
47
48
# File 'lib/greentable/greentable_table.rb', line 39

def greencolumn(th = nil, opts = {}, &block)
  @th_html[@current_col] = th

  @td_html[@row_counter.i] ||= []
  @td_html[@row_counter.i][@current_col] = @parent.capture(&block)
  @td_attributes[@row_counter.i] = opts

  @current_col += 1
  return nil
end

#greensubrow(opts = {}, &block) ⇒ Object



27
28
29
30
31
32
33
# File 'lib/greentable/greentable_table.rb', line 27

def greensubrow(opts = {}, &block)
  return if capture_headers
  return if opts[:display_on] == :first && !@row_counter.first?
  return if opts[:display_on] == :last && !@row_counter.last?
  #@th_html[@col_counter] = th
  #@td_html[@col_counter] = capture(@records[@row_counter.i], self, &block)
end

#process(&block) ⇒ Object



50
51
52
53
54
55
56
57
58
# File 'lib/greentable/greentable_table.rb', line 50

def process(&block)
  @row_counter = Counter.new(@records.size)

  @records.each do |record|
    @current_col = 0
    block.call(self, record)
    @row_counter.inc
  end
end

#to_sObject



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/greentable/greentable_table.rb', line 60

def to_s
  ret = ""
  ret << "<table#{do_attributes(@opts)}>"
  ret << "<thead>"
  ret << "<tr>"
  @th_html.each do |th|
    ret << "<th#{do_attributes(@opts_th)}>#{th}</th>"
  end
  ret << "</tr>"
  ret << "</thead>"
  ret << "<tbody>"

  @row_counter.i.times do |i|
    ret << "<tr#{do_attributes(@opts_tr.merge(@tr_attributes[i]||{}))}>"
    @td_html[i].each do |td|
      ret << "<td#{do_attributes(@opts_td.merge(@td_attributes[i]||{}))}>#{td}</td>"
    end
    ret << "</tr>"
  end
  ret << "</tbody>"
  ret << "</table>"
end