Class: INat::Report::Table

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

Defined Under Namespace

Modules: DSL

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeTable

Returns a new instance of Table.



7
8
9
10
11
# File 'lib/inat/report/table.rb', line 7

def initialize
  @columns = []
  @rows = []
  @line_no = 0
end

Instance Attribute Details

#columnsObject (readonly)

Returns the value of attribute columns.



5
6
7
# File 'lib/inat/report/table.rb', line 5

def columns
  @columns
end

Instance Method Details

#<<(data) ⇒ Object



48
49
50
51
52
53
54
55
56
# File 'lib/inat/report/table.rb', line 48

def << data
  if Array === data
    rows(*data)
  elsif Hash === data
    row(**data)
  else
    raise TypeError, "Invalid data type: #{ data.inspect }!", caller
  end
end

#column(title, width: nil, align: nil, data: nil, marker: false, &block) ⇒ Object (private)



13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/inat/report/table.rb', line 13

def column title, width: nil, align: nil, data: nil, marker: false, &block
  if data == nil && !block_given?
    raise ArgumentError, "Data argument or block must be provided!", caller
  end
  @columns << {
    title:  title,
    width:  width,
    align:  align,
    data:   data,
    marker: marker,
    block:  block
  }
end

#empty?Boolean

Returns:



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

def empty?
  @rows.empty?
end

#headerObject (private)



74
75
76
77
78
79
80
# File 'lib/inat/report/table.rb', line 74

private def header
  result = []
  result << "<tr>"
  result += @columns.map { |c| th(c) }
  result << "</tr>"
  result.join ""
end

#row(**data) ⇒ Object



29
30
31
32
33
34
35
# File 'lib/inat/report/table.rb', line 29

def row **data
  if !data.has_key?(:line_no)
    @line_no += 1
    data[:line_no] ||= @line_no
  end
  @rows << data
end

#row_to_html(row) ⇒ Object (private)



112
113
114
115
116
117
118
# File 'lib/inat/report/table.rb', line 112

private def row_to_html row
  result = []
  result << "<tr#{ row[:style] ? " style=\"#{ row[:style] }\"" : '' }>"
  result += @columns.map { |c| td(c, row) }
  result << "</td>"
  result.join "\n"
end

#rows(*data) ⇒ Object



37
38
39
40
41
42
# File 'lib/inat/report/table.rb', line 37

def rows *data
  data.each do |r|
    row(**r)
  end
  @rows
end

#td(column, row) ⇒ Object (private)



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/inat/report/table.rb', line 82

private def td column, row
  inner = case column[:data]
  when String, Symbol
    row[column[:data].intern]
  when Proc
    column[:data].call row
  when nil
    column[:block].call row
  end
  inner = inner.to_html if inner.respond_to?(:to_html)
  style = ""
  case column[:width]
  when Numeric
    style += "width:#{ column[:width] }em;"
  when String
    style += "width:#{ column[:width] };"
  end
  style += "text-align:#{ column[:align] };" if column[:align]
  marker = nil
  if column[:marker]
    anchor = row[:anchor]
    marker = "<a name=\"#{ anchor }\"></a>"
  end
  if style.empty?
    "<td>#{ marker }#{ inner }</td>"
  else
    "<td style=\"#{ style }\">#{ marker }#{ inner }</td>"
  end
end

#th(column) ⇒ Object (private)



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/inat/report/table.rb', line 58

private def th column
  style = ""
  case column[:width]
  when Numeric
    style += "width:#{ column[:width] }em;"
  when String
    style += "width:#{ column[:width] };"
  end
  style += "text-align:#{ column[:align] };" if column[:align]
  if style.empty?
    "<th>#{ column[:title] }</th>"
  else
    "<th style=\"#{ style }\">#{ column[:title] }</th>"
  end
end

#to_htmlObject



120
121
122
123
124
125
126
127
# File 'lib/inat/report/table.rb', line 120

def to_html
  result = []
  result << "<table>"
  result << header
  result += @rows.map { |r| row_to_html(r) }
  result << "</table>"
  result.join "\n"
end