table_pal 0.1.0 → 0.2.0

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: 4b1c3f5234498b916d764d00cc8e9d6a050fd91cac4bc803791b015934494e86
4
- data.tar.gz: 42e49d8eee541a96e21090e467f3abdd87bd96545b0a6dacb12e67cacda061d2
3
+ metadata.gz: 48767bd03cc5c790c74a94af5e6aa8543f67685b3f241d5de639786edc24f09d
4
+ data.tar.gz: 59ccde4f78da37851987b132e86d245b04dd798499615efd3fdb97359e26e05d
5
5
  SHA512:
6
- metadata.gz: 7f7cb22a2a031b3d48bad82329d3d76393d855491b78be17e218b7b235db23b06b0a47c6a54a5725d4d9ea178b761b51039635fa81c605a59d46ce844d8677a0
7
- data.tar.gz: 8dfea9dfe91bc61d63f03ad30aced9bfafbf756c07e0b3aa0027b6eda44123d93e14592c1fee490454f34cf397a7f27cf9646ab6a39f304f3ca223a49eccc3f0
6
+ metadata.gz: 80e74c7849b253a12fe954ed950efd98d3889a7e0d47c4e47b3302c159042f11731ed22378978a9e000461ac32568d1f5943653568bde62d407362be89866811
7
+ data.tar.gz: 97802a7e8f6855f7f1ef3612c018a27a4dae7d628b32b0db51dbb29f52c6775587e946dc4435fd645c05855b9276c083ab925fc1484d1ceda4ad02a67b8a3b81
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- table_pal (0.1.0)
4
+ table_pal (0.2.1)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
data/lib/column.rb CHANGED
@@ -1,10 +1,11 @@
1
1
  module TablePal
2
2
  class Column
3
3
 
4
- attr_reader :table, :left_padding, :right_padding, :left_border, :right_border, :justification, :colour
4
+ attr_reader :table, :left_padding, :right_padding, :left_border, :right_border, :justification, :colour, :formatter
5
5
 
6
- def initialize(table:, left_padding: ' ', right_padding: ' ', left_border: '', right_border: '|', justification: :left, colour: nil)
6
+ def initialize(table:, formatter: nil, left_padding: ' ', right_padding: ' ', left_border: '', right_border: '|', justification: :left, colour: nil)
7
7
  @table = table
8
+ @formatter = formatter
8
9
  @left_padding = left_padding
9
10
  @right_padding = right_padding
10
11
  @left_border = left_border
data/lib/formatters.rb CHANGED
@@ -1,7 +1,11 @@
1
1
  module TablePal
2
- Dollar = -> content { format('$%.2f', content) }
3
- FourFractionDigits = -> content { format('%.4f', content) }
4
- NoFormatting = -> content { content }
5
- Titleize = -> content { content.titleize }
6
- Upper = -> content { content.upcase }
2
+ Dollar = lambda { |content|
3
+ return content unless content.is_a?(Float)
4
+
5
+ format('$%.2f', content)
6
+ }
7
+
8
+ NoFormatting = -> content { content }
9
+ Titleize = -> content { content.titleize }
10
+ Upper = -> content { content.upcase }
7
11
  end
@@ -14,17 +14,14 @@ module TablePal
14
14
  private
15
15
 
16
16
  def print_row(row)
17
- row.cells_including_empty.each do |cell|
18
- print_cell(cell)
19
- end
20
-
17
+ print_cells(row)
21
18
  puts
19
+ underline if row.heading
20
+ end
22
21
 
23
- if row.heading
24
- underline
25
- empty
26
- elsif row.subheading
27
- empty
22
+ def print_cells(row)
23
+ row.cells_including_empty.each do |cell|
24
+ print_cell(cell)
28
25
  end
29
26
  end
30
27
 
@@ -49,13 +46,5 @@ module TablePal
49
46
  puts
50
47
  end
51
48
 
52
- def empty
53
- table.create_row.empty_cells.each do |cell|
54
- print_cell(cell)
55
- end
56
-
57
- puts
58
- end
59
-
60
49
  end
61
50
  end
data/lib/requires.rb CHANGED
@@ -1,5 +1,4 @@
1
1
  require_relative 'colours'
2
- require_relative 'table_pal'
3
2
  require_relative 'cell'
4
3
  require_relative 'column'
5
4
  require_relative 'csv_formatted'
@@ -9,3 +8,5 @@ require_relative 'plain_text'
9
8
  require_relative 'plain_text_coloured'
10
9
  require_relative 'row'
11
10
  require_relative 'table'
11
+ require_relative 'table_pal_error'
12
+ require_relative 'validate'
data/lib/table.rb CHANGED
@@ -10,6 +10,8 @@ module TablePal
10
10
  end
11
11
 
12
12
  def create_row(options = {})
