Class: AsciiParadise::AsciiTable
- Inherits:
-
Object
- Object
- AsciiParadise::AsciiTable
show all
- Defined in:
- lib/ascii_paradise/asciitable/toplevel_methods.rb,
lib/ascii_paradise/asciitable/row.rb,
lib/ascii_paradise/asciitable/cell.rb,
lib/ascii_paradise/asciitable/style.rb,
lib/ascii_paradise/asciitable/table.rb,
lib/ascii_paradise/asciitable/separator.rb,
lib/ascii_paradise/asciitable/table_helper.rb
Overview
AsciiParadise::AsciiTable
Defined Under Namespace
Modules: TableHelper
Classes: Cell, Row, Separator, Style
Class Method Summary
collapse
-
.disable_colours ⇒ Object
# === AsciiParadise::AsciiTable.disable_colours ========================================================================= #.
-
.enable_colours ⇒ Object
# === AsciiParadise::AsciiTable.enable_colours ========================================================================= #.
-
.use_colours? ⇒ Boolean
# === AsciiParadise::AsciiTable.use_colours? ========================================================================= #.
-
.use_this_colour(i) ⇒ Object
# === AsciiParadise::AsciiTable.use_this_colour ========================================================================= #.
-
.use_which_colours? ⇒ Boolean
# === AsciiParadise::AsciiTable.use_which_colours?.
Instance Method Summary
collapse
Constructor Details
#initialize(i = {}, &block) ⇒ AsciiTable
#
initialize
Generates a ASCII table with the given options.
#
14
15
16
17
18
19
20
21
22
23
|
# File 'lib/ascii_paradise/asciitable/table.rb', line 14
def initialize(
i = {}, &block
)
reset
self.style = i.fetch :style, {}
self.headings = i.fetch :headings, []
self.rows = i.fetch :rows, []
self.title = i.fetch :title, nil
yield_or_eval(&block) if block
end
|
Class Method Details
.disable_colours ⇒ Object
#
AsciiParadise::AsciiTable.disable_colours
#
28
29
30
|
# File 'lib/ascii_paradise/asciitable/toplevel_methods.rb', line 28
def self.disable_colours
@use_colours = false
end
|
.enable_colours ⇒ Object
#
AsciiParadise::AsciiTable.enable_colours
#
35
36
37
|
# File 'lib/ascii_paradise/asciitable/toplevel_methods.rb', line 35
def self.enable_colours
@use_colours = true
end
|
.use_colours? ⇒ Boolean
#
AsciiParadise::AsciiTable.use_colours?
#
42
43
44
|
# File 'lib/ascii_paradise/asciitable/toplevel_methods.rb', line 42
def self.use_colours?
@use_colours
end
|
.use_this_colour(i) ⇒ Object
#
AsciiParadise::AsciiTable.use_this_colour
#
58
59
60
|
# File 'lib/ascii_paradise/asciitable/toplevel_methods.rb', line 58
def self.use_this_colour(i)
@use_this_colour = i
end
|
.use_which_colours? ⇒ Boolean
#
AsciiParadise::AsciiTable.use_which_colours?
Currently hardcoded. Determines which colour to use.
#
51
52
53
|
# File 'lib/ascii_paradise/asciitable/toplevel_methods.rb', line 51
def self.use_which_colours?
@use_this_colour
end
|
Instance Method Details
#==(other) ⇒ Object
#
==
Check if other is equal to self. other is considered equal if it contains the same headings and rows.
#
253
254
255
256
257
|
# File 'lib/ascii_paradise/asciitable/table.rb', line 253
def ==(other)
if other.respond_to? :render and other.respond_to? :rows
self.headings == other.headings and self.rows == other.rows
end
end
|
#add(i = :separator) ⇒ Object
103
104
105
106
107
108
109
110
|
# File 'lib/ascii_paradise/asciitable/table.rb', line 103
def add(i = :separator)
case i
when :separator
add_separator
else add_row(i)
end
end
|
#add_row(array) ⇒ Object
Also known as:
<<, append
#
add_row (row tag, append tag)
Add a row. We add either a separator, or a new row.
You can also use the << operator for this method.
#
89
90
91
92
93
94
95
96
97
|
# File 'lib/ascii_paradise/asciitable/table.rb', line 89
def add_row(array)
if array == :separator
row = Separator.new(self)
else
row = Row.new(self, array)
end
@rows << row
recalc_column_widths(row)
end
|
#add_separator ⇒ Object
#
add_separator
Add a separator. A separator has —- and +.
#
117
118
119
|
# File 'lib/ascii_paradise/asciitable/table.rb', line 117
def add_separator
add_row(:separator)
end
|
#align_column(n, alignment) ⇒ Object
#
align_column
Align column n to the given alignment of :center, :left, or :right.
#
69
70
71
72
73
74
75
76
77
78
79
80
|
# File 'lib/ascii_paradise/asciitable/table.rb', line 69
def align_column(n, alignment)
case alignment
when :middle
alignment = :center
end
r = rows
column(n).each_with_index { |col, i|
cell = r[i][n]
next unless cell
cell.alignment = alignment unless cell.alignment?
}
end
|
#cell_padding ⇒ Object
#
cell_padding
Return the total padding used here.
#
133
134
135
|
# File 'lib/ascii_paradise/asciitable/table.rb', line 133
def cell_padding
style.padding_left + style.padding_right
end
|
#cell_spacing ⇒ Object
124
125
126
|
# File 'lib/ascii_paradise/asciitable/table.rb', line 124
def cell_spacing
cell_padding + style.border_y.length
end
|
#column(n, method = :value, array = rows) ⇒ Object
#
column
Return column n.
#
142
143
144
145
146
147
|
# File 'lib/ascii_paradise/asciitable/table.rb', line 142
def column(n, method = :value, array = rows)
array.map { |row|
cell = row[n]
cell && method ? cell.__send__(method) : cell
}.compact
end
|
#column_width(n) ⇒ Object
Also known as:
length_of_column
#
column_width
Return length of column n.
#
172
173
174
175
|
# File 'lib/ascii_paradise/asciitable/table.rb', line 172
def column_width(n)
width = @column_widths[n] || 0
width + additional_column_widths[n].to_i
end
|
#column_with_headings(n, method = :value) ⇒ Object
#
column_with_headings
Return n column including headings.
#
154
155
156
|
# File 'lib/ascii_paradise/asciitable/table.rb', line 154
def column_with_headings(n, method = :value)
column(n, method, headings_with_rows)
end
|
#columns ⇒ Object
#
columns
Return columns.
#
163
164
165
|
# File 'lib/ascii_paradise/asciitable/table.rb', line 163
def columns
(0...number_of_columns).map { |n| column n }
end
|
#headings=(i) ⇒ Object
#
headings=
Set the headings
#
191
192
193
194
|
# File 'lib/ascii_paradise/asciitable/table.rb', line 191
def headings=(i)
@headings = Row.new(self, i)
recalc_column_widths @headings
end
|
#headings? ⇒ Boolean
Also known as:
headings
#
headings?
Reader method for @headings.
#
40
41
42
|
# File 'lib/ascii_paradise/asciitable/table.rb', line 40
def headings?
@headings
end
|
#number_of_columns ⇒ Object
#
number_of_columns
Return total number of columns available.
#
182
183
184
|
# File 'lib/ascii_paradise/asciitable/table.rb', line 182
def number_of_columns
headings_with_rows.map { |r| r.cells.size }.max
end
|
#render ⇒ Object
Also known as:
to_s
#
render
Render the table.
#
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
|
# File 'lib/ascii_paradise/asciitable/table.rb', line 201
def render
separator = Separator.new(self)
buffer = [separator]
unless @title.nil?
buffer << Row.new(self, [title_cell_options])
buffer << separator
end
unless @headings.cells.empty?
buffer << @headings
buffer << separator
end
buffer += @rows
buffer << separator
buffer.map { |r| r.render }.join("\n")
end
|
#reset ⇒ Object
28
29
30
31
32
33
|
# File 'lib/ascii_paradise/asciitable/table.rb', line 28
def reset
@column_widths = []
@result = nil
end
|
#rows ⇒ Object
#
rows
Return rows without separator rows.
#
222
223
224
|
# File 'lib/ascii_paradise/asciitable/table.rb', line 222
def rows
@rows.reject { |row| row.is_a? Separator }
end
|
#rows=(i) ⇒ Object
#
rows
Input to this should be an array.
#
231
232
233
234
|
# File 'lib/ascii_paradise/asciitable/table.rb', line 231
def rows=(i)
@rows = []
i.each { |entry| self << entry }
end
|
#style ⇒ Object
59
60
61
|
# File 'lib/ascii_paradise/asciitable/table.rb', line 59
def style
@style ||= Style.new end
|
#style=(i) ⇒ Object
#
style= (style tag)
Set the style setter. We do this by calling apply() on the style object.
Specific example how to use this:
t.style = {:padding_left => 2, :width => 80}
#
52
53
54
|
# File 'lib/ascii_paradise/asciitable/table.rb', line 52
def style=(i)
style.apply(i) end
|
#title=(i) ⇒ Object
Also known as:
, set_title
#
title= (title tag)
Set the title here.
#
241
242
243
244
|
# File 'lib/ascii_paradise/asciitable/table.rb', line 241
def title=(i)
@title = i
recalc_column_widths( Row.new(self, [title_cell_options]) )
end
|
#yield_or_eval(&block) ⇒ Object
Also known as:
display, run
#
yield_or_eval (run tag)
#
262
263
264
265
266
267
268
269
270
|
# File 'lib/ascii_paradise/asciitable/table.rb', line 262
def yield_or_eval(&block)
return unless block
if block.arity > 0
yield self
else
@result = self.instance_eval(&block)
end
return @result
end
|