tablesmith 0.3.1 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: bb370deff33cfe4a11b130d95acc2a873072471e
4
- data.tar.gz: 67757bc15ebb601c7aa74f923ad5e08baf7fc0b3
3
+ metadata.gz: 96ab0a18c776e80aa103908884db53eb98cf7a89
4
+ data.tar.gz: a645ccf65e779cd8b5311b7bd6429d3a48ea752d
5
5
  SHA512:
6
- metadata.gz: 9af99d86d365a7a5f7be65ebf675497f743518dc6960b53d34776d5e7497d8ccf8f42408e454fbbaf8cfc44b032a6fc48f42bd0bb20b67bb1734359d471818df
7
- data.tar.gz: 8e51547aebc4eaee672e78d9bab8961e2d1ea4a00fe279a5bd28fae7b9d334f25bb8197f0f652a1af0d44bf7382f6a1796b3a8da1cb784b1c37064e1a02f6752
6
+ metadata.gz: 8e1d90632da2e260c393346d9a4a230dc747bdf1820ab3a95243efd612ad14f6578ba0ae6062300d1685f3dc811617d5c65eaa0e93caca67c37ad7739570985f
7
+ data.tar.gz: 5162cfc4edc75df6c4019333a87ecd3b867f57f923b9b13cd96b7c6952880cd4202c01cce5a2e145e697eaaec5e3a94c9e31aa5f49ab774dcac8bd64149e781d
@@ -1,5 +1,11 @@
1
+ require:
2
+ - rubocop_lineup
3
+
1
4
  AllCops:
2
5
  TargetRubyVersion: 2.3
3
6
 
4
7
  Layout/IndentHeredoc:
5
- Enabled: true
8
+ Enabled: true
9
+
10
+ Style/Documentation:
11
+ Enabled: false
@@ -3,4 +3,5 @@ require 'tablesmith/array_rows_source'
3
3
  require 'tablesmith/hash_rows_base'
4
4
  require 'tablesmith/hash_rows_source'
5
5
  require 'tablesmith/active_record_source'
6
+ require 'tablesmith/html_formatter'
6
7
  require 'tablesmith/version'
@@ -0,0 +1,63 @@
1
+ # frozen_string_literal: true
2
+
3
+ class HtmlFormatter
4
+ attr_reader :indent
5
+
6
+ def initialize(table)
7
+ @table = table
8
+ @indent = ' '
9
+ end
10
+
11
+ def to_html
12
+ @lines = []
13
+ @lines << '<table>'
14
+ append_table_head
15
+ append_table_body
16
+ @lines << '</table>'
17
+ @lines.join("\n") + "\n"
18
+ end
19
+
20
+ private
21
+
22
+ def append_table_head
23
+ @lines << "#{indent}<thead>"
24
+ @lines << "#{indent}<tr>"
25
+ unless @table.empty?
26
+ rows = @table.text_table.rows[0..0]
27
+ append_rows(rows, 'th')
28
+ end
29
+ @lines << "#{indent}</tr>"
30
+ @lines << "#{indent}</thead>"
31
+ end
32
+
33
+ def append_table_body
34
+ @lines << "#{indent}<tbody>"
35
+ @lines << "#{indent}<tr>"
36
+
37
+ unless @table.empty?
38
+ rows = @table.text_table.rows[2..-1]
39
+ append_rows(rows, 'td')
40
+ end
41
+ @lines << "#{indent}</tr>"
42
+ @lines << "#{indent}</tbody>"
43
+ end
44
+
45
+ def append_rows(rows, tag)
46
+ rows.each do |row|
47
+ next if row == :separator
48
+ row.map do |cell|
49
+ value = cell_value(cell)
50
+ @lines << "#{indent}#{indent}<#{tag}>#{value}</#{tag}>"
51
+ end
52
+ end
53
+ end
54
+
55
+ def cell_value(cell)
56
+ case cell
57
+ when Hash
58
+ cell[:value]
59
+ else
60
+ cell
61
+ end
62
+ end
63
+ end
@@ -12,6 +12,10 @@ module Tablesmith
12
12
  end
13
13
  end
14
14
 
15
+ def respond_to_missing?
16
+ super
17
+ end
18
+
15
19
  # irb
16
20
  def inspect
17
21
  pretty_inspect
@@ -56,6 +60,10 @@ module Tablesmith
56
60
  end
57
61
  end
58
62
 
