tabled 0.0.5 → 0.0.6

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: 6975cba3c924564332a058b0780101a7e469e25ca6143d9743bd19ea3e45c0c5
4
- data.tar.gz: d3de8444e683807a3490f2c7fa34e5c0c3538054923036d2981659074e0bc31e
3
+ metadata.gz: 724a82614926269cb03016b9f3d8b7115d7d2b5257427b35b2653e5ddc159b07
4
+ data.tar.gz: 7fbf7a9518cd7a5421cc3bb8575f6c43f601ee7e42167bfa17ca5a34fb557cb6
5
5
  SHA512:
6
- metadata.gz: d161770b55f054132a064529a4eb4d420fb9172b8ccff788cba15b337f4110c22adde0ea2a26d36d05bacf99795de62a1e77bebf9b75919868cb04c77f83c194
7
- data.tar.gz: 2481ed7691562b8f78a17a8cfa5de798787a76385f64d55d545574639a514af4e5ba7ee040893ea3a3b721f57547d2b9b196efec4c044f4edbe61a11672c0371
6
+ metadata.gz: 4e0a7bb5e09def2c59446434d02ede9b7973a307bbc3bbdd3d033aeb14f39f7c152bd856a452e2c56471da1a2b33833df29c8686522cc876262c64ee39f3288e
7
+ data.tar.gz: 7683d779cd2241c372fc292c33f068508f3338c6a819860e99e1fab5bfbcb22d8162eef0be12144b9b8cf7d91444fb5ab91deb2b2f6d00324f332037e010eb2c
@@ -0,0 +1,24 @@
1
+ name: CodeMetrics report sending
2
+
3
+ on:
4
+ push:
5
+ branches: [ main ]
6
+ pull_request:
7
+ branches: [ main ]
8
+
9
+ permissions:
10
+ contents: read
11
+
12
+ jobs:
13
+
14
+ report_sending:
15
+ runs-on: ubuntu-latest
16
+ steps:
17
+ - uses: actions/checkout@v3
18
+ - name: Set up Ruby
19
+ uses: ruby/setup-ruby@v1
20
+ with:
21
+ ruby-version: 3.0
22
+ bundler-cache: true
23
+
24
+ - run: sh ./sending_report.sh
@@ -30,13 +30,14 @@ jobs:
30
30
  with:
31
31
  ruby-version: ${{ matrix.ruby-version }}
32
32
  bundler-cache: true
33
+ - run: bundle exec rubocop
33
34
 
34
35
  test:
35
36
  runs-on: ${{ matrix.os }}
36
37
  strategy:
37
38
  matrix:
38
39
  os: [ubuntu-latest, macos-latest]
39
- ruby-version: ['2.6', '2.7', '3.0']
40
+ ruby-version: ['2.7', '3.0', '3.1', '3.2']
40
41
  steps:
41
42
  - uses: actions/checkout@v3
42
43
  - name: Set up Ruby
data/.gitignore CHANGED
@@ -1,4 +1,5 @@
1
1
  .byebug_history
2
2
  coverage
3
3
  .idea
4
- *.gem
4
+ *.gem
5
+ .DS_Store
data/.rubocop_todo.yml CHANGED
@@ -1,42 +1,42 @@
1
1
  # This configuration was generated by
2
2
  # `rubocop --auto-gen-config --auto-gen-only-exclude`
3
- # on 2022-06-24 15:05:43 UTC using RuboCop version 1.29.1.
3
+ # on 2023-09-06 20:39:31 UTC using RuboCop version 1.56.2.
4
4
  # The point is for the user to remove these configuration records
5
5
  # one by one as the offenses are removed from the code base.
6
6
  # Note that changes in the inspected code, or installation of new
7
7
  # versions of RuboCop, may require this file to be generated again.
8
8
 
