table_pal 0.3.1 → 0.3.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/lib/cell.rb +9 -11
- data/lib/column.rb +1 -1
- data/lib/plain_text.rb +76 -31
- data/lib/{plain_text_coloured.rb → plain_text_not_coloured.rb} +2 -2
- data/lib/requires.rb +0 -1
- data/lib/table.rb +29 -13
- data/lib/version.rb +1 -1
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f95417189c9c2c4f52b6f01538b23dafd8831d87f204f30261006cd0d43262c8
|
4
|
+
data.tar.gz: 7bd18b009733e0442faf3a3cecb0018257a6eb7b1b08a3d7b8622c914a477529
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6771a4deffb3e214ab14ff93fef20b5fa503f35418874d27e3d7dd8a6367f191dfda064297dad5e7eefd8892ffb1ba81e65a21d3d05d0c4c4855f91dfe9917df
|
7
|
+
data.tar.gz: fbec272fc84a48a056d54d64ce46c17fc16f6eaaccaa8af49161bb78ca1a8f5d460d7ef86ace1a5c9131efb6d3c2b95757a410164a1c1f96fc2f6b1f0d350340
|
data/Gemfile.lock
CHANGED
data/lib/cell.rb
CHANGED
@@ -3,6 +3,12 @@ module TablePal
|
|
3
3
|
|
4
4
|
using Colours
|
5
5
|
|
6
|
+
JUSTIFICATIONS = {
|
7
|
+
left: :ljust,
|
8
|
+
right: :rjust,
|
9
|
+
center: :center
|
10
|
+
}.freeze
|
11
|
+
|
6
12
|
attr_reader :row, :column, :content, :formatter, :justification, :colour
|
7
13
|
|
8
14
|
def initialize(row:, column:, content: '', formatter: nil, justification: nil, colour: nil)
|
@@ -16,7 +22,7 @@ module TablePal
|
|
16
22
|
|
17
23
|
def formatted(justified: false, coloured: false)
|
18
24
|
result = format(content)
|
19
|
-
result = justify(result)
|
25
|
+
result = justify(result) if justified
|
20
26
|
result = colourize(result) if coloured
|
21
27
|
|
22
28
|
result
|
@@ -29,7 +35,7 @@ module TablePal
|
|
29
35
|
end
|
30
36
|
|
31
37
|
def justify(result)
|
32
|
-
result.send(
|
38
|
+
result.send(JUSTIFICATIONS[justification], column.width)
|
33
39
|
end
|
34
40
|
|
35
41
|
def colourize(result)
|
@@ -37,15 +43,7 @@ module TablePal
|
|
37
43
|
end
|
38
44
|
|
39
45
|
def width
|
40
|
-
formatted.length
|
41
|
-
end
|
42
|
-
|
43
|
-
def justification_to_method
|
44
|
-
{
|
45
|
-
left: :ljust,
|
46
|
-
right: :rjust,
|
47
|
-
center: :center
|
48
|
-
}[justification]
|
46
|
+
@width ||= formatted.length
|
49
47
|
end
|
50
48
|
|
51
49
|
end
|
data/lib/column.rb
CHANGED
data/lib/plain_text.rb
CHANGED
@@ -1,66 +1,111 @@
|
|
1
1
|
module TablePal
|
2
2
|
class PlainText
|
3
3
|
|
4
|
-
attr_reader :table
|
4
|
+
attr_reader :table, :coloured, :output, :result
|
5
5
|
|
6
|
-
def initialize(table:)
|
7
|
-
@table
|
6
|
+
def initialize(table:, coloured: false, output: true)
|
7
|
+
@table = table
|
8
|
+
@coloured = coloured
|
9
|
+
@output = output
|
10
|
+
@result = ''
|
8
11
|
|
9
12
|
table.rows.each do |row|
|
10
13
|
print_row(row)
|
11
14
|
end
|
12
15
|
end
|
13
16
|
|
17
|
+
def to_s
|
18
|
+
result
|
19
|
+
end
|
20
|
+
|
14
21
|
private
|
15
22
|
|
16
23
|
def print_row(row)
|
17
|
-
row.
|
18
|
-
|
24
|
+
if row.heading
|
25
|
+
print_heading_row(row)
|
26
|
+
elsif row.subheading
|
27
|
+
print_subheading_row(row)
|
28
|
+
elsif row.section_end
|
29
|
+
print_section_end_row(row)
|
30
|
+
else
|
31
|
+
print_regular_row(row)
|
19
32
|
end
|
33
|
+
end
|
20
34
|
|
21
|
-
|
35
|
+
def print_heading_row(row)
|
36
|
+
print_cells(row)
|
37
|
+
add_new_line
|
38
|
+
underline
|
39
|
+
empty
|
40
|
+
end
|
22
41
|
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
42
|
+
def print_subheading_row(row)
|
43
|
+
print_cells(row)
|
44
|
+
add_new_line
|
45
|
+
empty
|
46
|
+
end
|
47
|
+
|
48
|
+
def print_section_end_row(row)
|
49
|
+
print_cells(row)
|
50
|
+
add_new_line
|
51
|
+
empty
|
52
|
+
end
|
53
|
+
|
54
|
+
def print_regular_row(row)
|
55
|
+
print_cells(row)
|
56
|
+
add_new_line
|
57
|
+
end
|
58
|
+
|
59
|
+
def print_cells(row)
|
60
|
+
row.cells_including_empty.each do |cell|
|
61
|
+
print_cell(cell)
|
28
62
|
end
|
29
63
|
end
|
30
64
|
|
31
65
|
def print_cell(cell)
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
66
|
+
add_chars cell.column.left_border
|
67
|
+
add_chars cell.column.left_padding
|
68
|
+
add_chars cell.formatted(justified: true, coloured: coloured)
|
69
|
+
add_chars cell.column.right_padding
|
70
|
+
add_chars cell.column.right_border
|
37
71
|
end
|
38
72
|
|
39
73
|
def underline
|
40
74
|
table.columns.map do |column|
|
41
75
|
char = '-'
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
76
|
+
add_chars column.left_border
|
77
|
+
add_chars column.left_padding_char(char)
|
78
|
+
add_chars char * column.width
|
79
|
+
add_chars column.right_padding_char(char)
|
80
|
+
add_chars column.right_border
|
47
81
|
end
|
48
82
|
|
49
|
-
|
83
|
+
add_new_line
|
50
84
|
end
|
51
85
|
|
52
86
|
def empty
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
print cell.column.right_border
|
87
|
+
table.columns.map do |column|
|
88
|
+
char = ' '
|
89
|
+
add_chars column.left_border
|
90
|
+
add_chars column.left_padding_char(char)
|
91
|
+
add_chars char * column.width
|
92
|
+
add_chars column.right_padding_char(char)
|
93
|
+
add_chars column.right_border
|
61
94
|
end
|
62
95
|
|
63
|
-
|
96
|
+
add_new_line
|
97
|
+
end
|
98
|
+
|
99
|
+
def add_chars(chars)
|
100
|
+
@result.concat(chars)
|
101
|
+
|
102
|
+
print chars if output
|
103
|
+
end
|
104
|
+
|
105
|
+
def add_new_line
|
106
|
+
@result.concat("\n")
|
107
|
+
|
108
|
+
puts if output
|
64
109
|
end
|
65
110
|
|
66
111
|
end
|
@@ -1,5 +1,5 @@
|
|
1
1
|
module TablePal
|
2
|
-
class
|
2
|
+
class PlainText
|
3
3
|
|
4
4
|
attr_reader :table
|
5
5
|
|
@@ -58,7 +58,7 @@ module TablePal
|
|
58
58
|
def print_cell(cell)
|
59
59
|
print cell.column.left_border
|
60
60
|
print cell.column.left_padding
|
61
|
-
print cell.formatted(justified: true, coloured:
|
61
|
+
print cell.formatted(justified: true, coloured: false)
|
62
62
|
print cell.column.right_padding
|
63
63
|
print cell.column.right_border
|
64
64
|
end
|
data/lib/requires.rb
CHANGED
data/lib/table.rb
CHANGED
@@ -1,12 +1,14 @@
|
|
1
1
|
module TablePal
|
2
2
|
class Table
|
3
3
|
|
4
|
-
attr_reader :rows, :columns, :
|
5
|
-
|
6
|
-
def initialize
|
7
|
-
@
|
8
|
-
@
|
9
|
-
@
|
4
|
+
attr_reader :validations, :rows, :columns, :cells_by_row_and_column, :cells_by_column
|
5
|
+
|
6
|
+
def initialize(validations: false)
|
7
|
+
@validations = validations
|
8
|
+
@rows = []
|
9
|
+
@columns = []
|
10
|
+
@cells_by_column = {}
|
11
|
+
@cells_by_row_and_column = {}
|
10
12
|
end
|
11
13
|
|
12
14
|
def create_row(options = {})
|
@@ -22,32 +24,46 @@ module TablePal
|
|
22
24
|
end
|
23
25
|
|
24
26
|
def create_column(options = {})
|
25
|
-
Validate.new(__method__, options)
|
27
|
+
Validate.new(__method__, options) if validations
|
26
28
|
|
27
29
|
Column.new(options.merge(table: self)).tap do |column|
|
30
|
+
cells_by_column[column] = []
|
28
31
|
@columns << column
|
29
32
|
end
|
30
33
|
end
|
31
34
|
|
32
35
|
def create_cell(options = {})
|
33
|
-
Validate.new(__method__, options)
|
36
|
+
Validate.new(__method__, options) if validations
|
37
|
+
|
38
|
+
row = options[:row]
|
39
|
+
column = options[:column]
|
34
40
|
|
35
|
-
ensure_cell_does_not_exist(
|
41
|
+
ensure_cell_does_not_exist(row: row, column: column)
|
36
42
|
|
37
43
|
Cell.new(options).tap do |cell|
|
38
|
-
|
44
|
+
cells_by_column[column] << cell
|
45
|
+
cells_by_row_and_column[key(options)] = cell
|
39
46
|
end
|
40
47
|
end
|
41
48
|
|
42
|
-
def find_cell(
|
43
|
-
|
49
|
+
def find_cell(options)
|
50
|
+
cells_by_row_and_column[key(options)]
|
51
|
+
end
|
52
|
+
|
53
|
+
def key(options)
|
54
|
+
{
|
55
|
+
row: options[:row],
|
56
|
+
column: options[:column]
|
57
|
+
}
|
44
58
|
end
|
45
59
|
|
46
60
|
def select_cells(column:)
|
47
|
-
|
61
|
+
cells_by_column[column]
|
48
62
|
end
|
49
63
|
|
50
64
|
def ensure_cell_does_not_exist(row:, column:)
|
65
|
+
return unless validations
|
66
|
+
|
51
67
|
cell = find_cell(row: row, column: column)
|
52
68
|
|
53
69
|
return unless cell
|
data/lib/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: table_pal
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.3.
|
4
|
+
version: 0.3.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Sean Lerner
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2019-11-
|
11
|
+
date: 2019-11-05 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: interesting_methods
|
@@ -99,7 +99,7 @@ files:
|
|
99
99
|
- lib/csv_unformatted.rb
|
100
100
|
- lib/formatters.rb
|
101
101
|
- lib/plain_text.rb
|
102
|
-
- lib/
|
102
|
+
- lib/plain_text_not_coloured.rb
|
103
103
|
- lib/requires.rb
|
104
104
|
- lib/row.rb
|
105
105
|
- lib/table.rb
|