63
+ def to_html
64
+ HtmlFormatter.new(self).to_html
65
+ end
66
+
59
67
  # override in subclass or mixin
60
68
  def row_values(row)
61
69
  row
@@ -1,3 +1,3 @@
1
1
  module Tablesmith
2
- VERSION = '0.3.1'.freeze
2
+ VERSION = '0.4.0'.freeze
3
3
  end
@@ -52,4 +52,27 @@ describe Table do
52
52
  TABLE
53
53
  actual.to_table.to_csv.should == expected
54
54
  end
55
+
56
+ it 'should output html' do
57
+ actual = [%w[a b c], %w[d e f]]
58
+ expected = <<~TABLE
59
+ <table>
60
+ <thead>
61
+ <tr>
62
+ <th>a</th>
63
+ <th>b</th>
64
+ <th>c</th>
65
+ </tr>
66
+ </thead>
67
+ <tbody>
68
+ <tr>
69
+ <td>d</td>
70
+ <td>e</td>
71
+ <td>f</td>
72
+ </tr>
73
+ </tbody>
74
+ </table>
75
+ TABLE
76
+ actual.to_table.to_html.should == expected
77
+ end
55
78
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tablesmith
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.1
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - chrismo
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-02-21 00:00:00.000000000 Z
11
+ date: 2018-08-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: text-table
@@ -121,7 +121,6 @@ files:
121
121
  - ".ruby-version"
122
122
  - ".travis.yml"
123
123
  - Gemfile
124
- - Gemfile.lock
125
124
  - LICENSE
126
125
  - README.md
127
126
  - Rakefile
@@ -130,6 +129,7 @@ files:
130
129
  - lib/tablesmith/array_rows_source.rb
131
130
  - lib/tablesmith/hash_rows_base.rb
132
131
  - lib/tablesmith/hash_rows_source.rb
132
+ - lib/tablesmith/html_formatter.rb
133
133
  - lib/tablesmith/table.rb
134
134
  - lib/tablesmith/version.rb
135
135
  - spec/active_record_table_spec.rb
@@ -1,66 +0,0 @@
1
- PATH
2
- remote: .
3
- specs:
4
- tablesmith (0.3.1)
5
- text-table
6
-
7
- GEM
8
- remote: https://rubygems.org/
9
- specs:
10
- activemodel (3.2.22.5)
11
- activesupport (= 3.2.22.5)
12
- builder (~> 3.0.0)
13
- activerecord (3.2.22.5)
14
- activemodel (= 3.2.22.5)
15
- activesupport (= 3.2.22.5)
16
- arel (~> 3.0.2)
17
- tzinfo (~> 0.3.29)
18
- activesupport (3.2.22.5)
19
- i18n (~> 0.6, >= 0.6.4)
20
- multi_json (~> 1.0)
21
- arel (3.0.3)
22
- builder (3.0.4)
23
- coderay (1.1.1)
24
- diff-lcs (1.3)
25
- docile (1.1.5)
26
- i18n (0.8.6)
27
- json (2.1.0)
28
- method_source (0.8.2)
29
- multi_json (1.12.1)
30
- pry (0.10.4)
31
- coderay (~> 1.1.0)
32
- method_source (~> 0.8.1)
33
- slop (~> 3.4)
34
- rake (10.5.0)
35
- rspec (2.99.0)
36
- rspec-core (~> 2.99.0)
37
- rspec-expectations (~> 2.99.0)
38
- rspec-mocks (~> 2.99.0)
39
- rspec-core (2.99.2)
40
- rspec-expectations (2.99.2)
41
- diff-lcs (>= 1.1.3, < 2.0)
42
- rspec-mocks (2.99.4)
43
- simplecov (0.14.1)
44
- docile (~> 1.1.0)
45
- json (>= 1.8, < 3)
46
- simplecov-html (~> 0.10.0)
47
- simplecov-html (0.10.1)
48
- slop (3.6.0)
49
- sqlite3 (1.3.13)
50
- text-table (1.2.4)
51
- tzinfo (0.3.53)
52
-
53
- PLATFORMS
54
- ruby
55
-
56
- DEPENDENCIES
57
- activerecord (~> 3.0)
58
- pry
59
- rake (~> 10.0)
60
- rspec (~> 2.0)
61
- simplecov
62
- sqlite3
63
- tablesmith!
64
-
65
- BUNDLED WITH
66
- 1.16.0.pre.2