13
+ Validate.new(__method__, options)
14
+
13
15
  Row.new(options.merge(table: self)).tap do |row|
14
16
  @rows << row
15
17
  end
@@ -20,12 +22,18 @@ module TablePal
20
22
  end
21
23
 
22
24
  def create_column(options = {})
25
+ Validate.new(__method__, options)
26
+
23
27
  Column.new(options.merge(table: self)).tap do |column|
24
28
  @columns << column
25
29
  end
26
30
  end
27
31
 
28
32
  def create_cell(options = {})
33
+ Validate.new(__method__, options)
34
+
35
+ ensure_cell_does_not_exist(options.slice(:row, :column))
36
+
29
37
  Cell.new(options).tap do |cell|
30
38
  @cells << cell
31
39
  end
@@ -39,5 +47,13 @@ module TablePal
39
47
  cells.select { |cell| cell.column == column }
40
48
  end
41
49
 
50
+ def ensure_cell_does_not_exist(row:, column:)
51
+ cell = find_cell(row: row, column: column)
52
+
53
+ return unless cell
54
+
55
+ raise TablePalError, "Cell with content: '#{cell.content}' already exists at this row and column."
56
+ end
57
+
42
58
  end
43
59
  end
data/lib/table_pal.rb CHANGED
@@ -1,2 +1,4 @@
1
1
  module TablePal
2
2
  end
3
+
4
+ require_relative 'requires'
@@ -0,0 +1,25 @@
1
+ module TablePal
2
+ class TablePalError < StandardError
3
+
4
+ # def initialize(message)
5
+ # @meth = meth
6
+
7
+ # options.each do |key, value|
8
+ # validate_option(key, value)
9
+ # end
10
+ # end
11
+
12
+ # def validate_option(key, value)
13
+ # case key
14
+ # when :row then validate_row(value)
15
+ # end
16
+ # end
17
+
18
+ # def validate_row(value)
19
+ # return if value.is_a?(Row)
20
+
21
+ # raise "Table.#{meth} error. Expected `row:` to be a Row instance, not a #{value.class}"
22
+ # end
23
+
24
+ end
25
+ end
data/lib/validate.rb ADDED
@@ -0,0 +1,49 @@
1
+ module TablePal
2
+ class Validate
3
+
4
+ attr_reader :meth
5
+
6
+ def initialize(meth, options)
7
+ @meth = meth
8
+
9
+ options.each do |key, value|
10
+ validate_option(key, value)
11
+ end
12
+ end
13
+
14
+ def validate_option(key, value)
15
+ case key
16
+ when :colour then validate(key: key, value: value, classes: [Symbol, NilClass])
17
+ when :column then validate(key: key, value: value, classes: Column)
18
+ when :content then validate(key: key, value: value, classes: [String, Float, Integer, NilClass])
19
+ when :formatter then validate(key: key, value: value, classes: Proc)
20
+ when :heading then validate(key: key, value: value, classes: [TrueClass])
21
+ when :justification then validate(key: key, value: value, classes: Symbol)
22
+ when :left_border then validate(key: key, value: value, classes: [String, NilClass])
23
+ when :left_padding then validate(key: key, value: value, classes: String)
24
+ when :right_border then validate(key: key, value: value, classes: [String, NilClass])
25
+ when :right_padding then validate(key: key, value: value, classes: String)
26
+ when :row then validate(key: key, value: value, classes: Row)
27
+ when :subheading then validate(key: key, value: value, classes: [TrueClass])
28
+ else raise TablePalError, "#{class_method} received Unexpected option: `#{key}`"
29
+ end
30
+ end
31
+
32
+ def validate(key:, value:, classes:)
33
+ classes = [classes].flatten
34
+
35
+ return if classes.any? { |klass| value.is_a?(klass) }
36
+
37
+ raise TablePalError, "#{class_method} expected `#{key}:` to be a #{for_sentence(classes)}, not a `#{value.class}`"
38
+ end
39
+
40
+ def class_method
41
+ "Table.#{meth}"
42
+ end
43
+
44
+ def for_sentence(classes)
45
+ classes.map { |klass| "`#{klass}`" }.join(' or ')
46
+ end
47
+
48
+ end
49
+ end
data/lib/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module TablePal
2
- VERSION = '0.1.0'
2
+ VERSION = '0.2.0'
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.1.0
4
+ version: 0.2.0
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-10-27 00:00:00.000000000 Z
11
+ date: 2019-11-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: interesting_methods
@@ -104,6 +104,8 @@ files:
104
104
  - lib/row.rb
105
105
  - lib/table.rb
106
106
  - lib/table_pal.rb
107
+ - lib/table_pal_error.rb
108
+ - lib/validate.rb
107
109
  - lib/version.rb
108
110
  - table_pal.gemspec
109
111
  homepage: https://github.com/seanlerner/table_pal