html_table_dsl 0.0.1 → 0.0.2

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
  SHA256:
3
- metadata.gz: e6d042b81a908f47f4a06115492bb0b3d59af150e00eaed401210f9735b6beb7
4
- data.tar.gz: 9f1ec8604e62a81ec9870044b367436036a163e50c87ab70fe0bcfed1548b834
3
+ metadata.gz: c3d8bba615e6b3109d0bc42b772ee8fb9af58fbba61429255b2f843caefe1011
4
+ data.tar.gz: 632c3177d6f57eb9fda67418d322e83758ff5a3db2bb134371a029d778877c6d
5
5
  SHA512:
6
- metadata.gz: b278ae4bfe43fbb99e422a15007e06b0801d39e52a59232b7c177a7a4e0a7e36a80055d27bbe73f868e0275e74df67fce07a1f1fd6d95107272a73e00a9a5dd9
7
- data.tar.gz: 4689a55bcf08b252c4c3916e035ca2662abd0e01aad25589e638c4eca87b84c0f2c688b0b7284c5082df7eec3f5b87a36ee641d2ba342f7fd8dcc352807be5ac
6
+ metadata.gz: 9b5b93a8185a38b80bbbd87b1e1d497fd60cf4d9e1c87d063314010fda0a9bbc0083f5b9047a8d7297fd32603fd4990e92842ecde056ab00037a64a6cd9a4012
7
+ data.tar.gz: b95e69b2bfa0d947a4b3b34d93c1b6c79b1420187061798af63c12d4d50bba8e5938a051d3f776960f2c0c5d3599b02ad98707f5835fa6bd32f46da17f82363d
data/Gemfile CHANGED
@@ -2,4 +2,4 @@ source 'https://rubygems.org'
2
2
  git_source(:github) { |repo| "https://github.com/#{repo}.git" }
3
3
  git_source(:github) { 'https://github.com/GorohovAlex/html_table_dsl' }
4
4
 
5
- gemspec
5
+ gemspec
data/README.md CHANGED
@@ -1,6 +1,14 @@
1
1
  # HTML-table DSL
2
2
  DLS for working with HTML tables. Easy creation and population of tables in Ruby.
3
3
 
4
+ ## Install gem
5
+ ```
6
+ gem install html_table_dsl
7
+ ```
8
+ ## Use gem
9
+ ```
10
+ require 'html/table'
11
+ ```
4
12
  ## Create table
5
13
  ```ruby
6
14
  html_table = Html.table(1, 2, name: 'table_name', class: 'table_class')
@@ -14,8 +22,8 @@ html_table.attributes = { class: 'table_class_new', name: nil, style: 'border: 1
14
22
  ## Set headers
15
23
  ```ruby
16
24
  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)
25
+ head_rows = [Html::Row.new(head_cols, name: 'row_name', class: 'row_class')]
26
+ html_table.write_header(head_rows)
19
27
  # <table><thead><tr name='row_name' class='row_class'><th>Text</th></tr></thead><tbody> ... </tbody></table>
