simple_tabler 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +44 -17
- data/lib/simple_tabler/version.rb +1 -1
- data/lib/simple_tabler.rb +32 -25
- data/lib/table_from_array.rb +15 -15
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 673a1a57ecff0f6f4bf586b10cff347c3c40947c84685cf344a5fbe71ab7b13f
|
4
|
+
data.tar.gz: 4d10bc4df14395fea3b95050d10ba3c2e0af4896aeb60b3b1efbcf6cf0433142
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 52a7b43f4b9bd90845ee426d257a88638c1fa57feaa0b685e8b03460e146b83093d3298e62ae1e3fb075bb407e5c543ba05444a7aef143f84a0ea01dde065077
|
7
|
+
data.tar.gz: dcb3e76b5778a510ee78e11b7f1720c2055b075c2a9c81f065e47447aac8c57aa3bb0d02dbb880a01252701ff0bb468694af5c6fffc071f7a9bd5caf7868e9c9
|
data/README.md
CHANGED
@@ -1,32 +1,59 @@
|
|
1
|
-
#
|
1
|
+
# Simple Tabler
|
2
2
|
|
3
|
-
|
3
|
+
Simple Tabler helps you to make a table in Terminal
|
4
4
|
|
5
|
-
|
5
|
+
It can be used as a gem.(https://rubygems.org/gems/simple_tabler)
|
6
6
|
|
7
7
|
## Installation
|
8
8
|
|
9
|
-
|
9
|
+
Add simple_tabler to your Gemfile.
|
10
10
|
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
$ gem install simple_tabler
|
11
|
+
```Gemfile
|
12
|
+
gem 'simple_tabler', '~> 0.1.0'
|
13
|
+
```
|
16
14
|
|
17
15
|
## Usage
|
18
16
|
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
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/
|
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
|
|
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
|
-
|
12
|
-
|
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
|
-
|
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
|
data/lib/table_from_array.rb
CHANGED
@@ -1,17 +1,18 @@
|
|
1
1
|
class TableFromArray
|
2
|
-
attr_reader :content_of_table
|
2
|
+
attr_reader :content_of_table
|
3
3
|
|
4
|
-
def initialize(
|
4
|
+
def initialize(array, column_names, scale)
|
5
5
|
@content_of_table = ""
|
6
|
-
@column_size
|
7
|
-
@
|
8
|
-
|
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
|
12
|
+
def add_header(column_names=nil)
|
13
|
+
if column_names
|
13
14
|
add_separation("=")
|
14
|
-
add_row(
|
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
|
36
|
-
|
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
|
-
|
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
|
41
|
-
line
|
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
|
-
|
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.
|
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-
|
11
|
+
date: 2022-07-29 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description:
|
14
14
|
email:
|