Class: Card::Set::All::Bootstrap::Table::HtmlFormat::TableHelper

Inherits:
Object
  • Object
show all
Defined in:
tmpsets/set/mod026-bootstrap/all/bootstrap/table.rb

Instance Method Summary collapse

Constructor Details

#initialize(format, content, opts = {}) ⇒ TableHelper

Returns a new instance of TableHelper.



10
11
12
13
14
15
16
17
18
19
# File 'tmpsets/set/mod026-bootstrap/all/bootstrap/table.rb', line 10

def initialize format, content, opts={}
  @format = format
  @div_table = opts.delete :div_table
  if opts[:header]
    @header = opts[:header].is_a?(Array) ? opts[:header] : content.shift
  end
  @rows = content
  @opts = opts
  @format.add_class opts, :table
end

Instance Method Details

#bodyObject



38
39
40
41
42
43
44
# File 'tmpsets/set/mod026-bootstrap/all/bootstrap/table.rb', line 38

def body
  tag :tbody do
    @rows.map do |row_content|
      row row_content
    end.join "\n"
  end
end

#cell(cell) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
# File 'tmpsets/set/mod026-bootstrap/all/bootstrap/table.rb', line 63

def cell cell
  if cell.is_a? Hash
    content = cell.delete(:content).to_s
    tag :td, cell do
      content
    end
  else
    tag :td do
      String(cell)
    end
  end
end

#headerObject



27
28
29
30
31
32
33
34
35
36
# File 'tmpsets/set/mod026-bootstrap/all/bootstrap/table.rb', line 27

def header
  return unless @header
  tag :thead do
    tag :tr do
      @header.map do |item|
        tag(:th) { item }
      end.join "\n"
    end
  end
end

#renderObject



21
22
23
24
25
# File 'tmpsets/set/mod026-bootstrap/all/bootstrap/table.rb', line 21

def render
  tag :table, class: @opts[:class] do
    [header, body]
  end
end

#row(row) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'tmpsets/set/mod026-bootstrap/all/bootstrap/table.rb', line 46

def row row
  row_data, row_class =
    case row
    when Hash then
      [row.delete(:content), row]
    else
      [row, {}]
    end
  row_content =
    if row_data.is_a?(Array)
      row_data.map { |item| cell item }.join "\n"
    else
      row_data
    end
  tag :tr, row_content, row_class
end

#tag(elem, content_or_opts = {}, opts = {}, &block) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
# File 'tmpsets/set/mod026-bootstrap/all/bootstrap/table.rb', line 76

def tag elem, content_or_opts={}, opts={}, &block
  if @div_table
    if content_or_opts.is_a? Hash
      @format.add_class content_or_opts, elem
    else
      @format.add_class opts, elem
    end
    elem = :div
  end
  @format.wrap_with elem, content_or_opts, opts, &block
end