20
28
  ```
21
29
  ## Set rows
@@ -0,0 +1,27 @@
1
+ lib = File.expand_path('lib', __dir__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+
4
+ Gem::Specification.new do |spec|
5
+ spec.name = 'html_table_dsl'
6
+ spec.version = '0.0.2'
7
+ spec.authors = ['GorohovAlex']
8
+ spec.email = ['gorochov.as@gmail.com']
9
+
10
+ spec.summary = 'HTML-table DSL'
11
+ spec.description = 'DLS for working with HTML tables. Easy creation and population of tables in Ruby.'
12
+ spec.homepage = 'https://github.com/GorohovAlex/html_table_dsl'
13
+
14
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
15
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
16
+ end
17
+ spec.bindir = 'exe'
18
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
19
+ spec.require_paths = ['lib']
20
+
21
+ spec.add_development_dependency 'bundler', '~> 2.0'
22
+ spec.add_development_dependency 'fasterer', '~> 0.10.1'
23
+ spec.add_development_dependency 'pry', '~> 0.14.2'
24
+ spec.add_development_dependency 'rspec', '~> 3.12'
25
+ spec.add_development_dependency 'rubocop-rspec', '~> 2.23.2'
26
+ spec.add_development_dependency 'simplecov', '~> 0.22.0'
27
+ end
data/lib/error.rb ADDED
@@ -0,0 +1,6 @@
1
+ class ChildFormatError < StandardError
2
+ def initialize(allowed_classes)
3
+ message = "The child must be an Html class on of: #{allowed_classes.join(', ')}"
4
+ super(message)
5
+ end
6
+ end
data/lib/html/col.rb CHANGED
@@ -3,20 +3,10 @@ require_relative 'tag'
3
3
 
4
4
  module Html
5
5
  class Col < Tag
6
- def initialize(value = '', **attrs)
7
- super(**attrs)
8
-
9
- @value = value
10
- end
11
-
12
6
  private
13
7
 
14
8
  def tag_name
15
9
  :td
16
10
  end
17
-
18
- def value
19
- CGI.escapeHTML(@value.to_s)
20
- end
21
11
  end
22
12
  end
data/lib/html/row.rb CHANGED
@@ -1,24 +1,15 @@
1
1
  require_relative 'col'
2
+ require_relative 'head_col'
2
3
  require_relative 'tag'
3
4
 
4
5
  module Html
5
6
  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
7
+ ALLOWED_CHILDREN = [Html::Col, Html::HeadCol].freeze
13
8
 
14
9
  private
15
10
 
16
11
  def tag_name
17
12
  :tr
18
13
  end
19
-
20
- def value
21
- @cols&.map(&:to_s)&.join
22
- end
23
14
  end
24
15
  end
data/lib/html/table.rb CHANGED
@@ -10,10 +10,10 @@ module Html
10
10
 
11
11
  class Table < Tag
12
12
  def initialize(rows = 0, cols = 0, **attrs)
13
- super(**attrs)
13
+ @head_rows = []
14
+ @body_rows = Array.new(rows, Row.new(Array.new(cols, Col.new(' '))))
14
15
 
15
- @head_rows = Row.new(Array.new(0))
16
- @body_rows = Array.new(rows, Row.new(Array.new(cols, Col.new)))
16
+ super(**attrs)
17
17
  end
18
18
 
19
19
  def write_row(index, row)
@@ -24,18 +24,18 @@ module Html
24
24
  @head_rows = head_rows
25
25
  end
26
26
 
27
- private
28
-
29
- def tag_name
30
- :table
31
- end
32
-
33
27
  def value
34
28
  output_value = ''
35
29
  output_value += head.to_s unless head.empty?
36
30
  output_value + body.to_s unless body.empty?
37
31
  end
38
32
 
33
+ private
34
+
35
+ def tag_name
36
+ :table
37
+ end
38
+
39
39
  def head
40
40
  @head ||= Thead.new(@head_rows)
41
41
  end
data/lib/html/tag.rb CHANGED
@@ -1,27 +1,38 @@
1
- require 'cgi'
1
+ require './lib/error'
2
2
 
3
3
  module Html
4
4
  class Tag
5
- def initialize(**attrs)
5
+ ALLOWED_CHILDREN = [].freeze
6
+ attr_reader :value
7
+
8
+ def initialize(value = nil, **attrs)
6
9
  @attributes = attrs
10
+ @value = value
11
+ valid_children?
7
12
  end
8
13
 
9
14
  def to_s
10
- "<#{tag_name}#{read_attributes}>#{value}</#{tag_name}>"
15
+ "<#{tag_name}#{read_attributes}>#{read_value}</#{tag_name}>"
11
16
  end
12
17
 
13
18
  def attributes=(new_attributes)
14
19
  @attributes.merge!(new_attributes).compact!
15
20
  end
16
21
 
17
- private
18
-
19
22
  def tag_name
20
- raise NotImplementedError
23
+ nil
21
24
  end
22
25
 
23
- def value
24
- raise NotImplementedError
26
+ def empty?
27
+ return value.all?(&:empty?) if value.is_a?(Array)
28
+
29
+ value.nil? ? true : value&.empty?
30
+ end
31
+
32
+ private
33
+
34
+ def read_value
35
+ value.is_a?(Array) ? value.map(&:to_s).join : value
25
36
  end
26
37
 
27
38
  def read_attributes
@@ -31,5 +42,12 @@ module Html
31
42
  def attributes
32
43
  @attributes.map { |key, value| "#{CGI.escapeHTML(key.to_s)}='#{CGI.escapeHTML(value)}'" }.join(' ')
33
44
  end
45
+
46
+ def valid_children?
47
+ return true if @value.is_a?(String) || @value.nil?
48
+
49
+ all_allowed = @value&.all? { |value| self.class::ALLOWED_CHILDREN.include?(value.class) }
50
+ raise ChildFormatError, self.class::ALLOWED_CHILDREN unless all_allowed
51
+ end
34
52
  end
35
53
  end
data/lib/html/tbody.rb CHANGED
@@ -1,24 +1,13 @@
1
1
  require_relative 'tag'
2
+ require_relative 'row'
2
3
  module Html
3
4
  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
5
+ ALLOWED_CHILDREN = [Html::Row].freeze
13
6
 
14
7
  private
15
8
 
16
9
  def tag_name
17
10
  :tbody
18
11
  end
19
-
20
- def value
21
- @rows&.map(&:to_s)&.join
22
- end
23
12
  end
24
13
  end
data/lib/html/thead.rb CHANGED
@@ -1,25 +1,14 @@
1
1
  require_relative 'tag'
2
+ require_relative 'row'
2
3
 
3
4
  module Html
4
5
  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
6
+ ALLOWED_CHILDREN = [Html::Row].freeze
14
7
 
15
8
  private
16
9
 
17
10
  def tag_name
18
11
  :thead
19
12
  end
20
-
21
- def value
22
- @row
23
- end
24
13
  end
25
14
  end
data/main.rb CHANGED
@@ -3,20 +3,24 @@ require_relative './lib/html/head_col'
3
3
  require_relative './lib/html/row'
4
4
  require_relative './lib/html/table'
5
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' }
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
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)
9
+ head_cols = [Html::HeadCol.new('<i>Text</i>')]
10
+ head_row = [Html::Row.new(head_cols, name: 'row_name', class: 'row_class')]
11
+ html_table.write_header(head_row)
12
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;')
13
+ # Html::Row.new(cols, name: 'row_name', class: 'row_class111', style: 'background-color: red;')
14
+ # first_row = Html::Row.new([1], name: 'row_name', class: 'row_class111', style: 'background-color: red;')
14
15
  # second_row = Html::Row.new(cols, name: 'row_name', class: 'row_class', style: 'background-color: green;')
15
16
 
16
17
  # html_table.write_row(0, first_row)
18
+ # puts html_table
19
+ # first_row.attributes = { class: 'row_class222' }
20
+
17
21
  # html_table.write_row(1, second_row)
18
22
 
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'))
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'))
21
25
 
22
26
  puts html_table
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: html_table_dsl
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - GorohovAlex
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-08-29 00:00:00.000000000 Z
11
+ date: 2023-08-31 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -108,6 +108,8 @@ files:
108
108
  - Gemfile
109
109
  - Gemfile.lock
110
110
  - README.md
111
+ - html_table_dsl.gemspec
112
+ - lib/error.rb
111
113
  - lib/html/col.rb
112
114
  - lib/html/head_col.rb
113
115
  - lib/html/row.rb