tbpgr_utils 0.0.121 → 0.0.122

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
  SHA1:
3
- metadata.gz: def617e72b46baf4acea16f53a796d7b91a89d61
4
- data.tar.gz: 9b4383ade5e36e6b6bd9534d41ff30889772b3d3
3
+ metadata.gz: 97f6252d4a59debf56d6ab9faf41e797b7bebe74
4
+ data.tar.gz: 48b3c60ea9c550a3c6d3e4a8ec06a2e593928aae
5
5
  SHA512:
6
- metadata.gz: bd9600a5cac06e3bf7733b438e01e75bab9fee1fd2a74926889cbfd4096cea725d40b9bd2c109187624ab1ad6e898ee2a1f6be114abdbbb633910523550fc11b
7
- data.tar.gz: 077edd7c7f229bdb433d4e0df51a28ec434d9bb7bdb42f5f4ada56bfc9af9d0299dfc3f1549f377fadb0c0fce9b2bc3db9c6948a0765b4e76b3d0776fc3b0b13
6
+ metadata.gz: 10af20b7e67eda10e86f4cbf244c12fde0423b9d02aa0b917e2a8fd99c78458cdbc81906c74cc9e266bbf6064f02d820232465df57d4416f48fd2328d85c451a
7
+ data.tar.gz: a71b5a826bd606cbb3b7de47cfc5e184b4cf3fb64758fc08c288c19253d83af5be1c6954843bfac298c3716d0a8b99c446f03e705527bc2b347aeff274dd9dd1
data/README.md CHANGED
@@ -144,6 +144,7 @@ Or install it yourself as:
144
144
  |[TbpgrUtils String#escape_double_quote](#stringescape_double_quote) |escape double quote |
145
145
  |[TbpgrUtils String#hyphen_to_a](#stringhyphen_to_a) |hyphen-format string to array |
146
146
  |[TbpgrUtils String#is_meta_variable?](#stringis_meta_variable) |is meta variable. |
147
+ |[TbpgrUtils String#justify_char](#stringjustify_char) |justify pipe format char string |
147
148
  |[TbpgrUtils String#justify_table](#stringjustify_table) |justify pipe format table string |
148
149
  |[TbpgrUtils String#say](#stringsay) |say string |
149
150
  |[TbpgrUtils String#spacing](#stringspacing) |get spacing string |
@@ -3157,6 +3158,29 @@ require 'tbpgr_utils'
3157
3158
 
3158
3159
  [back to list](#list)
3159
3160
 
3161
+ ### String#justify_char
3162
+ ~~~ruby
3163
+ require 'tbpgr_utils'
3164
+
3165
+ str =<<-EOS
3166
+ print 'hoge' # => 'hoge'
3167
+ print 'hoge' * 2 # => 'hogehoge'
3168
+ print 'hoge' + 'hige' # => 'hogehige'
3169
+ EOS
3170
+
3171
+ str.justify_char('#')
3172
+ ~~~
3173
+
3174
+ output
3175
+
3176
+ ~~~
3177
+ print 'hoge' # => 'hoge'
3178
+ print 'hoge' * 2 # => 'hogehoge'
3179
+ print 'hoge' + 'hige' # => 'hogehige'
3180
+ ~~~
3181
+
3182
+ [back to list](#list)
3183
+
3160
3184
  ### String#justify_table
3161
3185
  ~~~ruby
3162
3186
  require 'tbpgr_utils'
@@ -3170,7 +3194,8 @@ EOS
3170
3194
  puts str.justify_table
3171
3195
  ~~~
3172
3196
 
3173
- output
3197
+ output
3198
+
3174
3199
  ~~~
3175
3200
  |* hogehogehoge|* hage|* hige |
3176
3201
  |test |tester|testest |
@@ -3567,6 +3592,7 @@ if you are Sublime Text2 user, you can use snippet for TbpgrUtils.
3567
3592
  https://github.com/tbpgr/tbpgr_utils_snippets
3568
3593
 
3569
3594
  ## History
3595
+ * version 0.0.122 : add String#justify_char
3570
3596
  * version 0.0.121 : add Array#average
3571
3597
  * version 0.0.120 : add Array#sum
3572
3598
  * version 0.0.119 : add Kernel#exchange
@@ -0,0 +1,80 @@
1
+ # encoding: utf-8
2
+ require 'open_classes/string/ascii1_other2_size'
3
+
4
+ # String
5
+ class String
6
+ # Justify string using separator
7
+ #
8
+ # before justify
9
+ #
10
+ # print 'hoge' # => 'hoge'
11
+ # print 'hoge' * 2 # => 'hogehoge'
12
+ # print 'hoge' + 'hige' # => 'hogehige'
13
+ #
14
+ # after justify
15
+ #
16
+ # print 'hoge' # => 'hoge'
17
+ # print 'hoge' * 2 # => 'hogehoge'
18
+ # print 'hoge' + 'hige' # => 'hogehige'
19
+ #
20
+ def justify_char(separator = '|', position = :left)
21
+ return self if empty?
22
+ return self unless include? separator
23
+ max_sizes = get_column_maxes(separator)
24
+ return self if max_sizes.nil?
25
+ justify_lines max_sizes, position, separator
26
+ end
27
+
28
+ private
29
+
30
+ def get_column_maxes(separator)
31
+ max_sizes = []
32
+ each_line do |line|
33
+ columns = get_columuns(line, separator)
34
+ max_sizes = get_column_max(columns, max_sizes)
35
+ end
36
+ max_sizes
37
+ end
38
+
39
+ def justify_lines(max_sizes, position, separator)
40
+ ret = []
41
+ each_line do |line|
42
+ columns = get_columuns(line, separator)
43
+ line_ret = []
44
+ columns.each_with_index do |column, cnt|
45
+ diff = column.ascii1_other2_size - column.size
46
+ line_ret << justified_column(column, max_sizes[cnt], diff, position)
47
+ end
48
+ ret << line_ret.join(separator).gsub(/ +$/m, '')
49
+ end
50
+ ret.join
51
+ end
52
+
53
+ def justified_column(column, max_size, diff, position)
54
+ pos = max_size - diff
55
+ case position
56
+ when :left
57
+ column.ljust(pos)
58
+ when :right
59
+ column.rjust(pos)
60
+ when :center
61
+ column.center(pos)
62
+ end
63
+ end
64
+
65
+ def get_columuns(line, separator)
66
+ line.split(separator)
67
+ end
68
+
69
+ def get_column_max(columns, max_sizes)
70
+ columns.each_with_index do |column, index|
71
+ current_size = column.ascii1_other2_size
72
+ if max_sizes[index].nil?
73
+ max_sizes << current_size
74
+ next
75
+ end
76
+ max_sizes[index] = current_size if current_size > max_sizes[index]
77
+ end
78
+ max_sizes
79
+ end
80
+ end
@@ -20,38 +20,38 @@ class String
20
20
  # |yusei |matsui |
21
21
  def justify_table(position = :left)
22
22
  return self if self.empty?
23
- max_sizes = get_column_maxes
23
+ max_sizes = get_column_maxes_table
24
24
  return self if max_sizes.nil?
25
- justify_lines max_sizes, position
25
+ justify_lines_table max_sizes, position
26
26
  end
27
27
 
28
28
  private
29
29
 
30
- def get_column_maxes
30
+ def get_column_maxes_table
31
31
  max_sizes = []
32
32
  each_line do |line|
33
33
  return nil unless table? line
34
- columns = get_columuns(line)
35
- max_sizes = get_column_max(columns, max_sizes)
34
+ columns = get_columuns_table(line)
35
+ max_sizes = get_column_max_table(columns, max_sizes)
36
36
  end
37
37
  max_sizes
38
38
  end
39
39
 
40
- def justify_lines(max_sizes, position)
40
+ def justify_lines_table(max_sizes, position)
41
41
  ret = []
42
42
  each_line do |line|
43
- columns = get_columuns(line)
43
+ columns = get_columuns_table(line)
44
44
  line_ret = []
45
45
  columns.each_with_index do |column, cnt|
46
46
  diff = column.ascii1_other2_size - column.size
47
- line_ret << justified_column(column, max_sizes[cnt], diff, position)
47
+ line_ret << justified_column_table(column, max_sizes[cnt], diff, position)
48
48
  end
49
49
  ret << "|#{line_ret.join('|')}|"
50
50
  end
51
51
  ret.join("\n") + "\n"
52
52
  end
53
53
 
54
- def justified_column(column, max_size, diff, position)
54
+ def justified_column_table(column, max_size, diff, position)
55
55
  pos = max_size - diff
56
56
  case position
57
57
  when :left
@@ -63,11 +63,11 @@ class String
63
63
  end
64
64
  end
65
65
 
66
- def get_columuns(line)
66
+ def get_columuns_table(line)
67
67
  line.split('|')[1..-2]
68
68
  end
69
69
 
70
- def get_column_max(columns, max_sizes)
70
+ def get_column_max_table(columns, max_sizes)
71
71
  columns.each_with_index do |column, index|
72
72
  current_size = column.ascii1_other2_size
73
73
  # current_size = column.size
@@ -8,6 +8,7 @@ require 'open_classes/string/escape_double_quote'
8
8
  require 'open_classes/string/escape_quote'
9
9
  require 'open_classes/string/hyphen_to_a'
10
10
  require 'open_classes/string/is_meta_variable'
11
+ require 'open_classes/string/justify_char'
11
12
  require 'open_classes/string/justify_table'
12
13
  require 'open_classes/string/say'
13
14
  require 'open_classes/string/spacing'
@@ -2,5 +2,5 @@
2
2
 
3
3
  # Tbpgr Utilities
4
4
  module TbpgrUtils
5
- VERSION = '0.0.121'
5
+ VERSION = '0.0.122'
6
6
  end
@@ -0,0 +1,98 @@
1
+ # encoding: utf-8
2
+ require 'spec_helper'
3
+ require 'tbpgr_utils'
4
+
5
+ describe String do
6
+
7
+ context :justify_table do
8
+ SAMPLE_STRING1 = <<-EOS
9
+ print 'hoge' # => 'hoge'
10
+ print 'hoge' * 2 # => 'hogehoge'
11
+ print 'hoge' + 'hige' # => 'hogehige'
12
+ EOS
13
+
14
+ SAMPLE_JUSTIFIED_STRING1 = <<-EOS
15
+ print 'hoge' # => 'hoge'
16
+ print 'hoge' * 2 # => 'hogehoge'
17
+ print 'hoge' + 'hige' # => 'hogehige'
18
+ EOS
19
+
20
+ SAMPLE_STRING2 = <<-EOS
21
+ print 'hoge' | => 'hoge'
22
+ print 'hoge' * 2 | => 'hogehoge'
23
+ print 'hoge' + 'hige' | => 'hogehige'
24
+ EOS
25
+
26
+ SAMPLE_JUSTIFIED_STRING2 = <<-EOS
27
+ print 'hoge' | => 'hoge'
28
+ print 'hoge' * 2 | => 'hogehoge'
29
+ print 'hoge' + 'hige' | => 'hogehige'
30
+ EOS
31
+
32
+ SAMPLE_STRING3 = <<-EOS
33
+ print 'hoge' // => 'hoge'
34
+ print 'hoge' * 2 // => 'hogehoge'
35
+ print 'hoge' + 'hige' // => 'hogehige'
36
+ EOS
37
+
38
+ SAMPLE_JUSTIFIED_STRING3 = <<-EOS
39
+ print 'hoge' // => 'hoge'
40
+ print 'hoge' * 2 // => 'hogehoge'
41
+ print 'hoge' + 'hige' // => 'hogehige'
42
+ EOS
43
+
44
+ cases = [
45
+ {
46
+ case_no: 1,
47
+ case_title: 'sharp justify case',
48
+ input: SAMPLE_STRING1,
49
+ separator: '#',
50
+ expected: SAMPLE_JUSTIFIED_STRING1,
51
+ },
52
+ {
53
+ case_no: 2,
54
+ case_title: 'use default separator(pipe) justify case',
55
+ input: SAMPLE_STRING2,
56
+ expected: SAMPLE_JUSTIFIED_STRING2,
57
+ },
58
+ {
59
+ case_no: 3,
60
+ case_title: 'double stash justify case',
61
+ input: SAMPLE_STRING3,
62
+ separator: '//',
63
+ expected: SAMPLE_JUSTIFIED_STRING3,
64
+ },
65
+ ]
66
+
67
+ cases.each do |c|
68
+ it "|case_no=#{c[:case_no]}|case_title=#{c[:case_title]}" do
69
+ begin
70
+ case_before c
71
+
72
+ # -- given --
73
+ # nothing
74
+
75
+ # -- when --
76
+ if (c[:separator])
77
+ actual = c[:input].justify_char c[:separator]
78
+ else
79
+ actual = c[:input].justify_char
80
+ end
81
+
82
+ # -- then --
83
+ expect(actual).to eq(c[:expected])
84
+ ensure
85
+ case_after c
86
+ end
87
+ end
88
+
89
+ def case_before(c)
90
+ # implement each case before
91
+ end
92
+
93
+ def case_after(c)
94
+ # implement each case after
95
+ end
96
+ end
97
+ end
98
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tbpgr_utils
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.121
4
+ version: 0.0.122
5
5
  platform: ruby
6
6
  authors:
7
7
  - tbpgr
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-05-08 00:00:00.000000000 Z
11
+ date: 2014-05-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -216,6 +216,7 @@ files:
216
216
  - lib/open_classes/string/heading_helper.rb
217
217
  - lib/open_classes/string/hyphen_to_a.rb
218
218
  - lib/open_classes/string/is_meta_variable.rb
219
+ - lib/open_classes/string/justify_char.rb
219
220
  - lib/open_classes/string/justify_table.rb
220
221
  - lib/open_classes/string/say.rb
221
222
  - lib/open_classes/string/spacing.rb
@@ -345,6 +346,7 @@ files:
345
346
  - spec/open_classes/string/escape_quote_spec.rb
346
347
  - spec/open_classes/string/hyphen_to_a_spec.rb
347
348
  - spec/open_classes/string/is_meta_variable_spec.rb
349
+ - spec/open_classes/string/justify_char_spec.rb
348
350
  - spec/open_classes/string/justify_table_spec.rb
349
351
  - spec/open_classes/string/say_spec.rb
350
352
  - spec/open_classes/string/spacing_spec.rb
@@ -497,6 +499,7 @@ test_files:
497
499
  - spec/open_classes/string/escape_quote_spec.rb
498
500
  - spec/open_classes/string/hyphen_to_a_spec.rb
499
501
  - spec/open_classes/string/is_meta_variable_spec.rb
502
+ - spec/open_classes/string/justify_char_spec.rb
500
503
  - spec/open_classes/string/justify_table_spec.rb
501
504
  - spec/open_classes/string/say_spec.rb
502
505
  - spec/open_classes/string/spacing_spec.rb