Module: Collimator::Table

Defined in:
lib/collimator/table.rb

Class Method Summary collapse

Class Method Details

.clear_allObject



156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/collimator/table.rb', line 156

def self.clear_all
  @columns = []
  @rows    = []
  @headers = []
  @footers = []
  @column_names = []
  @use_column_headings = false
  @horiz   = '-'
  @border  = '|'
  @corner  = '+'
  @auto_clear = true
  @separators = []
  @live_update = false
  @table_string = []
  @use_capture_string = false
  @use_capture_html   = false
end

.clear_dataObject



174
175
176
177
# File 'lib/collimator/table.rb', line 174

def self.clear_data
  @rows       = []
  @separators = []
end

.column(heading, opts = {}) ⇒ Object



59
60
61
62
63
64
65
# File 'lib/collimator/table.rb', line 59

def self.column(heading, opts = {})
  width, padding, justification, color = parse_options(opts)

  @columns << { :width => width, :padding => padding, :justification => justification, :color => color }
  @use_column_headings = true if heading.length > 0
  @column_names << heading
end

.complete_html_tableObject



125
126
127
# File 'lib/collimator/table.rb', line 125

def self.complete_html_table
  @table_string << "</table>"
end

.complete_live_updateObject



134
135
136
137
138
139
# File 'lib/collimator/table.rb', line 134

def self.complete_live_update
  put_horizontal_line
  put_footer

  clear_all if @auto_clear
end

.csvObject



141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/collimator/table.rb', line 141

def self.csv
  lines = []

  lines.concat(@headers.map { |h| h[:text] }) if @headers.count > 0
  lines << @column_names.join(',') if @use_column_headings
  @rows.each { |row| lines << row.join(',') }
  lines.concat(@footers.map { |h| h[:text] }) if @headers.count > 0

  out = lines.join("\n")

  clear_all if @auto_clear

  out
end


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

def self.footer(text, opts)
  width, padding, justification = parse_options(opts)

  @footers << { :text => text, :padding => padding, :justification => justification }
end

.header(text, opts = {}) ⇒ Object



47
48
49
50
51
# File 'lib/collimator/table.rb', line 47

def self.header(text, opts = {})
  width, padding, justification, color = parse_options(opts)

  @headers << { :text => text, :padding => padding, :justification => justification , :color => color}
end

.live_updateObject



27
28
29
# File 'lib/collimator/table.rb', line 27

def self.live_update
  @live_update
end

.live_update=(live_upate) ⇒ Object



23
24
25
# File 'lib/collimator/table.rb', line 23

def self.live_update=(live_upate)
  @live_update = live_upate
end

.prep_html_tableObject



120
121
122
123
# File 'lib/collimator/table.rb', line 120

def self.prep_html_table
  @table_string = []
  @table_string << "<table STYLE=\"font-family: helvetica, verdana, tahoma; border-collapse: collapse;\">"
end

.row(row) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/collimator/table.rb', line 67

def self.row(row)
  colored_data_array = []
  if row.class.to_s == "Hash"
    colored_data_array = row[:data].map { |v| {:data => v, :color => row[:color] } }
  else
    colored_data_array = row.clone
  end
  if @live_update
    put_line_of_data(colored_data_array)
  else
    @rows << colored_data_array
  end
end

.separatorObject



81
82
83
84
85
86
87
# File 'lib/collimator/table.rb', line 81

def self.separator
  if @live_update
    put_horizontal_line_with_dividers
  else
    @separators << @rows.length
  end
end

.set_auto_clear(auto_clear = true) ⇒ Object



31
32
33
# File 'lib/collimator/table.rb', line 31

def self.set_auto_clear(auto_clear = true)
  @auto_clear = auto_clear
end

.set_border(border_character) ⇒ Object



39
40
41
# File 'lib/collimator/table.rb', line 39

def self.set_border(border_character)
  @border = border_character
end

.set_corner(corner_character) ⇒ Object



35
36
37
# File 'lib/collimator/table.rb', line 35

def self.set_corner(corner_character)
  @corner = corner_character
end

.set_horizontal(horizontal_character) ⇒ Object



43
44
45
# File 'lib/collimator/table.rb', line 43

def self.set_horizontal(horizontal_character)
  @horiz = horizontal_character
end

.start_live_updateObject



129
130
131
132
# File 'lib/collimator/table.rb', line 129

def self.start_live_update
  put_header
  put_column_heading_text
end

.tabulateObject



89
90
91
92
93
94
95
96
97
98
# File 'lib/collimator/table.rb', line 89

def self.tabulate
  put_header
  put_column_heading_text
  prep_data
  put_table
  put_horizontal_line
  put_footer

  clear_all if @auto_clear
end

.tabulate_to_htmlObject



108
109
110
111
112
113
114
115
116
117
118
# File 'lib/collimator/table.rb', line 108

def self.tabulate_to_html
  @use_capture_string = false
  @use_capture_html = true
  @auto_clear = false

  prep_html_table
  tabulate
  complete_html_table

  @table_string.join("\n")
end

.tabulate_to_stringObject



100
101
102
103
104
105
106
# File 'lib/collimator/table.rb', line 100

def self.tabulate_to_string
  @use_capture_html = false
  @use_capture_string = true
  @auto_clear = false
  tabulate
  @table_string.join("\n")
end