9
+ # Offense count: 4
10
+ # Configuration parameters: EnforcedStyle, AllowedGems, Include.
11
+ # SupportedStyles: Gemfile, gems.rb, gemspec
12
+ # Include: **/*.gemspec, **/Gemfile, **/gems.rb
13
+ Gemspec/DevelopmentDependencies:
14
+ Exclude:
15
+ - 'tabled.gemspec'
16
+
9
17
  # Offense count: 1
10
- # Configuration parameters: Include.
18
+ # Configuration parameters: Severity, Include.
11
19
  # Include: **/*.gemspec
12
20
  Gemspec/RequiredRubyVersion:
13
21
  Exclude:
14
22
  - 'tabled.gemspec'
15
23
 
16
- # Offense count: 3
17
- # Configuration parameters: IgnoredMethods, CountRepeatedAttributes, Max.
24
+ # Offense count: 4
25
+ # Configuration parameters: AllowedMethods, AllowedPatterns, CountRepeatedAttributes, Max.
18
26
  Metrics/AbcSize:
19
27
  Exclude:
20
28
  - 'lib/content_shaper.rb'
29
+ - 'lib/file_builders/csv_file_builder.rb'
21
30
  - 'lib/helpers.rb'
22
31
 
23
- # Offense count: 3
24
- # Configuration parameters: CountComments, Max, CountAsOne, ExcludedMethods, IgnoredMethods.
25
- # IgnoredMethods: refine
26
- Metrics/BlockLength:
27
- Exclude:
28
- - '**/*.gemspec'
29
- - 'spec/framed/tabled_spec.rb'
30
- - 'spec/non_framed/tabled_spec.rb'
31
-
32
32
  # Offense count: 1
33
- # Configuration parameters: IgnoredMethods, Max.
33
+ # Configuration parameters: AllowedMethods, AllowedPatterns, Max.
34
34
  Metrics/CyclomaticComplexity:
35
35
  Exclude:
36
36
  - 'lib/helpers.rb'
37
37
 
38
38
  # Offense count: 5
39
- # Configuration parameters: CountComments, Max, CountAsOne, ExcludedMethods, IgnoredMethods.
39
+ # Configuration parameters: CountComments, Max, CountAsOne, AllowedMethods, AllowedPatterns.
40
40
  Metrics/MethodLength:
41
41
  Exclude:
42
42
  - 'lib/content_shaper.rb'
@@ -44,18 +44,31 @@ Metrics/MethodLength:
44
44
  - 'spec/factories/factory_data.rb'
45
45
 
46
46
  # Offense count: 1
47
- # Configuration parameters: IgnoredMethods, Max.
47
+ # Configuration parameters: AllowedMethods, AllowedPatterns, Max.
48
48
  Metrics/PerceivedComplexity:
49
49
  Exclude:
50
50
  - 'lib/helpers.rb'
51
51
 
52
- # Offense count: 1
52
+ # Offense count: 4
53
53
  # Configuration parameters: Max, CountAsOne.
54
54
  RSpec/ExampleLength:
55
55
  Exclude:
56
- - 'spec/framed/tabled_spec.rb'
56
+ - 'spec/integration/file_builders/export_csv_spec.rb'
57
+ - 'spec/integration/file_builders/export_json_spec.rb'
58
+ - 'spec/unit/tabled_spec.rb'
59
+
60
+ # Offense count: 1
61
+ RSpec/MultipleDescribes:
62
+ Exclude:
63
+ - 'spec/integration/tabled_spec.rb'
64
+
65
+ # Offense count: 7
66
+ RSpec/RepeatedDescription:
67
+ Exclude:
68
+ - 'spec/integration/tabled_spec.rb'
69
+ - 'spec/unit/tabled_spec.rb'
57
70
 
58
- # Offense count: 9
71
+ # Offense count: 12
59
72
  # Configuration parameters: AllowedConstants.
60
73
  Style/Documentation:
61
74
  Exclude:
@@ -63,6 +76,9 @@ Style/Documentation:
63
76
  - 'test/**/*'
64
77
  - 'bin/tabled'
65
78
  - 'lib/content_shaper.rb'
