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

Inherits:
Object
  • Object
show all
Defined in:
card/tmpsets/set/mod021-card-mod-bootstrap/all/bootstrap/table.rb,
card/tmpsets/set/mod022-card-mod-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
# File 'card/tmpsets/set/mod021-card-mod-bootstrap/all/bootstrap/table.rb', line 10

def initialize format, content, opts={}
  @format = format
  @div_table = opts.delete :div_table
  @header = initialize_header opts[:header], content
  @rows = content
  @opts = opts
  @format.add_class opts, :table
end

Instance Method Details

#bodyObject



36
37
38
39
40
41
42
# File 'card/tmpsets/set/mod021-card-mod-bootstrap/all/bootstrap/table.rb', line 36

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

#cell(cell) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
# File 'card/tmpsets/set/mod021-card-mod-bootstrap/all/bootstrap/table.rb', line 61

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



25
26
27
28
29
30
31
32
33
34
# File 'card/tmpsets/set/mod021-card-mod-bootstrap/all/bootstrap/table.rb', line 25

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



19
20
21
22
23
# File 'card/tmpsets/set/mod021-card-mod-bootstrap/all/bootstrap/table.rb', line 19

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

#row(row) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'card/tmpsets/set/mod021-card-mod-bootstrap/all/bootstrap/table.rb', line 44

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, *args, &block) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
# File 'card/tmpsets/set/mod021-card-mod-bootstrap/all/bootstrap/table.rb', line 74

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