simple_tabler 0.1.0 → 0.1.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: fa3cb33da89108fb63317f82e46ac6d26da3172af8c78b300139253cb9c3c7c0
4
- data.tar.gz: 0efcaab18aeac0c92f5b89923d95852b53b7a2658dc14ceb2c107ccb6747a9a2
3
+ metadata.gz: 673a1a57ecff0f6f4bf586b10cff347c3c40947c84685cf344a5fbe71ab7b13f
4
+ data.tar.gz: 4d10bc4df14395fea3b95050d10ba3c2e0af4896aeb60b3b1efbcf6cf0433142
5
5
  SHA512:
6
- metadata.gz: 5a8570aaedb9deae427f66cdde705ff40c8012ef2e7a5a2a2392093697e22f3a736c4c666dc7ffd9f9016d5b8744ad7fa82d9cc88b2ea8dc3c028ea7c22425f7
7
- data.tar.gz: 2c89697b977645a451c30464a24fe432f0243c3825e649323901fef17012f44abbaf66bfe37f0ef70bf800ea56548185cc054505d9844f509171456b0160d042
6
+ metadata.gz: 52a7b43f4b9bd90845ee426d257a88638c1fa57feaa0b685e8b03460e146b83093d3298e62ae1e3fb075bb407e5c543ba05444a7aef143f84a0ea01dde065077
7
+ data.tar.gz: dcb3e76b5778a510ee78e11b7f1720c2055b075c2a9c81f065e47447aac8c57aa3bb0d02dbb880a01252701ff0bb468694af5c6fffc071f7a9bd5caf7868e9c9
data/README.md CHANGED
@@ -1,32 +1,59 @@
1
- # SimpleTabler
1
+ # Simple Tabler
2
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.
3
+ Simple Tabler helps you to make a table in Terminal
4
4
 
5
- TODO: Delete this and the text above, and describe your gem
5
+ It can be used as a gem.(https://rubygems.org/gems/simple_tabler)
6
6
 
7
7
  ## Installation
8
8
 
9
- Install the gem and add to the application's Gemfile by executing:
9
+ Add simple_tabler to your Gemfile.
10
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
11
+ ```Gemfile
12
+ gem 'simple_tabler', '~> 0.1.0'
13
+ ```
16
14
 
17
15
  ## Usage
18
16
 
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).
17
+ First of all, you should import the module Simple Tabler.
18
+
19
+ ```.rb
20
+ require "simple_tabler"
21
+ ```
22
+
23
+ Now, you can create a table from an array like blow.
24
+
25
+ ```.rb
26
+ a = [
27
+ [
28
+ "ABCDE_Mail",
29
+ "example@example.com",
30
+ "abcdefg_" * 10
31
+ ],
32
+ [
33
+ "XYZ_TV",
34
+ "Taro Yamada",
35
+ "qwerty" * 10
36
+ ]
37
+ ]
38
+
39
+ puts a.generate_table(["Service", "username", "password"], 30)
40
+
41
+ #|============================================================================================|
42
+ #|Service |username |password |
43
+ #|============================================================================================|
44
+ #|ABCDE_Mail |example@example.com |abcdefg_abcdefg_abcdefg_abcdef|
45
+ #| | |g_abcdefg_abcdefg_abcdefg_abcd|
46
+ #| | |efg_abcdefg_abcdefg_ |
47
+ #|--------------------------------------------------------------------------------------------|
48
+ #|XYZ_TV |Taro Yamada |qwertyqwertyqwertyqwertyqwerty|
49
+ #| | |qwertyqwertyqwertyqwertyqwerty|
50
+ #| | | |
51
+ #|--------------------------------------------------------------------------------------------|
52
+ ```
26
53
 
27
54
  ## Contributing
28
55
 
29
- Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/simple_tabler.
56
+ Bug reports and pull requests are welcome on GitHub at https://github.com/Takahashi-Riki/simple_tabler.
30
57
 
31
58
  ## License
32
59
 
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module SimpleTabler
4
- VERSION = "0.1.0"
4
+ VERSION = "0.1.1"
5
5
  end
data/lib/simple_tabler.rb CHANGED
@@ -6,51 +6,58 @@ require_relative "table_from_array"
6
6
  class Array; include SimpleTabler; end
7
7
 
8
8
  module SimpleTabler