79
+ - 'lib/file_builders/base_file_builder.rb'
80
+ - 'lib/file_builders/csv_file_builder.rb'
81
+ - 'lib/file_builders/json_file_builder.rb'
66
82
  - 'lib/helpers.rb'
67
83
  - 'lib/parsers/csv_parser.rb'
68
84
  - 'lib/tabled.rb'
data/CHANGELOG.md CHANGED
@@ -1,3 +1,6 @@
1
+ ### Version 0.0.6 (September 05, 2023)
2
+ 1. Tabled now can export data into CSV and JSON files
3
+
1
4
  ### Version 0.0.5 (September 04, 2023)
2
5
  1. (Bug fix) Unnecessary dev gem reference removed
3
6
 
data/Gemfile.lock CHANGED
@@ -1,59 +1,75 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- tabled (0.0.5)
4
+ tabled (0.0.6)
5
5
  dry-cli
6
6
 
7
7
  GEM
8
8
  remote: http://rubygems.org/
9
9
  specs:
10
10
  ast (2.4.2)
11
+ base64 (0.1.1)
11
12
  diff-lcs (1.5.0)
12
13
  docile (1.4.0)
13
14
  dry-cli (1.0.0)
14
- parallel (1.22.1)
15
- parser (3.1.2.0)
15
+ json (2.6.3)
16
+ language_server-protocol (3.17.0.3)
17
+ parallel (1.23.0)
18
+ parser (3.2.2.3)
16
19
  ast (~> 2.4.1)
20
+ racc
21
+ racc (1.7.1)
17
22
  rainbow (3.1.1)
18
- regexp_parser (2.4.0)
19
- rexml (3.2.5)
20
- rspec (3.11.0)
21
- rspec-core (~> 3.11.0)
22
- rspec-expectations (~> 3.11.0)
23
- rspec-mocks (~> 3.11.0)
24
- rspec-core (3.11.0)
25
- rspec-support (~> 3.11.0)
26
- rspec-expectations (3.11.0)
23
+ regexp_parser (2.8.1)
24
+ rexml (3.2.6)
25
+ rspec (3.12.0)
26
+ rspec-core (~> 3.12.0)
27
+ rspec-expectations (~> 3.12.0)
28
+ rspec-mocks (~> 3.12.0)
29
+ rspec-core (3.12.2)
30
+ rspec-support (~> 3.12.0)
31
+ rspec-expectations (3.12.3)
27
32
  diff-lcs (>= 1.2.0, < 2.0)
28
- rspec-support (~> 3.11.0)
29
- rspec-mocks (3.11.1)
33
+ rspec-support (~> 3.12.0)
34
+ rspec-mocks (3.12.6)
30
35
  diff-lcs (>= 1.2.0, < 2.0)
31
- rspec-support (~> 3.11.0)
32
- rspec-support (3.11.0)
33
- rubocop (1.29.1)
36
+ rspec-support (~> 3.12.0)
37
+ rspec-support (3.12.1)
38
+ rubocop (1.56.2)
39
+ base64 (~> 0.1.1)
40
+ json (~> 2.3)
41
+ language_server-protocol (>= 3.17.0)
34
42
  parallel (~> 1.10)
35
- parser (>= 3.1.0.0)
43
+ parser (>= 3.2.2.3)
36
44
  rainbow (>= 2.2.2, < 4.0)
37
45
  regexp_parser (>= 1.8, < 3.0)
38
46
  rexml (>= 3.2.5, < 4.0)
39
- rubocop-ast (>= 1.17.0, < 2.0)
47
+ rubocop-ast (>= 1.28.1, < 2.0)
40
48
  ruby-progressbar (~> 1.7)
