html_table_dsl 0.0.1

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: e6d042b81a908f47f4a06115492bb0b3d59af150e00eaed401210f9735b6beb7
4
+ data.tar.gz: 9f1ec8604e62a81ec9870044b367436036a163e50c87ab70fe0bcfed1548b834
5
+ SHA512:
6
+ metadata.gz: b278ae4bfe43fbb99e422a15007e06b0801d39e52a59232b7c177a7a4e0a7e36a80055d27bbe73f868e0275e74df67fce07a1f1fd6d95107272a73e00a9a5dd9
7
+ data.tar.gz: 4689a55bcf08b252c4c3916e035ca2662abd0e01aad25589e638c4eca87b84c0f2c688b0b7284c5082df7eec3f5b87a36ee641d2ba342f7fd8dcc352807be5ac
data/.gitignore ADDED
@@ -0,0 +1,56 @@
1
+ *.gem
2
+ *.rbc
3
+ /.config
4
+ /coverage/
5
+ /InstalledFiles
6
+ /pkg/
7
+ /spec/reports/
8
+ /spec/examples.txt
9
+ /test/tmp/
10
+ /test/version_tmp/
11
+ /tmp/
12
+
13
+ # Used by dotenv library to load environment variables.
14
+ # .env
15
+
16
+ # Ignore Byebug command history file.
17
+ .byebug_history
18
+
19
+ ## Specific to RubyMotion:
20
+ .dat*
21
+ .repl_history
22
+ build/
23
+ *.bridgesupport
24
+ build-iPhoneOS/
25
+ build-iPhoneSimulator/
26
+
27
+ ## Specific to RubyMotion (use of CocoaPods):
28
+ #
29
+ # We recommend against adding the Pods directory to your .gitignore. However
30
+ # you should judge for yourself, the pros and cons are mentioned at:
31
+ # https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control
32
+ #
33
+ # vendor/Pods/
34
+
35
+ ## Documentation cache and generated files:
36
+ /.yardoc/
37
+ /_yardoc/
38
+ /doc/
39
+ /rdoc/
40
+
41
+ ## Environment normalization:
42
+ /.bundle/
43
+ /vendor/bundle
44
+ /lib/bundler/man/
45
+
46
+ # for a library or gem, you might want to ignore these files since the code is
47
+ # intended to run in multiple environments; otherwise, check them in:
48
+ # Gemfile.lock
49
+ # .ruby-version
50
+ # .ruby-gemset
51
+
52
+ # unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
53
+ .rvmrc
54
+
55
+ # Used by RuboCop. Remote config files pulled in from inherit_from directive.
56
+ # .rubocop-https?--*
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,16 @@
1
+ require: rubocop-rspec
2
+
3
+ Metrics/LineLength:
4
+ Max: 120
5
+
6
+ Metrics/BlockLength:
7
+ Max: 35
8
+
9
+ Style/Documentation:
10
+ Enabled: false
11
+
12
+ Style/FrozenStringLiteralComment:
13
+ EnforcedStyle: never
14
+
15
+ RSpec/MultipleExpectations:
16
+ Enabled: false
data/Gemfile ADDED
@@ -0,0 +1,5 @@
1
+ source 'https://rubygems.org'
2
+ git_source(:github) { |repo| "https://github.com/#{repo}.git" }
3
+ git_source(:github) { 'https://github.com/GorohovAlex/html_table_dsl' }
4
+
5
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,92 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ html_table_dsl (0.0.1)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ ast (2.4.2)
10
+ base64 (0.1.1)
11
+ coderay (1.1.3)
12
+ colorize (0.8.1)
13
+ diff-lcs (1.5.0)
14
+ docile (1.4.0)
15
+ fasterer (0.10.1)
16
+ colorize (~> 0.7)
17
+ ruby_parser (>= 3.19.1)
18
+ json (2.6.3)
19
+ language_server-protocol (3.17.0.3)
20
+ method_source (1.0.0)
21
+ parallel (1.23.0)
22
+ parser (3.2.2.3)
23
+ ast (~> 2.4.1)
24
+ racc
25
+ pry (0.14.2)
26
+ coderay (~> 1.1)
27
+ method_source (~> 1.0)
28
+ racc (1.7.1)
29
+ rainbow (3.1.1)
30
+ regexp_parser (2.8.1)
31
+ rexml (3.2.6)
32
+ rspec (3.12.0)
33
+ rspec-core (~> 3.12.0)
34
+ rspec-expectations (~> 3.12.0)
35
+ rspec-mocks (~> 3.12.0)
36
+ rspec-core (3.12.2)
37
+ rspec-support (~> 3.12.0)
38
+ rspec-expectations (3.12.3)
39
+ diff-lcs (>= 1.2.0, < 2.0)
40
+ rspec-support (~> 3.12.0)
41
+ rspec-mocks (3.12.6)
42
+ diff-lcs (>= 1.2.0, < 2.0)
43
+ rspec-support (~> 3.12.0)
44
+ rspec-support (3.12.1)
45
+ rubocop (1.56.2)
46
+ base64 (~> 0.1.1)
47
+ json (~> 2.3)
48
+ language_server-protocol (>= 3.17.0)
49
+ parallel (~> 1.10)
50
+ parser (>= 3.2.2.3)
51
+ rainbow (>= 2.2.2, < 4.0)
52
+ regexp_parser (>= 1.8, < 3.0)
53
+ rexml (>= 3.2.5, < 4.0)
54
+ rubocop-ast (>= 1.28.1, < 2.0)
55
+ ruby-progressbar (~> 1.7)
56
+ unicode-display_width (>= 2.4.0, < 3.0)
57
+ rubocop-ast (1.29.0)
58
+ parser (>= 3.2.1.0)
59
+ rubocop-capybara (2.18.0)
60
+ rubocop (~> 1.41)
61
+ rubocop-factory_bot (2.23.1)
62
+ rubocop (~> 1.33)
63
+ rubocop-rspec (2.23.2)
64
+ rubocop (~> 1.33)
65
+ rubocop-capybara (~> 2.17)
66
+ rubocop-factory_bot (~> 2.22)
67
+ ruby-progressbar (1.13.0)
68
+ ruby_parser (3.20.3)
69
+ sexp_processor (~> 4.16)
70
+ sexp_processor (4.17.0)
71
+ simplecov (0.22.0)
72
+ docile (~> 1.1)
73
+ simplecov-html (~> 0.11)
74
+ simplecov_json_formatter (~> 0.1)
75
+ simplecov-html (0.12.3)
76
+ simplecov_json_formatter (0.1.4)
77
+ unicode-display_width (2.4.2)
78
+
79
+ PLATFORMS
80
+ arm64-darwin-22
81
+
82
+ DEPENDENCIES
83
+ bundler (~> 2.0)
84
+ fasterer (~> 0.10.1)
85
+ html_table_dsl!
86
+ pry (~> 0.14.2)
87
+ rspec (~> 3.12)
88
+ rubocop-rspec (~> 2.23.2)
89
+ simplecov (~> 0.22.0)
90
+
91
+ BUNDLED WITH
92
+ 2.3.14
data/README.md ADDED
@@ -0,0 +1,26 @@
1
+ # HTML-table DSL
2
+ DLS for working with HTML tables. Easy creation and population of tables in Ruby.
3
+
4
+ ## Create table
5
+ ```ruby
6
+ html_table = Html.table(1, 2, name: 'table_name', class: 'table_class')
7
+ # <table name='table_name' class='table_class'><tbody><tr><td></td><td></td></tr></tbody></table>
8
+ ```
9
+ ## Change attributes
10
+ ```ruby
11
+ html_table.attributes = { class: 'table_class_new', name: nil, style: 'border: 1px' }
12
+ # <table class='table_class_new' style='border: 1px'> ... </table>
13
+ ```
14
+ ## Set headers
15
+ ```ruby
16
+ head_cols = [Html::HeadCol.new('Text')]
17
+ head_row = Html::Row.new(head_cols, name: 'row_name', class: 'row_class')
18
+ html_table.write_header(head_row)
19
+ # <table><thead><tr name='row_name' class='row_class'><th>Text</th></tr></thead><tbody> ... </tbody></table>
20
+ ```
21
+ ## Set rows
22
+ ```ruby
23
+ cols = [Html::Col.new('Text', name: 'col_name', style: 'col_style')]
24
+ html_table.write_row(0, Html::Row.new(cols, name: 'row_name', class: 'row_class'))
25
+ # <table><tbody><tr name='row_name' class='row_class'><td name='col_name' style='col_style'>Text</td></tr></tbody></table>
26
+ ```
data/lib/html/col.rb ADDED
@@ -0,0 +1,22 @@
1
+ require 'cgi'
2
+ require_relative 'tag'
3
+
4
+ module Html
5
+ class Col < Tag
6
+ def initialize(value = '', **attrs)
7
+ super(**attrs)
8
+
9
+ @value = value
10
+ end
11
+
12
+ private
13
+
14
+ def tag_name
15
+ :td
16
+ end
17
+
18
+ def value
19
+ CGI.escapeHTML(@value.to_s)
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,11 @@
1
+ require_relative 'col'
2
+
3
+ module Html
4
+ class HeadCol < Col
5
+ private
6
+
7
+ def tag_name
8
+ :th
9
+ end
10
+ end
11
+ end
data/lib/html/row.rb ADDED
@@ -0,0 +1,24 @@
1
+ require_relative 'col'
2
+ require_relative 'tag'
3
+
4
+ module Html
5
+ class Row < Tag
6
+ attr_reader :cols
7
+
8
+ def initialize(cols = Array.new(0, Col.new), **attrs)
9
+ super(**attrs)
10
+
11
+ @cols = cols
12
+ end
13
+
14
+ private
15
+
16
+ def tag_name
17
+ :tr
18
+ end
19
+
20
+ def value
21
+ @cols&.map(&:to_s)&.join
22
+ end
23
+ end
24
+ end
data/lib/html/table.rb ADDED
@@ -0,0 +1,47 @@
1
+ require_relative 'tag'
2
+ require_relative 'row'
3
+ require_relative 'thead'
4
+ require_relative 'tbody'
5
+
6
+ module Html
7
+ def self.table(rows_number, cols_number, **attrs)
8
+ Table.new(rows_number, cols_number, **attrs)
9
+ end
10
+
11
+ class Table < Tag
12
+ def initialize(rows = 0, cols = 0, **attrs)
13
+ super(**attrs)
14
+
15
+ @head_rows = Row.new(Array.new(0))
16
+ @body_rows = Array.new(rows, Row.new(Array.new(cols, Col.new)))
17
+ end
18
+
19
+ def write_row(index, row)
20
+ @body_rows[index] = row
21
+ end
22
+
23
+ def write_header(head_rows)
24
+ @head_rows = head_rows
25
+ end
26
+
27
+ private
28
+
29
+ def tag_name
30
+ :table
31
+ end
32
+
33
+ def value
34
+ output_value = ''
35
+ output_value += head.to_s unless head.empty?
36
+ output_value + body.to_s unless body.empty?
37
+ end
38
+
39
+ def head
40
+ @head ||= Thead.new(@head_rows)
41
+ end
42
+
43
+ def body
44
+ @body ||= Tbody.new(@body_rows)
45
+ end
46
+ end
47
+ end
data/lib/html/tag.rb ADDED
@@ -0,0 +1,35 @@
1
+ require 'cgi'
2
+
3
+ module Html
4
+ class Tag
5
+ def initialize(**attrs)
6
+ @attributes = attrs
7
+ end
8
+
9
+ def to_s
10
+ "<#{tag_name}#{read_attributes}>#{value}</#{tag_name}>"
11
+ end
12
+
13
+ def attributes=(new_attributes)
14
+ @attributes.merge!(new_attributes).compact!
15
+ end
16
+
17
+ private
18
+
19
+ def tag_name
20
+ raise NotImplementedError
21
+ end
22
+
23
+ def value
24
+ raise NotImplementedError
25
+ end
26
+
27
+ def read_attributes
28
+ attributes.empty? ? nil : " #{attributes}"
29
+ end
30
+
31
+ def attributes
32
+ @attributes.map { |key, value| "#{CGI.escapeHTML(key.to_s)}='#{CGI.escapeHTML(value)}'" }.join(' ')
33
+ end
34
+ end
35
+ end
data/lib/html/tbody.rb ADDED
@@ -0,0 +1,24 @@
1
+ require_relative 'tag'
2
+ module Html
3
+ class Tbody < Tag
4
+ def initialize(rows = Row.new(Array.new(0)), **attrs)
5
+ super(**attrs)
6
+
7
+ @rows = rows
8
+ end
9
+
10
+ def empty?
11
+ @rows.nil? || @rows.empty?
12
+ end
13
+
14
+ private
15
+
16
+ def tag_name
17
+ :tbody
18
+ end
19
+
20
+ def value
21
+ @rows&.map(&:to_s)&.join
22
+ end
23
+ end
24
+ end
data/lib/html/thead.rb ADDED
@@ -0,0 +1,25 @@
1
+ require_relative 'tag'
2
+
3
+ module Html
4
+ class Thead < Tag
5
+ def initialize(row = Row.new(Array.new(0)), **attrs)
6
+ super(**attrs)
7
+
8
+ @row = row
9
+ end
10
+
11
+ def empty?
12
+ @row.nil? || @row.cols.empty?
13
+ end
14
+
15
+ private
16
+
17
+ def tag_name
18
+ :thead
19
+ end
20
+
21
+ def value
22
+ @row
23
+ end
24
+ end
25
+ end
data/main.rb ADDED
@@ -0,0 +1,22 @@
1
+ require_relative './lib/html/col'
2
+ require_relative './lib/html/head_col'
3
+ require_relative './lib/html/row'
4
+ require_relative './lib/html/table'
5
+
6
+ html_table = Html.table(1, 2) #, name: 'table_name', class: 'table_class')
7
+ # html_table.attributes = { class: 'table_class_new', name: nil, style: 'border: 1px' }
8
+
9
+ # head_cols = [Html::HeadCol.new('Text')]
10
+ # head_row = Html::Row.new(head_cols, name: 'row_name', class: 'row_class')
11
+ # html_table.write_header(head_row)
12
+ # cols = Array.new(3) { |index| Html::Col.new("COL #{index} VALUE", style: 'border: 1px solid black') }
13
+ # first_row = Html::Row.new(cols, name: 'row_name', class: 'row_class', style: 'background-color: red;')
14
+ # second_row = Html::Row.new(cols, name: 'row_name', class: 'row_class', style: 'background-color: green;')
15
+
16
+ # html_table.write_row(0, first_row)
17
+ # html_table.write_row(1, second_row)
18
+
19
+ cols = [Html::Col.new('Text', name: 'col_name', style: 'col_style')]
20
+ html_table.write_row(0, Html::Row.new(cols, name: 'row_name', class: 'row_class'))
21
+
22
+ puts html_table
metadata ADDED
@@ -0,0 +1,141 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: html_table_dsl
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - GorohovAlex
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2023-08-29 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2.0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: fasterer
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 0.10.1
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 0.10.1
41
+ - !ruby/object:Gem::Dependency
42
+ name: pry
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 0.14.2
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 0.14.2
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.12'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.12'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rubocop-rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: 2.23.2
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: 2.23.2
83
+ - !ruby/object:Gem::Dependency
84
+ name: simplecov
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: 0.22.0
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: 0.22.0
97
+ description: DLS for working with HTML tables. Easy creation and population of tables
98
+ in Ruby.
99
+ email:
100
+ - gorochov.as@gmail.com
101
+ executables: []
102
+ extensions: []
103
+ extra_rdoc_files: []
104
+ files:
105
+ - ".gitignore"
106
+ - ".rspec"
107
+ - ".rubocop.yml"
108
+ - Gemfile
109
+ - Gemfile.lock
110
+ - README.md
111
+ - lib/html/col.rb
112
+ - lib/html/head_col.rb
113
+ - lib/html/row.rb
114
+ - lib/html/table.rb
115
+ - lib/html/tag.rb
116
+ - lib/html/tbody.rb
117
+ - lib/html/thead.rb
118
+ - main.rb
119
+ homepage: https://github.com/GorohovAlex/html_table_dsl
120
+ licenses: []
121
+ metadata: {}
122
+ post_install_message:
123
+ rdoc_options: []
124
+ require_paths:
125
+ - lib
126
+ required_ruby_version: !ruby/object:Gem::Requirement
127
+ requirements:
128
+ - - ">="
129
+ - !ruby/object:Gem::Version
130
+ version: '0'
131
+ required_rubygems_version: !ruby/object:Gem::Requirement
132
+ requirements:
133
+ - - ">="
134
+ - !ruby/object:Gem::Version
135
+ version: '0'
136
+ requirements: []
137
+ rubygems_version: 3.2.22
138
+ signing_key:
139
+ specification_version: 4
140
+ summary: HTML-table DSL
141
+ test_files: []