tbpgr_utils 0.0.40 → 0.0.41

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -67,6 +67,8 @@ Or install it yourself as:
67
67
  |[TbpgrUtils Module.alias_methods](#modulealias_methods) |create alias methods |
68
68
  |[TbpgrUtils Object#any_of?](#objectany_of) |if self match any one of items, return true |
69
69
  |[TbpgrUtils Object#boolean?](#objectboolean) |data type check for boolean |
70
+ |[TbpgrUtils Object#guard](#objectguard) |data type check for guard |
71
+ |[TbpgrUtils Object#unless_guard](#objectunless_guard) |data type check for unless_guard |
70
72
  |[TbpgrUtils Object#my_methods](#objectmy_methods) |return public/protected/private self define methods |
71
73
  |[TbpgrUtils Object#to_bool](#objectto_bool) |syntax sugar of !!. convert [false, nil] => fasel, other => true. |
72
74
  |[TbpgrUtils String#justify_table](#stringjustify_table) |justify pipe format table string |
@@ -1344,6 +1346,60 @@ p "true".boolean? # =>false
1344
1346
 
1345
1347
  [back to list](#list)
1346
1348
 
1349
+ ### Object#guard
1350
+ guard return case
1351
+
1352
+ ~~~ruby
1353
+ def hoge(msg)
1354
+ guard(msg) {return "guard"}
1355
+ "not guard"
1356
+ end
1357
+
1358
+ hoge true # => "guard"
1359
+ hoge false # => "not guard"
1360
+ ~~~
1361
+
1362
+ guard fail case
1363
+
1364
+ ~~~ruby
1365
+ def hoge(msg)
1366
+ guard(msg) {fail ArgumentError, 'error!!'}
1367
+ "not guard"
1368
+ end
1369
+
1370
+ hoge true # => raise ArgumentError. message = error!!
1371
+ hoge false # => "not guard"
1372
+ ~~~
1373
+
1374
+ [back to list](#list)
1375
+
1376
+ ### Object#unless_guard
1377
+ unless_guard return case
1378
+
1379
+ ~~~ruby
1380
+ def hoge(msg)
1381
+ unless_guard(msg) {return "unless_guard"}
1382
+ "not unless_guard"
1383
+ end
1384
+
1385
+ hoge false # => "unless_guard"
1386
+ hoge true # => "not unless_guard"
1387
+ ~~~
1388
+
1389
+ unless_guard fail case
1390
+
1391
+ ~~~ruby
1392
+ def hoge(msg)
1393
+ unless_guard(msg) {fail ArgumentError, 'error!!'}
1394
+ "not unless_guard"
1395
+ end
1396
+
1397
+ hoge false # => raise ArgumentError. message = error!!
1398
+ hoge true # => "not unless_guard"
1399
+ ~~~
1400
+
1401
+ [back to list](#list)
1402
+
1347
1403
  ### Object#my_methods
1348
1404
  ~~~ruby
1349
1405
  require 'tbpgr_utils'
@@ -1551,6 +1607,7 @@ if you are Sublime Text2 user, you can use snippet for TbpgrUtils.
1551
1607
  https://github.com/tbpgr/tbpgr_utils_snippets
1552
1608
 
1553
1609
  ## History
1610
+ * version 0.0.41 : add Object#guard, unless_guard
1554
1611
  * version 0.0.40 : add Kernel#aa_ancestors.
1555
1612
  * version 0.0.39 : add String#surround.
1556
1613
  * version 0.0.38 : add Array#together_shuffle(alias tshuffle).
@@ -1,70 +1,70 @@
1
- # encoding: utf-8
2
- require 'open_classes/string'
3
-
4
- # Kernel
5
- module Kernel
6
- # Ascii Art Ancestors
7
- #
8
- # === Examples
9
- #
10
- # define class
11
- #
12
- # class BaseHogeForAncestors;end
13
- # class HogeForAncestors < BaseHogeForAncestors;end
14
- #
15
- # execute aa_ancestors
16
- #
17
- # puts HogeForAncestors.aa_ancestors
18
- #
19
- # result is ...
20
- #
21
- # ----------------------
22
- # | BasicObject |
23
- # ----------------------
24
- # |
25
- # ----------------------
26
- # | Kernel |
27
- # ----------------------
28
- # |
29
- # ----------------------
30
- # | Object |
31
- # ----------------------
32
- # |
33
- # ----------------------
34
- # |BaseHogeForAncestors|
35
- # ----------------------
36
- # |
37
- # ----------------------
38
- # | HogeForAncestors |
39
- # ----------------------
40
- def aa_ancestors
41
- max_class = max_size_class
42
- max = max_class.to_s.size
43
- ca = centered_ancestors(max)
44
- ba = surrounded_ancestors(ca)
45
- connected_box_ancestors(ba, max)
46
- end
47
-
48
- private
49
-
50
- def max_size_class
51
- ancestors.max_by {|v|v.to_s.size}
52
- end
53
-
54
- def centered_ancestors(max)
55
- ancestors.reverse.map {|v|v.to_s.center max}
56
- end
57
-
58
- def surrounded_ancestors(ca)
59
- ca.reduce([]) {|ret, k|ret << k.surround}
60
- end
61
-
62
- def connected_box_ancestors(ba, max)
63
- ba.join(ancestors_connector(max))
64
- end
65
-
66
- def ancestors_connector(max)
67
- connector = "|".rjust((max + 2)/2)
68
- "\n#{connector}\n"
69
- end
70
- end
1
+ # encoding: utf-8
2
+ require 'open_classes/string'
3
+
4
+ # Kernel
5
+ module Kernel
6
+ # Ascii Art Ancestors
7
+ #
8
+ # === Examples
9
+ #
10
+ # define class
11
+ #
12
+ # class BaseHogeForAncestors;end
13
+ # class HogeForAncestors < BaseHogeForAncestors;end
14
+ #
15
+ # execute aa_ancestors
16
+ #
17
+ # puts HogeForAncestors.aa_ancestors
18
+ #
19
+ # result is ...
20
+ #
21
+ # ----------------------
22
+ # | BasicObject |
23
+ # ----------------------
24
+ # |
25
+ # ----------------------
26
+ # | Kernel |
27
+ # ----------------------
28
+ # |
29
+ # ----------------------
30
+ # | Object |
31
+ # ----------------------
32
+ # |
33
+ # ----------------------
34
+ # |BaseHogeForAncestors|
35
+ # ----------------------
36
+ # |
37
+ # ----------------------
38
+ # | HogeForAncestors |
39
+ # ----------------------
40
+ def aa_ancestors
41
+ max_class = max_size_class
42
+ max = max_class.to_s.size
43
+ ca = centered_ancestors(max)
44
+ ba = surrounded_ancestors(ca)
45
+ connected_box_ancestors(ba, max)
46
+ end
47
+
48
+ private
49
+
50
+ def max_size_class
51
+ ancestors.max_by { |v|v.to_s.size }
52
+ end
53
+
54
+ def centered_ancestors(max)
55
+ ancestors.reverse.map { |v|v.to_s.center max }
56
+ end
57
+
58
+ def surrounded_ancestors(ca)
59
+ ca.reduce([]) { |ret, k|ret << k.surround }
60
+ end
61
+
62
+ def connected_box_ancestors(ba, max)
63
+ ba.join(ancestors_connector(max))
64
+ end
65
+
66
+ def ancestors_connector(max)
67
+ connector = '|'.rjust((max + 2) / 2)
68
+ "\n#{connector}\n"
69
+ end
70
+ end
@@ -0,0 +1,14 @@
1
+ # encoding: utf-8
2
+
3
+ # Object
4
+ class Object
5
+ # If self match any one of args, return true.
6
+ #
7
+ # "hoge".any_of? %w{hoge hige} # => true
8
+ # "hige".any_of? %w{hoge hige} # => true
9
+ # "hege".any_of? %w{hoge hige} # => false
10
+ def any_of?(*args)
11
+ args.each { |value|return true if self == value }
12
+ false
13
+ end
14
+ end
@@ -0,0 +1,16 @@
1
+ # encoding: utf-8
2
+
3
+ # Object
4
+ class Object
5
+ # Check boolean type
6
+ #
7
+ # boolean? true # => true
8
+ # boolean? false # => true
9
+ # boolean? nil # => false
10
+ # boolean? 'true' # => false
11
+ # boolean? 'false' # => false
12
+ # boolean? '' # => false
13
+ def boolean?
14
+ self.is_a?(TrueClass) || self.is_a?(FalseClass)
15
+ end
16
+ end
@@ -0,0 +1,66 @@
1
+ # encoding: utf-8
2
+
3
+ # Object
4
+ class Object
5
+ # guard condition
6
+ #
7
+ # === Param
8
+ #
9
+ # * <tt>:condition</tt> - guard condition
10
+ #
11
+ # === Example
12
+ #
13
+ # guard return case
14
+ #
15
+ # def hoge(msg)
16
+ # guard(msg) {return "guard"}
17
+ # "not guard"
18
+ # end
19
+ #
20
+ # hoge true # => "guard"
21
+ # hoge false # => "not guard"
22
+ #
23
+ # guard fail case
24
+ #
25
+ # def hoge(msg)
26
+ # guard(msg) {fail ArgumentError, 'error!!'}
27
+ # "not guard"
28
+ # end
29
+ #
30
+ # hoge true # => raise ArgumentError. message = error!!
31
+ # hoge false # => "not guard"
32
+ def guard(condition)
33
+ yield if condition
34
+ end
35
+
36
+ # unless_guard condition
37
+ #
38
+ # === Param
39
+ #
40
+ # * <tt>:condition</tt> - guard condition
41
+ #
42
+ # === Example
43
+ #
44
+ # unless_guard return case
45
+ #
46
+ # def hoge(msg)
47
+ # unless_guard(msg) {return "unless_guard"}
48
+ # "not unless_guard"
49
+ # end
50
+ #
51
+ # hoge false # => "unless_guard"
52
+ # hoge true # => "not unless_guard"
53
+ #
54
+ # unless_guard fail case
55
+ #
56
+ # def hoge(msg)
57
+ # unless_guard(msg) {fail ArgumentError, 'error!!'}
58
+ # "not unless_guard"
59
+ # end
60
+ #
61
+ # hoge false # => raise ArgumentError. message = error!!
62
+ # hoge true # => "not unless_guard"
63
+ def unless_guard(condition)
64
+ yield unless condition
65
+ end
66
+ end
@@ -0,0 +1,29 @@
1
+ # encoding: utf-8
2
+
3
+ # Object
4
+ class Object
5
+ # Get self define methods.
6
+ #
7
+ # class SampleClass < String
8
+ # def public_hello
9
+ # "public hello"
10
+ # end
11
+ #
12
+ # protected
13
+ #
14
+ # def protected_hello
15
+ # "protected hello"
16
+ # end
17
+ #
18
+ # private
19
+ #
20
+ # def private_hello
21
+ # "private hello"
22
+ # end
23
+ # end
24
+ #
25
+ # SampleClass.new.my_methods # => [:public_hello, :protected_hello, :private_hello]
26
+ def my_methods
27
+ public_methods(false) + protected_methods(false) + private_methods(false)
28
+ end
29
+ end
@@ -0,0 +1,18 @@
1
+ # encoding: utf-8
2
+
3
+ # Object
4
+ class Object
5
+ # you get bool value
6
+ #
7
+ # true.to_bool # => true
8
+ # false.to_bool # => false
9
+ # 0.to_bool # => true
10
+ # 1.to_bool # => true
11
+ # ''.to_bool # => true
12
+ # 'true'.to_bool # => true
13
+ # 'false'.to_bool # => true
14
+ # nil.to_bool # => false
15
+ def to_bool
16
+ !!self
17
+ end
18
+ end
@@ -1,65 +1,7 @@
1
1
  # encoding: utf-8
2
2
 
3
- # Object
4
- class Object
5
- # Check boolean type
6
- #
7
- # boolean? true # => true
8
- # boolean? false # => true
9
- # boolean? nil # => false
10
- # boolean? 'true' # => false
11
- # boolean? 'false' # => false
12
- # boolean? '' # => false
13
- def boolean?
14
- self.is_a?(TrueClass) || self.is_a?(FalseClass)
15
- end
16
-
17
- # Get self define methods.
18
- #
19
- # class SampleClass < String
20
- # def public_hello
21
- # "public hello"
22
- # end
23
- #
24
- # protected
25
- #
26
- # def protected_hello
27
- # "protected hello"
28
- # end
29
- #
30
- # private
31
- #
32
- # def private_hello
33
- # "private hello"
34
- # end
35
- # end
36
- #
37
- # SampleClass.new.my_methods # => [:public_hello, :protected_hello, :private_hello]
38
- def my_methods
39
- public_methods(false) + protected_methods(false) + private_methods(false)
40
- end
41
-
42
- # If self match any one of args, return true.
43
- #
44
- # "hoge".any_of? %w{hoge hige} # => true
45
- # "hige".any_of? %w{hoge hige} # => true
46
- # "hege".any_of? %w{hoge hige} # => false
47
- def any_of?(*args)
48
- args.each { |value|return true if self == value }
49
- false
50
- end
51
-
52
- # you get bool value
53
- #
54
- # true.to_bool # => true
55
- # false.to_bool # => false
56
- # 0.to_bool # => true
57
- # 1.to_bool # => true
58
- # ''.to_bool # => true
59
- # 'true'.to_bool # => true
60
- # 'false'.to_bool # => true
61
- # nil.to_bool # => false
62
- def to_bool
63
- !!self
64
- end
65
- end
3
+ require 'open_classes/object/boolean'
4
+ require 'open_classes/object/to_bool'
5
+ require 'open_classes/object/any_of'
6
+ require 'open_classes/object/my_methods'
7
+ require 'open_classes/object/guard'
@@ -2,5 +2,5 @@
2
2
 
3
3
  # Tbpgr Utilities
4
4
  module TbpgrUtils
5
- VERSION = '0.0.40'
5
+ VERSION = '0.0.41'
6
6
  end
@@ -4,8 +4,8 @@ require 'open_classes/kernel'
4
4
 
5
5
  describe Kernel do
6
6
  context :aa_ancestors do
7
- class BaseHogeForAncestors;end
8
- class HogeForAncestors < BaseHogeForAncestors;end
7
+ class BaseHogeForAncestors; end
8
+ class HogeForAncestors < BaseHogeForAncestors; end
9
9
 
10
10
  cases = [
11
11
  {
@@ -0,0 +1,72 @@
1
+ # encoding: utf-8
2
+ require 'spec_helper'
3
+ require 'tbpgr_utils'
4
+
5
+ describe Object do
6
+ context :any_of? do
7
+ cases = [
8
+ {
9
+ case_no: 1,
10
+ case_title: 'true case',
11
+ input: 'hoge',
12
+ check: %w{hoge hige},
13
+ expected: true,
14
+ },
15
+ {
16
+ case_no: 2,
17
+ case_title: 'true case',
18
+ input: 'hige',
19
+ check: %w{hoge hige},
20
+ expected: true,
21
+ },
22
+ {
23
+ case_no: 3,
24
+ case_title: 'false case',
25
+ input: 'hege',
26
+ check: %w{hoge hige},
27
+ expected: false,
28
+ },
29
+ {
30
+ case_no: 4,
31
+ case_title: 'number true case',
32
+ input: 1,
33
+ check: [1, 2, 3, 4, 5],
34
+ expected: true,
35
+ },
36
+ {
37
+ case_no: 5,
38
+ case_title: 'number true case',
39
+ input: 6,
40
+ check: [1, 2, 3, 4, 5],
41
+ expected: false,
42
+ },
43
+ ]
44
+
45
+ cases.each do |c|
46
+ it "|case_no=#{c[:case_no]}|case_title=#{c[:case_title]}" do
47
+ begin
48
+ case_before c
49
+
50
+ # -- given --
51
+ # nothing
52
+
53
+ # -- when --
54
+ actual = c[:input].any_of?(*c[:check])
55
+
56
+ # -- then --
57
+ expect(actual).to eq(c[:expected])
58
+ ensure
59
+ case_after c
60
+ end
61
+ end
62
+
63
+ def case_before(c)
64
+ # implement each case before
65
+ end
66
+
67
+ def case_after(c)
68
+ # implement each case after
69
+ end
70
+ end
71
+ end
72
+ end
@@ -0,0 +1,67 @@
1
+ # encoding: utf-8
2
+ require 'spec_helper'
3
+ require 'tbpgr_utils'
4
+
5
+ describe Object do
6
+ context :boolean? do
7
+ cases = [
8
+ {
9
+ case_no: 1,
10
+ case_title: 'true case',
11
+ input: true,
12
+ expected: true,
13
+ },
14
+ {
15
+ case_no: 2,
16
+ case_title: 'false case',
17
+ input: false,
18
+ expected: true,
19
+ },
20
+ {
21
+ case_no: 3,
22
+ case_title: "string 'true' case",
23
+ input: 'true',
24
+ expected: false,
25
+ },
26
+ {
27
+ case_no: 4,
28
+ case_title: 'empty case',
29
+ input: '',
30
+ expected: false,
31
+ },
32
+ {
33
+ case_no: 5,
34
+ case_title: 'nil case',
35
+ input: nil,
36
+ expected: false,
37
+ },
38
+ ]
39
+
40
+ cases.each do |c|
41
+ it "|case_no=#{c[:case_no]}|case_title=#{c[:case_title]}" do
42
+ begin
43
+ case_before c
44
+
45
+ # -- given --
46
+ # nothing
47
+
48
+ # -- when --
49
+ actual = c[:input].boolean?
50
+
51
+ # -- then --
52
+ expect(actual).to eq(c[:expected])
53
+ ensure
54
+ case_after c
55
+ end
56
+ end
57
+
58
+ def case_before(c)
59
+ # implement each case before
60
+ end
61
+
62
+ def case_after(c)
63
+ # implement each case after
64
+ end
65
+ end
66
+ end
67
+ end
@@ -0,0 +1,175 @@
1
+ # encoding: utf-8
2
+ require 'spec_helper'
3
+ require 'tbpgr_utils'
4
+
5
+ describe Object do
6
+ context :guard do
7
+ class HogeGuard
8
+ def hoge_guard_return(input)
9
+ guard(input) { return 'guard!!' }
10
+ 'not guard return'
11
+ end
12
+
13
+ def hoge_guard_fail(input)
14
+ guard(input) { fail ArgumentError, 'error!!' }
15
+ 'not guard fail'
16
+ end
17
+ end
18
+ cases = [
19
+ {
20
+ case_no: 1,
21
+ case_title: 'gard return case',
22
+ input: true,
23
+ action: "return 'guard!!'",
24
+ expected: 'guard!!',
25
+ },
26
+ {
27
+ case_no: 2,
28
+ case_title: 'not gard return case',
29
+ input: false,
30
+ action: "return 'guard!!'",
31
+ expected: 'not guard return',
32
+ },
33
+ {
34
+ case_no: 3,
35
+ case_title: 'gard fail case',
36
+ input: true,
37
+ action: "fail ArgumentError, 'error!!'",
38
+ fail: true,
39
+ expect_error: true
40
+ },
41
+ {
42
+ case_no: 4,
43
+ case_title: 'not gard fail case',
44
+ input: false,
45
+ action: "fail ArgumentError, 'error!!'",
46
+ fail: true,
47
+ expected: 'not guard fail',
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
+ if c[:fail]
60
+ if c[:expect_error]
61
+ # -- when & then --
62
+ lambda { HogeGuard.new.hoge_guard_fail c[:input] }.should raise_error(ArgumentError)
63
+ else
64
+ # -- when --
65
+ actual = HogeGuard.new.hoge_guard_fail c[:input]
66
+ # -- then --
67
+ expect(actual).to eq(c[:expected])
68
+ end
69
+ else
70
+ # -- when --
71
+ actual = HogeGuard.new.hoge_guard_return c[:input]
72
+ # -- then --
73
+ expect(actual).to eq(c[:expected])
74
+ end
75
+
76
+ ensure
77
+ case_after c
78
+ end
79
+ end
80
+
81
+ def case_before(c)
82
+ # implement each case before
83
+ end
84
+
85
+ def case_after(c)
86
+ # implement each case after
87
+ end
88
+ end
89
+ end
90
+
91
+ context :unless_guard do
92
+ class HogeUnlessGuard
93
+ def hoge_unless_guard_return(input)
94
+ unless_guard(input) { return 'unless_guard!!' }
95
+ 'not unless_guard return'
96
+ end
97
+
98
+ def hoge_unless_guard_fail(input)
99
+ unless_guard(input) { fail ArgumentError, 'error!!' }
100
+ 'not unless_guard fail'
101
+ end
102
+ end
103
+ cases = [
104
+ {
105
+ case_no: 1,
106
+ case_title: 'unless_gard return case',
107
+ input: false,
108
+ action: "return 'unless_guard!!'",
109
+ expected: 'unless_guard!!',
110
+ },
111
+ {
112
+ case_no: 2,
113
+ case_title: 'not unless_gard return case',
114
+ input: true,
115
+ action: "return 'unless_guard!!'",
116
+ expected: 'not unless_guard return',
117
+ },
118
+ {
119
+ case_no: 3,
120
+ case_title: 'unless_gard fail case',
121
+ input: false,
122
+ action: "fail ArgumentError, 'error!!'",
123
+ fail: true,
124
+ expect_error: true
125
+ },
126
+ {
127
+ case_no: 4,
128
+ case_title: 'not unless_gard fail case',
129
+ input: true,
130
+ action: "fail ArgumentError, 'error!!'",
131
+ fail: true,
132
+ expected: 'not unless_guard fail',
133
+ },
134
+ ]
135
+
136
+ cases.each do |c|
137
+ it "|case_no=#{c[:case_no]}|case_title=#{c[:case_title]}" do
138
+ begin
139
+ case_before c
140
+
141
+ # -- given --
142
+ # nothing
143
+
144
+ if c[:fail]
145
+ if c[:expect_error]
146
+ # -- when & then --
147
+ lambda { HogeUnlessGuard.new.hoge_unless_guard_fail c[:input] }.should raise_error(ArgumentError)
148
+ else
149
+ # -- when --
150
+ actual = HogeUnlessGuard.new.hoge_unless_guard_fail c[:input]
151
+ # -- then --
152
+ expect(actual).to eq(c[:expected])
153
+ end
154
+ else
155
+ # -- when --
156
+ actual = HogeUnlessGuard.new.hoge_unless_guard_return c[:input]
157
+ # -- then --
158
+ expect(actual).to eq(c[:expected])
159
+ end
160
+
161
+ ensure
162
+ case_after c
163
+ end
164
+ end
165
+
166
+ def case_before(c)
167
+ # implement each case before
168
+ end
169
+
170
+ def case_after(c)
171
+ # implement each case after
172
+ end
173
+ end
174
+ end
175
+ end
@@ -0,0 +1,61 @@
1
+ # encoding: utf-8
2
+ require 'spec_helper'
3
+ require 'tbpgr_utils'
4
+
5
+ describe Object do
6
+ context :my_methods do
7
+ SampleClass = Class.new(String) do
8
+ def public_hello
9
+ "#{self} public hello"
10
+ end
11
+
12
+ protected
13
+
14
+ def protected_hello
15
+ "#{self} protected hello"
16
+ end
17
+
18
+ private
19
+
20
+ def private_hello
21
+ "#{self} private hello"
22
+ end
23
+ end
24
+
25
+ cases = [
26
+ {
27
+ case_no: 1,
28
+ case_title: 'true case',
29
+ input: SampleClass.new,
30
+ expected: [:public_hello, :protected_hello, :private_hello],
31
+ },
32
+ ]
33
+
34
+ cases.each do |c|
35
+ it "|case_no=#{c[:case_no]}|case_title=#{c[:case_title]}" do
36
+ begin
37
+ case_before c
38
+
39
+ # -- given --
40
+ # nothing
41
+
42
+ # -- when --
43
+ actual = c[:input].my_methods
44
+
45
+ # -- then --
46
+ expect(actual).to eq(c[:expected])
47
+ ensure
48
+ case_after c
49
+ end
50
+ end
51
+
52
+ def case_before(c)
53
+ # implement each case before
54
+ end
55
+
56
+ def case_after(c)
57
+ # implement each case after
58
+ end
59
+ end
60
+ end
61
+ end
@@ -0,0 +1,73 @@
1
+ # encoding: utf-8
2
+ require 'spec_helper'
3
+ require 'tbpgr_utils'
4
+
5
+ describe Object do
6
+ context :to_bool do
7
+ cases = [
8
+ {
9
+ case_no: 1,
10
+ case_title: 'TrueClass case',
11
+ input: true,
12
+ expected: true,
13
+ },
14
+ {
15
+ case_no: 2,
16
+ case_title: 'FalseClass case',
17
+ input: false,
18
+ expected: false,
19
+ },
20
+ {
21
+ case_no: 3,
22
+ case_title: "string 'true' case",
23
+ input: 'true',
24
+ expected: true,
25
+ },
26
+ {
27
+ case_no: 4,
28
+ case_title: "string 'false' case",
29
+ input: 'false',
30
+ expected: true,
31
+ },
32
+ {
33
+ case_no: 5,
34
+ case_title: 'empty case',
35
+ input: '',
36
+ expected: true,
37
+ },
38
+ {
39
+ case_no: 6,
40
+ case_title: 'nil case',
41
+ input: nil,
42
+ expected: false,
43
+ },
44
+ ]
45
+
46
+ cases.each do |c|
47
+ it "|case_no=#{c[:case_no]}|case_title=#{c[:case_title]}" do
48
+ begin
49
+ case_before c
50
+
51
+ # -- given --
52
+ # nothing
53
+
54
+ # -- when --
55
+ actual = c[:input].to_bool
56
+
57
+ # -- then --
58
+ expect(actual).to eq(c[:expected])
59
+ ensure
60
+ case_after c
61
+ end
62
+ end
63
+
64
+ def case_before(c)
65
+ # implement each case before
66
+ end
67
+
68
+ def case_after(c)
69
+ # implement each case after
70
+ end
71
+ end
72
+ end
73
+ 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.40
4
+ version: 0.0.41
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-16 00:00:00.000000000 Z
12
+ date: 2014-02-17 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activesupport
16
- requirement: &30397152 !ruby/object:Gem::Requirement
16
+ requirement: &27980232 !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: *30397152
24
+ version_requirements: *27980232
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: bundler
27
- requirement: &30396864 !ruby/object:Gem::Requirement
27
+ requirement: &27994020 !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: *30396864
35
+ version_requirements: *27994020
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: rake
38
- requirement: &30396636 !ruby/object:Gem::Requirement
38
+ requirement: &27990504 !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: *30396636
46
+ version_requirements: *27990504
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: rspec
49
- requirement: &30396312 !ruby/object:Gem::Requirement
49
+ requirement: &27989256 !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: *30396312
57
+ version_requirements: *27989256
58
58
  - !ruby/object:Gem::Dependency
59
59
  name: simplecov
60
- requirement: &30396012 !ruby/object:Gem::Requirement
60
+ requirement: &27988116 !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: *30396012
68
+ version_requirements: *27988116
69
69
  description: Utilities
70
70
  email:
71
71
  - tbpgr@tbpgr.jp
@@ -118,6 +118,11 @@ files:
118
118
  - lib/open_classes/kernel/p_evals.rb
119
119
  - lib/open_classes/module.rb
120
120
  - lib/open_classes/object.rb
121
+ - lib/open_classes/object/any_of.rb
122
+ - lib/open_classes/object/boolean.rb
123
+ - lib/open_classes/object/guard.rb
124
+ - lib/open_classes/object/my_methods.rb
125
+ - lib/open_classes/object/to_bool.rb
121
126
  - lib/open_classes/string.rb
122
127
  - lib/open_classes/string/justify_table.rb
123
128
  - lib/open_classes/string/surround.rb
@@ -162,7 +167,11 @@ files:
162
167
  - spec/open_classes/kernel/print_eval_spec.rb
163
168
  - spec/open_classes/kernel/puts_eval_spec.rb
164
169
  - spec/open_classes/module_spec.rb
165
- - spec/open_classes/object_spec.rb
170
+ - spec/open_classes/object/any_of_spec.rb
171
+ - spec/open_classes/object/boolean_spec.rb
172
+ - spec/open_classes/object/guard_spec.rb
173
+ - spec/open_classes/object/my_methods_spec.rb
174
+ - spec/open_classes/object/to_bool_spec.rb
166
175
  - spec/open_classes/string/justify_table_spec.rb
167
176
  - spec/open_classes/string/surround_spec.rb
168
177
  - spec/spec_helper.rb
@@ -230,7 +239,11 @@ test_files:
230
239
  - spec/open_classes/kernel/print_eval_spec.rb
231
240
  - spec/open_classes/kernel/puts_eval_spec.rb
232
241
  - spec/open_classes/module_spec.rb
233
- - spec/open_classes/object_spec.rb
242
+ - spec/open_classes/object/any_of_spec.rb
243
+ - spec/open_classes/object/boolean_spec.rb
244
+ - spec/open_classes/object/guard_spec.rb
245
+ - spec/open_classes/object/my_methods_spec.rb
246
+ - spec/open_classes/object/to_bool_spec.rb
234
247
  - spec/open_classes/string/justify_table_spec.rb
235
248
  - spec/open_classes/string/surround_spec.rb
236
249
  - spec/spec_helper.rb
@@ -1,259 +0,0 @@
1
- # encoding: utf-8
2
- require 'spec_helper'
3
- require 'tbpgr_utils'
4
-
5
- describe Object do
6
- context :boolean? do
7
- cases = [
8
- {
9
- case_no: 1,
10
- case_title: 'true case',
11
- input: true,
12
- expected: true,
13
- },
14
- {
15
- case_no: 2,
16
- case_title: 'false case',
17
- input: false,
18
- expected: true,
19
- },
20
- {
21
- case_no: 3,
22
- case_title: "string 'true' case",
23
- input: 'true',
24
- expected: false,
25
- },
26
- {
27
- case_no: 4,
28
- case_title: 'empty case',
29
- input: '',
30
- expected: false,
31
- },
32
- {
33
- case_no: 5,
34
- case_title: 'nil case',
35
- input: nil,
36
- expected: false,
37
- },
38
- ]
39
-
40
- cases.each do |c|
41
- it "|case_no=#{c[:case_no]}|case_title=#{c[:case_title]}" do
42
- begin
43
- case_before c
44
-
45
- # -- given --
46
- # nothing
47
-
48
- # -- when --
49
- actual = c[:input].boolean?
50
-
51
- # -- then --
52
- expect(actual).to eq(c[:expected])
53
- ensure
54
- case_after c
55
- end
56
- end
57
-
58
- def case_before(c)
59
- # implement each case before
60
- end
61
-
62
- def case_after(c)
63
- # implement each case after
64
- end
65
- end
66
- end
67
-
68
- context :my_methods do
69
- SampleClass = Class.new(String) do
70
- def public_hello
71
- "#{self} public hello"
72
- end
73
-
74
- protected
75
-
76
- def protected_hello
77
- "#{self} protected hello"
78
- end
79
-
80
- private
81
-
82
- def private_hello
83
- "#{self} private hello"
84
- end
85
- end
86
-
87
- cases = [
88
- {
89
- case_no: 1,
90
- case_title: 'true case',
91
- input: SampleClass.new,
92
- expected: [:public_hello, :protected_hello, :private_hello],
93
- },
94
- ]
95
-
96
- cases.each do |c|
97
- it "|case_no=#{c[:case_no]}|case_title=#{c[:case_title]}" do
98
- begin
99
- case_before c
100
-
101
- # -- given --
102
- # nothing
103
-
104
- # -- when --
105
- actual = c[:input].my_methods
106
-
107
- # -- then --
108
- expect(actual).to eq(c[:expected])
109
- ensure
110
- case_after c
111
- end
112
- end
113
-
114
- def case_before(c)
115
- # implement each case before
116
- end
117
-
118
- def case_after(c)
119
- # implement each case after
120
- end
121
- end
122
- end
123
-
124
-
125
- context :any_of? do
126
- cases = [
127
- {
128
- case_no: 1,
129
- case_title: 'true case',
130
- input: 'hoge',
131
- check: %w{hoge hige},
132
- expected: true,
133
- },
134
- {
135
- case_no: 2,
136
- case_title: 'true case',
137
- input: 'hige',
138
- check: %w{hoge hige},
139
- expected: true,
140
- },
141
- {
142
- case_no: 3,
143
- case_title: 'false case',
144
- input: 'hege',
145
- check: %w{hoge hige},
146
- expected: false,
147
- },
148
- {
149
- case_no: 4,
150
- case_title: 'number true case',
151
- input: 1,
152
- check: [1, 2, 3, 4, 5],
153
- expected: true,
154
- },
155
- {
156
- case_no: 5,
157
- case_title: 'number true case',
158
- input: 6,
159
- check: [1, 2, 3, 4, 5],
160
- expected: false,
161
- },
162
- ]
163
-
164
- cases.each do |c|
165
- it "|case_no=#{c[:case_no]}|case_title=#{c[:case_title]}" do
166
- begin
167
- case_before c
168
-
169
- # -- given --
170
- # nothing
171
-
172
- # -- when --
173
- actual = c[:input].any_of?(*c[:check])
174
-
175
- # -- then --
176
- expect(actual).to eq(c[:expected])
177
- ensure
178
- case_after c
179
- end
180
- end
181
-
182
- def case_before(c)
183
- # implement each case before
184
- end
185
-
186
- def case_after(c)
187
- # implement each case after
188
- end
189
- end
190
- end
191
-
192
- context :to_bool do
193
- cases = [
194
- {
195
- case_no: 1,
196
- case_title: 'TrueClass case',
197
- input: true,
198
- expected: true,
199
- },
200
- {
201
- case_no: 2,
202
- case_title: 'FalseClass case',
203
- input: false,
204
- expected: false,
205
- },
206
- {
207
- case_no: 3,
208
- case_title: "string 'true' case",
209
- input: 'true',
210
- expected: true,
211
- },
212
- {
213
- case_no: 4,
214
- case_title: "string 'false' case",
215
- input: 'false',
216
- expected: true,
217
- },
218
- {
219
- case_no: 5,
220
- case_title: 'empty case',
221
- input: '',
222
- expected: true,
223
- },
224
- {
225
- case_no: 6,
226
- case_title: 'nil case',
227
- input: nil,
228
- expected: false,
229
- },
230
- ]
231
-
232
- cases.each do |c|
233
- it "|case_no=#{c[:case_no]}|case_title=#{c[:case_title]}" do
234
- begin
235
- case_before c
236
-
237
- # -- given --
238
- # nothing
239
-
240
- # -- when --
241
- actual = c[:input].to_bool
242
-
243
- # -- then --
244
- expect(actual).to eq(c[:expected])
245
- ensure
246
- case_after c
247
- end
248
- end
249
-
250
- def case_before(c)
251
- # implement each case before
252
- end
253
-
254
- def case_after(c)
255
- # implement each case after
256
- end
257
- end
258
- end
259
- end