table_pal 0.3.1 → 0.3.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 5909bd4d2dccc7ccf6f2b13b71c126bf0065094977e52d842c785c6323960980
4
- data.tar.gz: 49982443771fe482f62ceb31be5240922a9f93e1ffab747f2491a64d50a76356
3
+ metadata.gz: f95417189c9c2c4f52b6f01538b23dafd8831d87f204f30261006cd0d43262c8
4
+ data.tar.gz: 7bd18b009733e0442faf3a3cecb0018257a6eb7b1b08a3d7b8622c914a477529
5
5
  SHA512:
6
- metadata.gz: 66a24580a7be428519549caba7f5a70bee869161210cb1fe8e9e7902589eea1605594da581b95e18343c6e01003fe228273ac8499acc402361121cf62ef5bde9
7
- data.tar.gz: a56a7f1f6963236a50dccad48dccfa899c9e1d2e89c5edecf5159ad37d1552cbfff2f2d2657dabe20a52bce778206ae84f552f316cd43bb98244056cfcb91e3f
6
+ metadata.gz: 6771a4deffb3e214ab14ff93fef20b5fa503f35418874d27e3d7dd8a6367f191dfda064297dad5e7eefd8892ffb1ba81e65a21d3d05d0c4c4855f91dfe9917df
7
+ data.tar.gz: fbec272fc84a48a056d54d64ce46c17fc16f6eaaccaa8af49161bb78ca1a8f5d460d7ef86ace1a5c9131efb6d3c2b95757a410164a1c1f96fc2f6b1f0d350340
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- table_pal (0.3.1)
4
+ table_pal (0.3.2)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
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) if justified
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(justification_to_method, column.width)
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
@@ -15,7 +15,7 @@ module TablePal
15
15
  end
16
16
 
17
17
  def width
18
- table.select_cells(column: self).max_by(&:width).width
18
+ @width ||= table.select_cells(column: self).max_by(&:width).width
19
19
  end
20
20
 
21
21
  def left_padding_char(char)
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 = 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.cells_including_empty.each do |cell|
18
- print_cell(cell)
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
- puts
35
+ def print_heading_row(row)
36
+ print_cells(row)
37
+ add_new_line
38
+ underline
39
+ empty
40
+ end
22
41
 
23
- if row.heading
24
- underline
25
- empty
26
- elsif row.subheading
27
- empty
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
- print cell.column.left_border
33
- print cell.column.left_padding
34
- print cell.formatted(justified: true)
35
- print cell.column.right_padding
36
- print cell.column.right_border
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
- print column.left_border
43
- print column.left_padding_char(char)
44
- print char * column.width
45
- print column.right_padding_char(char)
46
- print column.right_border
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
- puts
83
+ add_new_line
50
84
  end
51
85
 
52
86
  def empty
53
- row = table.create_row
54
-
55
- row.empty_cells.each do |cell|
56
- print cell.column.left_border
57
- print cell.column.left_padding
58
- print cell.formatted(justified: true)
59
- print cell.column.right_padding
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
- puts
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 PlainTextColoured
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: true)
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
@@ -5,7 +5,6 @@ require_relative 'csv_formatted'
5
5
  require_relative 'csv_unformatted'
6
6
  require_relative 'formatters'
7
7
  require_relative 'plain_text'
8
- require_relative 'plain_text_coloured'
9
8
  require_relative 'row'
10
9
  require_relative 'table'
11
10
  require_relative 'table_pal_error'
data/lib/table.rb CHANGED
@@ -1,12 +1,14 @@
1
1
  module TablePal
2
2
  class Table
3
3
 
4
- attr_reader :rows, :columns, :cells
5
-
6
- def initialize
7
- @rows = []
8
- @columns = []
9
- @cells = []
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(options.slice(:row, :column))
41
+ ensure_cell_does_not_exist(row: row, column: column)
36
42
 
37
43
  Cell.new(options).tap do |cell|
38
- @cells << cell
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(row:, column:)
43
- cells.find { |cell| cell.row == row && cell.column == column }
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
- cells.select { |cell| cell.column == column }
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
@@ -1,3 +1,3 @@
1
1
  module TablePal
2
- VERSION = '0.3.1'
2
+ VERSION = '0.3.2'
3
3
  end
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.1
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-04 00:00:00.000000000 Z
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/plain_text_coloured.rb
102
+ - lib/plain_text_not_coloured.rb
103
103
  - lib/requires.rb
104
104
  - lib/row.rb
105
105
  - lib/table.rb