simple_tabler 0.1.0

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: fa3cb33da89108fb63317f82e46ac6d26da3172af8c78b300139253cb9c3c7c0
4
+ data.tar.gz: 0efcaab18aeac0c92f5b89923d95852b53b7a2658dc14ceb2c107ccb6747a9a2
5
+ SHA512:
6
+ metadata.gz: 5a8570aaedb9deae427f66cdde705ff40c8012ef2e7a5a2a2392093697e22f3a736c4c666dc7ffd9f9016d5b8744ad7fa82d9cc88b2ea8dc3c028ea7c22425f7
7
+ data.tar.gz: 2c89697b977645a451c30464a24fe432f0243c3825e649323901fef17012f44abbaf66bfe37f0ef70bf800ea56548185cc054505d9844f509171456b0160d042
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,13 @@
1
+ AllCops:
2
+ TargetRubyVersion: 2.6
3
+
4
+ Style/StringLiterals:
5
+ Enabled: true
6
+ EnforcedStyle: double_quotes
7
+
8
+ Style/StringLiteralsInInterpolation:
9
+ Enabled: true
10
+ EnforcedStyle: double_quotes
11
+
12
+ Layout/LineLength:
13
+ Max: 120
data/Gemfile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in simple_tabler.gemspec
6
+ gemspec
7
+
8
+ gem "rake", "~> 13.0"
9
+
10
+ gem "rspec", "~> 3.0"
11
+
12
+ gem "rubocop", "~> 1.21"
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2022 Takahashi-Riki
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,33 @@
1
+ # SimpleTabler
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/simple_tabler`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Install the gem and add to the application's Gemfile by executing:
10
+
11
+ $ bundle add simple_tabler
12
+
13
+ If bundler is not being used to manage dependencies, install the gem by executing:
14
+
15
+ $ gem install simple_tabler
16
+
17
+ ## Usage
18
+
19
+ TODO: Write usage instructions here
20
+
21
+ ## Development
22
+
23
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
24
+
25
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
26
+
27
+ ## Contributing
28
+
29
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/simple_tabler.
30
+
31
+ ## License
32
+
33
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ require "rubocop/rake_task"
9
+
10
+ RuboCop::RakeTask.new
11
+
12
+ task default: %i[spec rubocop]
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SimpleTabler
4
+ VERSION = "0.1.0"
5
+ end
@@ -0,0 +1,56 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "simple_tabler/version"
4
+ require_relative "table_from_array"
5
+
6
+ class Array; include SimpleTabler; end
7
+
8
+ module SimpleTabler
9
+ def dimension_equal_two?
10
+ self.each do |child_array|
11
+ if !child_array.kind_of?(Array)
12
+ return false
13
+ elsif child_array != child_array.flatten
14
+ return false
15
+ end
16
+ end
17
+ return true
18
+ end
19
+
20
+ def child_array_size_same?
21
+ child_array_sizes = self.map{|child_array| child_array.size}
22
+ child_array_sizes.uniq.size == 1
23
+ end
24
+
25
+ def element_type_correct?
26
+ correct_object_types = [String, Numeric, Symbol, TrueClass, FalseClass]
27
+ self.each do |child_array|
28
+ child_array.each do |element|
29
+ result_each_kind_of = correct_object_types.map{|object_type| element.kind_of?(object_type)}
30
+ return false if !result_each_kind_of.include?(true)
31
+ end
32
+ end
33
+ return true
34
+ end
35
+
36
+ def column_size_correct?(column_names)
37
+ self[0].size == column_names.size
38
+ end
39
+
40
+ def generate_table(column_names=nil, scale=20)
41
+ if !dimension_equal_two?
42
+ raise ArgumentError, "The depth of array should be two."
43
+ elsif !child_array_size_same?
44
+ raise ArgumentError, "All child arrays should have same amount of element."
45
+ elsif !element_type_correct?
46
+ raise ArgumentError, "All child arrays should have elements which is kind of allowed class.\nAllowed class is String, Numeric, Symbol, TrueClass, FalseClass"
47
+ elsif !column_names.nil? && !column_size_correct?(column_names)
48
+ raise ArgumentError, "All child arrays should have same amount of element with column names you passed as an argument."
49
+ end
50
+ column_size = self[0].size
51
+ table_generated_from_array = TableFromArray.new(column_size, column_names, scale)
52
+ table_generated_from_array.add_header
53
+ table_generated_from_array.add_content(self)
54
+ return table_generated_from_array.content_of_table
55
+ end
56
+ end
@@ -0,0 +1,52 @@
1
+ class TableFromArray
2
+ attr_reader :content_of_table, :column_names
3
+
4
+ def initialize(column_size, column_names, scale)
5
+ @content_of_table = ""
6
+ @column_size = column_size
7
+ @column_names = column_names
8
+ @scale = scale
9
+ end
10
+
11
+ def add_header
12
+ if @column_names
13
+ add_separation("=")
14
+ add_row(@column_names)
15
+ add_separation("=")
16
+ else
17
+ add_separation("-")
18
+ end
19
+ end
20
+
21
+ def add_content(table_materials)
22
+ table_materials.each do |row_materials|
23
+ add_row(row_materials)
24
+ add_separation("-")
25
+ end
26
+ end
27
+
28
+ private
29
+ def max_length_of_array_element(array)
30
+ array.map{|element| element.to_s.size}.max
31
+ end
32
+
33
+ def add_row(row_materials)
34
+ content_of_row = ""
35
+ number_of_lines = max_length_of_array_element(row_materials) / @scale + 1
36
+ for i in 0...number_of_lines do
37
+ line = "|"
38
+ for row_material in row_materials do
39
+ row_material_sliced_for_this_line = row_material[@scale*i...@scale*(i+1)].to_s
40
+ space_to_fill_the_blank = " " * (@scale - row_material_sliced_for_this_line.size)
41
+ line += row_material_sliced_for_this_line + space_to_fill_the_blank + "|"
42
+ end
43
+ content_of_row += line + "\n"
44
+ end
45
+ @content_of_table += content_of_row
46
+ end
47
+
48
+ def add_separation(mark)
49
+ separation = "|" + mark * (@scale * @column_size + @column_size - 1) + "|" + "\n"
50
+ @content_of_table += separation
51
+ end
52
+ end
@@ -0,0 +1,4 @@
1
+ module SimpleTabler
2
+ VERSION: String
3
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
+ end
metadata ADDED
@@ -0,0 +1,55 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: simple_tabler
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - mew3880
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2022-03-17 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description:
14
+ email:
15
+ - mew3880@gmail.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - ".rspec"
21
+ - ".rubocop.yml"
22
+ - Gemfile
23
+ - LICENSE.txt
24
+ - README.md
25
+ - Rakefile
26
+ - lib/simple_tabler.rb
27
+ - lib/simple_tabler/version.rb
28
+ - lib/table_from_array.rb
29
+ - sig/simple_tabler.rbs
30
+ homepage: https://github.com/Takahashi-Riki/simple_tabler
31
+ licenses:
32
+ - MIT
33
+ metadata:
34
+ homepage_uri: https://github.com/Takahashi-Riki/simple_tabler
35
+ source_code_uri: https://github.com/Takahashi-Riki/simple_tabler
36
+ post_install_message:
37
+ rdoc_options: []
38
+ require_paths:
39
+ - lib
40
+ required_ruby_version: !ruby/object:Gem::Requirement
41
+ requirements:
42
+ - - ">="
43
+ - !ruby/object:Gem::Version
44
+ version: 2.6.0
45
+ required_rubygems_version: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - ">="
48
+ - !ruby/object:Gem::Version
49
+ version: '0'
50
+ requirements: []
51
+ rubygems_version: 3.0.3
52
+ signing_key:
53
+ specification_version: 4
54
+ summary: Simple Tabler helps you to make a table in Terminal
55
+ test_files: []