41
- unicode-display_width (>= 1.4.0, < 3.0)
42
- rubocop-ast (1.18.0)
43
- parser (>= 3.1.1.0)
44
- rubocop-rspec (2.10.0)
45
- rubocop (~> 1.19)
46
- ruby-progressbar (1.11.0)
47
- simplecov (0.21.2)
49
+ unicode-display_width (>= 2.4.0, < 3.0)
50
+ rubocop-ast (1.29.0)
51
+ parser (>= 3.2.1.0)
52
+ rubocop-capybara (2.18.0)
53
+ rubocop (~> 1.41)
54
+ rubocop-factory_bot (2.23.1)
55
+ rubocop (~> 1.33)
56
+ rubocop-rspec (2.23.2)
57
+ rubocop (~> 1.33)
58
+ rubocop-capybara (~> 2.17)
59
+ rubocop-factory_bot (~> 2.22)
60
+ ruby-progressbar (1.13.0)
61
+ simplecov (0.22.0)
48
62
  docile (~> 1.1)
49
63
  simplecov-html (~> 0.11)
50
64
  simplecov_json_formatter (~> 0.1)
51
65
  simplecov-html (0.12.3)
52
66
  simplecov_json_formatter (0.1.4)
53
- unicode-display_width (2.1.0)
67
+ unicode-display_width (2.4.2)
54
68
 
55
69
  PLATFORMS
56
- ruby
70
+ arm64-darwin-22
71
+ x86_64-darwin-20
72
+ x86_64-linux
57
73
 
58
74
  DEPENDENCIES
59
75
  rspec
data/README.md CHANGED
@@ -3,7 +3,7 @@
3
3
 
4
4
 
5
5
  # Description
6
- Library can be used to render your data to a console. Though it's quite simple, but has many features.
6
+ Library can be used to render your data to a console. Though it's quite simple, but has many features.
7
7
 
8
8
  # How to use
9
9
  1. Install the gem `bundle add tabled` or `gem install tabled`
@@ -11,7 +11,7 @@ Library can be used to render your data to a console. Though it's quite simple,
11
11
  3. Pass to the application array of rows. Each row may have any amount of columns and optional footer text.
12
12
 
13
13
  ### Params for Tabled instance
14
- Tabled accepts two params. Data which is prohibited and options.
14
+ Tabled accepts two params. Data which is prohibited and options.
15
15
  Available options:
16
16
  1. `framed` - optional, default is true
17
17
  2. `row_separator` - optional, default is `-`. Can be `nil` if you don't need separate rows.
@@ -27,7 +27,7 @@ data = [
27
27
  ["Alan", "23 years", "Male"],
28
28
  ]
29
29
 