9
+ CORRECT_OBJECTS = [String, Numeric, Symbol, TrueClass, FalseClass]
10
+
11
+ def generate_table(column_names=nil, scale=20)
12
+ if !dimension_equal_two?
13
+ raise ArgumentError, "The depth of array should be two."
14
+ elsif !child_array_size_same?
15
+ raise ArgumentError, "All child arrays should have same amount of element."
16
+ elsif !element_type_correct?
17
+ raise ArgumentError, "All child arrays should have elements which is kind of allowed class.\nAllowed class is String, Numeric, Symbol, TrueClass, FalseClass"
18
+ elsif !column_names.nil? && !column_size_correct?(column_names)
19
+ raise ArgumentError, "All child arrays should have same amount of element with column names you passed as an argument."
20
+ end
21
+
22
+ table_generated_from_array = TableFromArray.new(self, column_names, scale)
23
+ return table_generated_from_array.content_of_table
24
+ end
25
+
26
+ # return true if dimension of array equal two.
27
+ # ex. [["hoge", "fuga"], ["hoge", "fuga"]]
28
+ # => true
29
+ # [["hoge", "fuga"], ["hoge", "fuga", ["hoge"]]]
30
+ # => false
9
31
  def dimension_equal_two?
10
32
  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
33
+ return false unless child_array.kind_of?(Array)
34
+ return false unless child_array == child_array.flatten
16
35
  end
17
36
  return true
18
37
  end
19
38
 
39
+ # return true if all of the child array has same length of item.
40
+ # ex. [["1", "2"], ["1", "2"]]
41
+ # => true
42
+ # [["1", "2"], ["1", "2", "3"]]
43
+ # => false
20
44
  def child_array_size_same?
21
45
  child_array_sizes = self.map{|child_array| child_array.size}
22
46
  child_array_sizes.uniq.size == 1
23
47
  end
24
48
 
49
+ # return true if all of array items are kind of CORRECT_OBJECTS.
25
50
  def element_type_correct?
26
- correct_object_types = [String, Numeric, Symbol, TrueClass, FalseClass]
27
51
  self.each do |child_array|
28
52
  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)
53
+ return false unless CORRECT_OBJECTS.any?{|object_type| element.kind_of?(object_type)}
31
54
  end
32
55
  end
33
56
  return true
34
57
  end
35
58
 
59
+ # return true if array size and column size is same.
36
60
  def column_size_correct?(column_names)
37
61
  self[0].size == column_names.size
38
62
  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
63
  end
@@ -1,17 +1,18 @@
1
1
  class TableFromArray
2
- attr_reader :content_of_table, :column_names
2
+ attr_reader :content_of_table
3
3
 
4
- def initialize(column_size, column_names, scale)
4
+ def initialize(array, column_names, scale)
5
5
  @content_of_table = ""
6
- @column_size = column_size
7
- @column_names = column_names
8
- @scale = scale
6
+ @column_size = array[0].size
7
+ @scale = scale
8
+ self.add_header(column_names)
9
+ self.add_content(array)
9
10
  end
10
11
 
11
- def add_header
12
- if @column_names
12
+ def add_header(column_names=nil)
13
+ if column_names
13
14
  add_separation("=")
14
- add_row(@column_names)
15
+ add_row(column_names)
15
16
  add_separation("=")
16
17
  else
17
18
  add_separation("-")
@@ -32,13 +33,13 @@ class TableFromArray
32
33
 
33
34
  def add_row(row_materials)
34
35
  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
36
+ number_of_lines = (max_length_of_array_element(row_materials).to_f / @scale).ceil
37
+ (0...number_of_lines).each do |i|
37
38
  line = "|"
38
- for row_material in row_materials do
39
+ row_materials.each do |row_material|
39
40
  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 + "|"
41
+ space_to_fill_the_blank = " " * (@scale - row_material_sliced_for_this_line.size)
42
+ line += row_material_sliced_for_this_line + space_to_fill_the_blank + "|"
42
43
  end
43
44
  content_of_row += line + "\n"
44
45
  end
@@ -46,7 +47,6 @@ class TableFromArray
46
47
  end
47
48
 
48
49
  def add_separation(mark)
49
- separation = "|" + mark * (@scale * @column_size + @column_size - 1) + "|" + "\n"
50
- @content_of_table += separation
50
+ @content_of_table += "|" + mark * (@scale * @column_size + @column_size - 1) + "|" + "\n"
51
51
  end
52
52
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: simple_tabler
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - mew3880
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-03-17 00:00:00.000000000 Z
11
+ date: 2022-07-29 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description:
14
14
  email: