tabled 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: c9ad158738f50082bbcc59da4ab6e84e6d1d3a336bd4168623c72a037144df34
4
+ data.tar.gz: 679b46c1e778597edcb871484106aaeaccc70a8153ad75749f8116eae563adbe
5
+ SHA512:
6
+ metadata.gz: cc7dc1ee13cd9f4085ada67db778e79153f4bec054501d745694644155186fff9a46335120f384c796c42d55cf69d641d358865a74c7fd7edbc86835b67dfed6
7
+ data.tar.gz: 5eda9d7584f0bd89813ff666f840d281e56fd1f5cc2617eedf8b7366da258b830a3774752b74ccbdb87921fb35e5a70a0651736f4c45c1287af5055c428d0193
@@ -0,0 +1,35 @@
1
+ # This workflow uses actions that are not certified by GitHub.
2
+ # They are provided by a third-party and are governed by
3
+ # separate terms of service, privacy policy, and support
4
+ # documentation.
5
+ # This workflow will download a prebuilt Ruby version, install dependencies and run tests with Rake
6
+ # For more information see: https://github.com/marketplace/actions/setup-ruby-jruby-and-truffleruby
7
+
8
+ name: Ruby
9
+
10
+ on:
11
+ push:
12
+ branches: [ main ]
13
+ pull_request:
14
+ branches: [ main ]
15
+
16
+ permissions:
17
+ contents: read
18
+
19
+ jobs:
20
+ test:
21
+
22
+ runs-on: ubuntu-latest
23
+ strategy:
24
+ matrix:
25
+ ruby-version: ['2.6', '2.7', '3.0']
26
+
27
+ steps:
28
+ - uses: actions/checkout@v3
29
+ - name: Set up Ruby
30
+ uses: ruby/setup-ruby@2b019609e2b0f1ea1a2bc8ca11cb82ab46ada124
31
+ with:
32
+ ruby-version: ${{ matrix.ruby-version }}
33
+ bundler-cache: true # runs 'bundle install' and caches installed gems automatically
34
+ - name: Run tests
35
+ run: bundle exec rspec
data/.gitignore ADDED
@@ -0,0 +1,3 @@
1
+ .byebug_history
2
+ coverage
3
+ .idea
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'http://rubygems.org'
2
+
3
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,42 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ tabled (0.0.1)
5
+
6
+ GEM
7
+ remote: http://rubygems.org/
8
+ specs:
9
+ byebug (11.1.3)
10
+ diff-lcs (1.5.0)
11
+ docile (1.4.0)
12
+ rspec (3.11.0)
13
+ rspec-core (~> 3.11.0)
14
+ rspec-expectations (~> 3.11.0)
15
+ rspec-mocks (~> 3.11.0)
16
+ rspec-core (3.11.0)
17
+ rspec-support (~> 3.11.0)
18
+ rspec-expectations (3.11.0)
19
+ diff-lcs (>= 1.2.0, < 2.0)
20
+ rspec-support (~> 3.11.0)
21
+ rspec-mocks (3.11.1)
22
+ diff-lcs (>= 1.2.0, < 2.0)
23
+ rspec-support (~> 3.11.0)
24
+ rspec-support (3.11.0)
25
+ simplecov (0.21.2)
26
+ docile (~> 1.1)
27
+ simplecov-html (~> 0.11)
28
+ simplecov_json_formatter (~> 0.1)
29
+ simplecov-html (0.12.3)
30
+ simplecov_json_formatter (0.1.4)
31
+
32
+ PLATFORMS
33
+ ruby
34
+
35
+ DEPENDENCIES
36
+ byebug
37
+ rspec
38
+ simplecov
39
+ tabled!
40
+
41
+ BUNDLED WITH
42
+ 2.2.3
data/lib/tabled.rb ADDED
@@ -0,0 +1,55 @@
1
+ require 'byebug'
2
+ require 'template'
3
+
4
+ class Tabled
5
+ DEFAULT_OPTIONS = { borders: false }
6
+ attr_accessor :data, :columns_width, :content, :options
7
+
8
+ def initialize(data, **options)
9
+ @options = DEFAULT_OPTIONS.merge(options)
10
+ @data = data
11
+ @columns_width = []
12
+
13
+ convert_to_required_structure
14
+ calculate_columns_width
15
+ prepare_content
16
+ end
17
+
18
+
19
+ def print_to_console
20
+ print content.join("\n")
21
+ end
22
+
23
+ private
24
+
25
+ def convert_to_required_structure
26
+ self.data = data.map { |row|
27
+ row << {} unless row.last.is_a?(Hash)
28
+ row
29
+ }
30
+ end
31
+
32
+ def calculate_columns_width
33
+ data.each do |row|
34
+ row_without_params = row[0..-2]
35
+ row_without_params.each_with_index { |column, index|
36
+ is_column_last = row_without_params.count == index + 1
37
+ if is_column_last
38
+ possible_new_value = [row.last.fetch(:footer, '').to_s.size, column.to_s.size].sort.last
39
+ self.columns_width[index] = possible_new_value if possible_new_value > (self.columns_width[index] || 0)
40
+ else
41
+ self.columns_width[index] = column.to_s.size if column.to_s.size > (self.columns_width[index] || 0)
42
+ end
43
+ }
44
+ end
45
+
46
+ self.columns_width = columns_width.map { |column_width| column_width + 1 }
47
+ end
48
+
49
+ def prepare_content
50
+ self.content = data.inject([]) { |enumerator, row|
51
+ enumerator << Tabled::Template::Row.render(row, columns_width, options)
52
+ enumerator << Tabled::Template::RowFooter.render(row)
53
+ }.compact
54
+ end
55
+ end
data/lib/template.rb ADDED
@@ -0,0 +1,24 @@
1
+ class Tabled
2
+ class Template
3
+ class Row
4
+ def self.render(row, columns_width, options)
5
+ row[0..-2].map.with_index { |column, index|
6
+ spaces = ' ' * (columns_width[index] - column.to_s.size)
7
+ column.to_s + spaces
8
+ }.join('')
9
+ end
10
+ end
11
+ end
12
+ end
13
+
14
+ class Tabled
15
+ class Template
16
+ class RowFooter
17
+ def self.render(row)
18
+ return nil unless row.last.fetch(:footer, false)
19
+
20
+ row.last.fetch(:footer)
21
+ end
22
+ end
23
+ end
24
+ end
data/tabled.gemspec ADDED
@@ -0,0 +1,19 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = 'tabled'
3
+ s.version = '0.0.1'
4
+ s.summary = "Library for rendering pretty tables in console"
5
+ s.description = "This library can be used to render your data to a console. It's quite simple and has many features"
6
+ s.authors = ["Max Rukomoynikov"]
7
+ s.email = 'rukomoynikov@gmail.com'
8
+ s.files =
9
+ Dir.chdir(File.expand_path(__dir__)) do
10
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{\A(?:test|spec|features)/}) }
11
+ end
12
+ s.homepage = 'https://rubygems.org/gems/tabled'
13
+ s.metadata = { "source_code_uri" => "https://github.com/Rukomoynikov/tabled" }
14
+ s.license = 'MIT'
15
+
16
+ s.add_development_dependency "byebug"
17
+ s.add_development_dependency "rspec"
18
+ s.add_development_dependency "simplecov"
19
+ end
metadata ADDED
@@ -0,0 +1,93 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: tabled
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Max Rukomoynikov
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2022-05-11 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: byebug
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rspec
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: simplecov
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: This library can be used to render your data to a console. It's quite
56
+ simple and has many features
57
+ email: rukomoynikov@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".github/workflows/linters.yml"
63
+ - ".gitignore"
64
+ - Gemfile
65
+ - Gemfile.lock
66
+ - lib/tabled.rb
67
+ - lib/template.rb
68
+ - tabled.gemspec
69
+ homepage: https://rubygems.org/gems/tabled
70
+ licenses:
71
+ - MIT
72
+ metadata:
73
+ source_code_uri: https://github.com/Rukomoynikov/tabled
74
+ post_install_message:
75
+ rdoc_options: []
76
+ require_paths:
77
+ - lib
78
+ required_ruby_version: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ required_rubygems_version: !ruby/object:Gem::Requirement
84
+ requirements:
85
+ - - ">="
86
+ - !ruby/object:Gem::Version
87
+ version: '0'
88
+ requirements: []
89
+ rubygems_version: 3.2.3
90
+ signing_key:
91
+ specification_version: 4
92
+ summary: Library for rendering pretty tables in console
93
+ test_files: []