tbpgr_utils 0.0.38 → 0.0.39

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.
data/README.md CHANGED
@@ -69,6 +69,7 @@ Or install it yourself as:
69
69
  |[TbpgrUtils Object#my_methods](#objectmy_methods) |return public/protected/private self define methods |
70
70
  |[TbpgrUtils Object#to_bool](#objectto_bool) |syntax sugar of !!. convert [false, nil] => fasel, other => true. |
71
71
  |[TbpgrUtils String#justify_table](#stringjustify_table) |justify pipe format table string |
72
+ |[TbpgrUtils String#surround](#stringsurround) |surround string |
72
73
  |[Templatable module](#templatable) |get result from template + placeholder |
73
74
  |[TemplateMethodable module](#templatemethodable) |for Template Method Pattern |
74
75
 
@@ -1364,6 +1365,55 @@ output
1364
1365
 
1365
1366
  [back to list](#list)
1366
1367
 
1368
+ ### String#surround
1369
+ single line, no option case
1370
+
1371
+ ~~~ruby
1372
+ require 'tbpgr_utils'
1373
+ 'hoge'.surround
1374
+ ~~~
1375
+
1376
+ result
1377
+
1378
+ ~~~
1379
+ ------
1380
+ |hoge|
1381
+ ------
1382
+ ~~~
1383
+
1384
+ multi line, no option case
1385
+
1386
+ ~~~ruby
1387
+ require 'tbpgr_utils'
1388
+ "hoge\na".surround
1389
+ ~~~
1390
+
1391
+ result
1392
+
1393
+ ~~~
1394
+ ------
1395
+ |hoge|
1396
+ |a |
1397
+ ------
1398
+ ~~~
1399
+
1400
+ single line, both option case
1401
+
1402
+ ~~~ruby
1403
+ require 'tbpgr_utils'
1404
+ 'hoge'.surround top_bottom: '=', side: '!'
1405
+ ~~~
1406
+
1407
+ result
1408
+
1409
+ ~~~
1410
+ ======
1411
+ !hoge!
1412
+ ======
1413
+ ~~~
1414
+
1415
+ [back to list](#list)
1416
+
1367
1417
  ### Templatable
1368
1418
  * include Templatable
1369
1419
  * set template by here-document
@@ -1464,6 +1514,7 @@ if you are Sublime Text2 user, you can use snippet for TbpgrUtils.
1464
1514
  https://github.com/tbpgr/tbpgr_utils_snippets
1465
1515
 
1466
1516
  ## History
1517
+ * version 0.0.39 : add String#surround.
1467
1518
  * version 0.0.38 : add Array#together_shuffle(alias tshuffle).
1468
1519
  * version 0.0.37 : add Array#together_sample(alias tsample).
1469
1520
  * version 0.0.36 : add Array#together_reverse,Array#together_reverse!(alias treverse, alias treverse!).
@@ -0,0 +1,71 @@
1
+ # encoding: utf-8
2
+
3
+ # String
4
+ class String
5
+ # Justify pipe using table format
6
+ #
7
+ # before justify
8
+ #
9
+ # |* first name|* family name|
10
+ # |eiichiro|oda|
11
+ # |akira|toriyama|
12
+ # |yusei|matsui|
13
+ #
14
+ # after justify
15
+ #
16
+ # |* first name|* family name|
17
+ # |eiichiro |oda |
18
+ # |akira |toriyama |
19
+ # |yusei |matsui |
20
+ def justify_table
21
+ return self if self.empty?
22
+ max_sizes = get_column_maxes
23
+ return self if max_sizes.nil?
24
+ justify_lines max_sizes
25
+ end
26
+
27
+ private
28
+
29
+ def get_column_maxes
30
+ max_sizes = []
31
+ each_line do |line|
32
+ return nil unless table? line
33
+ columns = get_columuns(line)
34
+ max_sizes = get_column_max(columns, max_sizes)
35
+ end
36
+ max_sizes
37
+ end
38
+
39
+ def justify_lines(max_sizes)
40
+ ret = []
41
+ each_line do |line|
42
+ columns = get_columuns(line)
43
+ line_ret = []
44
+ columns.each_with_index do |column, cnt|
45
+ line_ret << column.ljust(max_sizes[cnt])
46
+ end
47
+ ret << "|#{line_ret.join('|')}|"
48
+ end
49
+ ret.join("\n") + "\n"
50
+ end
51
+
52
+ def get_columuns(line)
53
+ line.split('|')[1..-2]
54
+ end
55
+
56
+ def get_column_max(columns, max_sizes)
57
+ columns.each_with_index do |column, index|
58
+ current_size = column.size
59
+ if max_sizes[index].nil?
60
+ max_sizes << current_size
61
+ next
62
+ end
63
+ max_sizes[index] = current_size if current_size > max_sizes[index]
64
+ end
65
+ max_sizes
66
+ end
67
+
68
+ def table?(text)
69
+ text.count('|') > 0
70
+ end
71
+ end
@@ -0,0 +1,72 @@
1
+ # encoding: utf-8
2
+
3
+ # String
4
+ class String
5
+ # surround string.
6
+ #
7
+ # ==== Options
8
+ #
9
+ # * <tt>:top_bottom</tt> - set top and bottom charactor
10
+ # * <tt>:side</tt> - set right and left charactor
11
+ #
12
+ # ==== Examples
13
+ #
14
+ # single line, no option case
15
+ #
16
+ # 'hoge'.surround
17
+ #
18
+ # result
19
+ #
20
+ # ------
21
+ # |hoge|
22
+ # ------
23
+ #
24
+ # multi line, no option case
25
+ #
26
+ # "hoge\na".surround
27
+ #
28
+ # result
29
+ #
30
+ # ------
31
+ # |hoge|
32
+ # |a |
33
+ # ------
34
+ #
35
+ # single line, both option case
36
+ #
37
+ # 'hoge'.surround top_bottom: '=', side: '!'
38
+ #
39
+ # result
40
+ #
41
+ # ======
42
+ # !hoge!
43
+ # ======
44
+ #
45
+ def surround(options = { top_bottom: '-', side: '|' })
46
+ top_bottom = init_top_bottom(options)
47
+ side = init_side(options)
48
+ inner_width = line_max
49
+ top_bottom = top_bottoms(top_bottom, inner_width)
50
+ ret = *each_line.reduce(["#{top_bottom}"]) { |ret, line|ret << "#{side}#{line.chomp.ljust(inner_width)}#{side}" }
51
+ ret.push("#{top_bottom}").join("\n")
52
+ end
53
+
54
+ private
55
+
56
+ def init_top_bottom(options)
57
+ options[:top_bottom].nil? ? '-' : options[:top_bottom]
58
+ end
59
+
60
+ def init_side(options)
61
+ options[:side].nil? ? '|' : options[:side]
62
+ end
63
+
64
+ def top_bottoms(top_bottom, inner_width)
65
+ top_bottom * (inner_width + 2)
66
+ end
67
+
68
+ def line_max
69
+ return 0 if self.empty?
70
+ each_line.max_by { |v|v.size }.chomp.size
71
+ end
72
+ end
@@ -1,71 +1,3 @@
1
1
  # encoding: utf-8
2
-
3
- # String
4
- class String
5
- # Justify pipe using table format
6
- #
7
- # before justify
8
- #
9
- # |* first name|* family name|
10
- # |eiichiro|oda|
11
- # |akira|toriyama|
12
- # |yusei|matsui|
13
- #
14
- # after justify
15
- #
16
- # |* first name|* family name|
17
- # |eiichiro |oda |
18
- # |akira |toriyama |
19
- # |yusei |matsui |
20
- def justify_table
21
- return self if self.empty?
22
- max_sizes = get_column_maxes
23
- return self if max_sizes.nil?
24
- justify_lines max_sizes
25
- end
26
-
27
- private
28
-
29
- def get_column_maxes
30
- max_sizes = []
31
- each_line do |line|
32
- return nil unless table? line
33
- columns = get_columuns(line)
34
- max_sizes = get_column_max(columns, max_sizes)
35
- end
36
- max_sizes
37
- end
38
-
39
- def justify_lines(max_sizes)
40
- ret = []
41
- each_line do |line|
42
- columns = get_columuns(line)
43
- line_ret = []
44
- columns.each_with_index do |column, cnt|
45
- line_ret << column.ljust(max_sizes[cnt])
46
- end
47
- ret << "|#{line_ret.join('|')}|"
48
- end
49
- ret.join("\n") + "\n"
50
- end
51
-
52
- def get_columuns(line)
53
- line.split('|')[1..-2]
54
- end
55
-
56
- def get_column_max(columns, max_sizes)
57
- columns.each_with_index do |column, index|
58
- current_size = column.size
59
- if max_sizes[index].nil?
60
- max_sizes << current_size
61
- next
62
- end
63
- max_sizes[index] = current_size if current_size > max_sizes[index]
64
- end
65
- max_sizes
66
- end
67
-
68
- def table?(text)
69
- text.count('|') > 0
70
- end
71
- end
2
+ require 'open_classes/string/justify_table'
3
+ require 'open_classes/string/surround'
@@ -2,5 +2,5 @@
2
2
 
3
3
  # Tbpgr Utilities
4
4
  module TbpgrUtils
5
- VERSION = '0.0.38'
5
+ VERSION = '0.0.39'
6
6
  end
data/rubocop-todo.yml CHANGED
@@ -5,9 +5,6 @@
5
5
  AccessModifierIndentation:
6
6
  Enabled: false
7
7
 
8
- ClassLength:
9
- Enabled: false
10
-
11
8
  Documentation:
12
9
  Enabled: false
13
10
 
@@ -41,7 +38,7 @@ ParenthesesAroundCondition:
41
38
  RaiseArgs:
42
39
  Enabled: false
43
40
 
44
- Semicolon:
41
+ ReduceArguments:
45
42
  Enabled: false
46
43
 
47
44
  ShadowingOuterLocalVariable:
@@ -0,0 +1,78 @@
1
+ # encoding: utf-8
2
+ require 'spec_helper'
3
+ require 'tbpgr_utils'
4
+
5
+ describe String do
6
+ context :surround do
7
+ cases = [
8
+ {
9
+ case_no: 1,
10
+ case_title: 'single line, no option case',
11
+ input: 'hoge',
12
+ expected: <<-EOS
13
+ ------
14
+ |hoge|
15
+ ------
16
+ EOS
17
+ },
18
+ {
19
+ case_no: 2,
20
+ case_title: 'multi line, no option case',
21
+ input: "hoge\na",
22
+ expected: <<-EOS
23
+ ------
24
+ |hoge|
25
+ |a |
26
+ ------
27
+ EOS
28
+ },
29
+ {
30
+ case_no: 3,
31
+ case_title: 'empty, no option case',
32
+ input: '',
33
+ expected: <<-EOS
34
+ --
35
+ --
36
+ EOS
37
+ },
38
+ {
39
+ case_no: 4,
40
+ case_title: 'single line, both option case',
41
+ input: 'hoge',
42
+ options: { top_bottom: '=', side: '!' },
43
+ expected: <<-EOS
44
+ ======
45
+ !hoge!
46
+ ======
47
+ EOS
48
+ },
49
+ ]
50
+
51
+ cases.each do |c|
52
+ it "|case_no=#{c[:case_no]}|case_title=#{c[:case_title]}" do
53
+ begin
54
+ case_before c
55
+
56
+ # -- given --
57
+ # nothing
58
+
59
+ # -- when --
60
+ actual = c[:options] ? c[:input].surround(c[:options]) : c[:input].surround
61
+
62
+ # -- then --
63
+ expect(actual).to eq(c[:expected].chop)
64
+ ensure
65
+ case_after c
66
+ end
67
+ end
68
+
69
+ def case_before(c)
70
+ # implement each case before
71
+ end
72
+
73
+ def case_after(c)
74
+ # implement each case after
75
+ end
76
+ end
77
+ end
78
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tbpgr_utils
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.38
4
+ version: 0.0.39
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2014-02-14 00:00:00.000000000 Z
12
+ date: 2014-02-15 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activesupport
16
- requirement: &26179104 !ruby/object:Gem::Requirement
16
+ requirement: &27926328 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ~>
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: 4.0.1
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *26179104
24
+ version_requirements: *27926328
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: bundler
27
- requirement: &26178816 !ruby/object:Gem::Requirement
27
+ requirement: &27926040 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ~>
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: '1.3'
33
33
  type: :development
34
34
  prerelease: false
35
- version_requirements: *26178816
35
+ version_requirements: *27926040
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: rake
38
- requirement: &26178588 !ruby/object:Gem::Requirement
38
+ requirement: &27925812 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ! '>='
@@ -43,10 +43,10 @@ dependencies:
43
43
  version: '0'
44
44
  type: :development
45
45
  prerelease: false
46
- version_requirements: *26178588
46
+ version_requirements: *27925812
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: rspec
49
- requirement: &26178264 !ruby/object:Gem::Requirement
49
+ requirement: &27925488 !ruby/object:Gem::Requirement
50
50
  none: false
51
51
  requirements:
52
52
  - - ~>
@@ -54,10 +54,10 @@ dependencies:
54
54
  version: 2.14.1
55
55
  type: :development
56
56
  prerelease: false
57
- version_requirements: *26178264
57
+ version_requirements: *27925488
58
58
  - !ruby/object:Gem::Dependency
59
59
  name: simplecov
60
- requirement: &26177964 !ruby/object:Gem::Requirement
60
+ requirement: &27925188 !ruby/object:Gem::Requirement
61
61
  none: false
62
62
  requirements:
63
63
  - - ~>
@@ -65,7 +65,7 @@ dependencies:
65
65
  version: 0.8.2
66
66
  type: :development
67
67
  prerelease: false
68
- version_requirements: *26177964
68
+ version_requirements: *27925188
69
69
  description: Utilities
70
70
  email:
71
71
  - tbpgr@tbpgr.jp
@@ -116,6 +116,8 @@ files:
116
116
  - lib/open_classes/module.rb
117
117
  - lib/open_classes/object.rb
118
118
  - lib/open_classes/string.rb
119
+ - lib/open_classes/string/justify_table.rb
120
+ - lib/open_classes/string/surround.rb
119
121
  - lib/tbpgr_utils.rb
120
122
  - lib/tbpgr_utils/version.rb
121
123
  - lib/templatable.rb
@@ -154,7 +156,8 @@ files:
154
156
  - spec/open_classes/kernel_spec.rb
155
157
  - spec/open_classes/module_spec.rb
156
158
  - spec/open_classes/object_spec.rb
157
- - spec/open_classes/string_spec.rb
159
+ - spec/open_classes/string/justify_table_spec.rb
160
+ - spec/open_classes/string/surround_spec.rb
158
161
  - spec/spec_helper.rb
159
162
  - spec/templatable_spec.rb
160
163
  - spec/template_methodable_spec.rb
@@ -217,7 +220,8 @@ test_files:
217
220
  - spec/open_classes/kernel_spec.rb
218
221
  - spec/open_classes/module_spec.rb
219
222
  - spec/open_classes/object_spec.rb
220
- - spec/open_classes/string_spec.rb
223
+ - spec/open_classes/string/justify_table_spec.rb
224
+ - spec/open_classes/string/surround_spec.rb
221
225
  - spec/spec_helper.rb
222
226
  - spec/templatable_spec.rb
223
227
  - spec/template_methodable_spec.rb