30
- Tabled.new(data, framed: false,
30
+ Tabled.new(data, framed: false,
31
31
  row_separator: nil).print_to_console
32
32
  ```
33
33
 
@@ -59,6 +59,29 @@ Result
59
59
  ----------------------------------------------------------------
60
60
  ```
61
61
 
62
+ ### Export data to CSV or JSON file
63
+ Passed data can be exported as a file. Available formats are `csv` and `json`. Both parameters are optional. By default, file will be saved in the current directory and with file name `tabled.csv` and file format is `CSV`.
64
+
65
+ ```ruby
66
+ # CSV
67
+ data = [
68
+ ["Helena", "20 years", "Female"],
69
+ ]
70
+
71
+ Tabled
72
+ .new(data)
73
+ .export_to_csv
74
+
75
+ # JSON
76
+ data = [
77
+ ["Helena", "20 years", "Female"],
78
+ ]
79
+
80
+ Tabled
81
+ .new(data, titles: ['Name', 'Age', 'Gender'])
82
+ .export_to_csv(format: :json)
83
+ ```
84
+
62
85
  # Printing CSV files to console
63
86
  As a part of the gem `tabled` binary is included. So, you can basicaly run it like:
64
87
  ```shell
data/demo.rb CHANGED
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require_relative './lib/tabled'
3
+ require_relative 'lib/tabled'
4
4
 
5
5
  STAR_WARS_CHARACTERS = [
6
6
  {
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'csv_file_builder'
4
+ require_relative 'json_file_builder'
5
+
6
+ module BaseFileBuilder
7
+ def export_to_file(file_name: 'tabled.csv', format: :csv)
8
+ case format
9
+ when :csv
10
+ FileBuilders::CSVFileBuilder.new(data, file_name, options).export_to_file
11
+ when :json
12
+ FileBuilders::JSONFileBuilder.new(data, file_name, options).export_to_file
13
+ else
14
+ raise 'Not implemented yet'
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ module FileBuilders
4
+ class CSVFileBuilder
5
+ attr_reader :data, :file_name, :options
6
+
7
+ def initialize(data, file_name, options)
8
+ @data = data
9
+ @file_name = file_name
10
+ @options = options
11
+ end
12
+
13
+ def export_to_file
14
+ titles_row = (options[:titles].join(';').concat(';').concat("\n") if options[:titles].size.positive?)
15
+
16
+ data_without_options = data.map { |row| row[0...-1] }
17
+ csv_data = data_without_options.map { |row| row.join(';').concat(';') }.join("\n")
18
+
19
+ File.open(file_name, 'a') do |file|
20
+ file << titles_row
21
+ file << csv_data
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,28 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'json'
4
+
5
+ module FileBuilders
6
+ class JSONFileBuilder
7
+ attr_reader :data, :file_name, :options, :titles
8
+
9
+ def initialize(data, file_name, options)
10
+ @data = data
11
+ @file_name = file_name
12
+ @options = options
13
+ @titles = options[:titles]
14
+ end
15
+
16
+ def export_to_file
17
+ return puts 'For JSON format titles are required' if titles.empty?
18
+
19
+ json_data = data.inject([]) do |result, row|
20
+ result << titles.zip(row).to_h
21
+ end
22
+
23
+ File.open(file_name, 'a') do |file|
24
+ file << JSON.pretty_generate(json_data)
25
+ end
26
+ end
27
+ end
28
+ end
data/lib/helpers.rb CHANGED
@@ -9,15 +9,16 @@ class Tabled
9
9
  data.each do |row|
10
10
  # [0..-2] Due to reason last item is always hash with row params
11
11
  row[0..-2].each_with_index do |column, index|
12
- columns_width[index] = column.to_s.size if column.to_s.size > (columns_width[index] || 0)
12
+ column_size = column.to_s.gsub(/\e\[[0-9;]*m/, '').size
13
+ columns_width[index] = column_size if column_size > (columns_width[index] || 0)
13
14
  end
14
15
  end
15
16
 
16
17
  # If titles longer than data use titles column width
17
18
  columns_width.each_with_index do |_column_size, index|
18
- title_size = options[:titles].fetch(index, '').size
19
+ title_size = options[:titles].fetch(index, '').gsub(/\e\[[0-9;]*m/, '').size
19
20
 
20
- columns_width[index] = title_size > columns_width[index] ? title_size : columns_width[index]
21
+ columns_width[index] = [title_size, columns_width[index]].max
21
22
  end
22
23
 
23
24
  # Add 1 space in the end of each column
@@ -28,7 +29,7 @@ class Tabled
28
29
  # Add to the last column footer spaces if needed
29
30
  longest_footer_size = # longest footers
30
31
  data.map do |row|
31
- row.last.fetch(:footer, '')
32
+ row.last.fetch(:footer, '').gsub(/\e\[[0-9;]*m/, '')
32
33
  end.max.to_s.size + 1
33
34
  last_col_spaces = longest_footer_size - columns_width.sum # spaces required to the end
34
35
  columns_width[-1] = columns_width.last + last_col_spaces if last_col_spaces.positive?
data/lib/tabled.rb CHANGED
@@ -1,12 +1,15 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require_relative './template'
4
- require_relative './helpers'
5
- require_relative './content_shaper'
3
+ require_relative 'template'
4
+ require_relative 'helpers'
5
+ require_relative 'content_shaper'
6
+ require_relative 'file_builders/base_file_builder'
6
7
 
7
8
  class Tabled
9
+ include BaseFileBuilder
10
+
8
11
  DEFAULT_OPTIONS = { framed: true, row_separator: '-', titles: [] }.freeze
9
- attr_accessor :data, :columns_width, :content, :options
12
+ attr_accessor :data, :data_without_options, :columns_width, :content, :options
10
13
 
11
14
  def initialize(data, **options)
12
15
  @options = DEFAULT_OPTIONS.merge(options)
data/lib/template.rb CHANGED
@@ -6,7 +6,7 @@ class Tabled
6
6
  # | Luke Skywalker 172 male |
7
7
  def self.render(row, columns_width, _is_framed)
8
8
  row[0..-2].map.with_index do |column, index|
9
- spaces = ' ' * (columns_width[index] - column.to_s.size)
9
+ spaces = ' ' * (columns_width[index] - column.to_s.gsub(/\e\[[0-9;]*m/, '').size)
10
10
  column.to_s + spaces
11
11
  end.join
12
12
  end
@@ -29,7 +29,7 @@ class Tabled
29
29
  return nil unless row.last.fetch(:footer, false)
30
30
 
31
31
  footer = row.last.fetch(:footer)
32
- required_spaces = columns_width.sum - footer.size
32
+ required_spaces = columns_width.sum - footer.gsub(/\e\[[0-9;]*m/, '').size
33
33
  footer + (' ' * required_spaces)
34
34
  end
35
35
  end
data/sending_report.sh ADDED
@@ -0,0 +1,9 @@
1
+ rm .rubocop.yml
2
+
3
+ bundle exec rubocop -f json -o rubocop-report.json
4
+
5
+ curl --location 'https://codemetrics.dev/reports' \
6
+ --form 'report=@"./rubocop-report.json"' \
7
+ --form 'tool="rubocop"' \
8
+ --form 'token="SFMyNTY.g2gDYQduBgCX0w9migFiAAFRgA.w0a9y7VQGzfLkr2G1Cxxwm7m9uTcl99c24dh3zpKrl0"' \
9
+ --form 'branch="'"$GITHUB_HEAD_REF"'"'
data/tabled.gemspec CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = 'tabled'
5
- s.version = '0.0.5'
5
+ s.version = '0.0.6'
6
6
  s.summary = 'Library for rendering pretty tables in console'
7
7
  s.description = 'Library can be used to render your data to a console. Being quite simple it has many features.'
8
8
  s.authors = ['Max Rukomoynikov']
@@ -17,8 +17,7 @@ Gem::Specification.new do |s|
17
17
  'rubygems_mfa_required' => 'true' }
18
18
  s.license = 'MIT'
19
19
 
20
- s.add_dependency 'dry-cli'
21
-
20
+ s.add_dependency 'dry-cli' # For CLI ap
22
21
  s.add_development_dependency 'rspec'
23
22
  s.add_development_dependency 'rubocop'
24
23
  s.add_development_dependency 'rubocop-rspec'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tabled
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: 0.0.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Max Rukomoynikov
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-09-05 00:00:00.000000000 Z
11
+ date: 2023-09-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: dry-cli
@@ -88,6 +88,7 @@ executables:
88
88
  extensions: []
89
89
  extra_rdoc_files: []
90
90
  files:
91
+ - ".github/workflows/codemetrics.yml"
91
92
  - ".github/workflows/linters.yml"
92
93
  - ".gitignore"
93
94
  - ".rspec"
@@ -100,10 +101,14 @@ files:
100
101
  - bin/tabled
101
102
  - demo.rb
102
103
  - lib/content_shaper.rb
104
+ - lib/file_builders/base_file_builder.rb
105
+ - lib/file_builders/csv_file_builder.rb
106
+ - lib/file_builders/json_file_builder.rb
103
107
  - lib/helpers.rb
104
108
  - lib/parsers/csv_parser.rb
105
109
  - lib/tabled.rb
106
110
  - lib/template.rb
111
+ - sending_report.sh
107
112
  - tabled.gemspec
108
113
  homepage: https://rubygems.org/gems/tabled
109
114
  licenses: