tbpgr_utils 0.0.14 → 0.0.15
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +35 -0
- data/lib/open_classes/array.rb +98 -98
- data/lib/open_classes/module.rb +13 -0
- data/lib/tbpgr_utils.rb +1 -0
- data/lib/tbpgr_utils/version.rb +1 -1
- data/spec/open_classes/array_spec.rb +175 -175
- data/spec/open_classes/module_spec.rb +71 -0
- metadata +13 -10
data/README.md
CHANGED
@@ -33,6 +33,7 @@ Or install it yourself as:
|
|
33
33
|
|TbpgrUtils Kernel#print_eval |Print code + eval result |
|
34
34
|
|TbpgrUtils Kernel#puts_eval |Puts code + eval result |
|
35
35
|
|TbpgrUtils Kernel#bulk_puts_eval |Puts each-line-code + eval result |
|
36
|
+
|TbpgrUtils Module.alias_methods |create alias methods |
|
36
37
|
|TbpgrUtils Object#any_of? |if self match any one of items, return true |
|
37
38
|
|TbpgrUtils Object#boolean? |data type check for boolean |
|
38
39
|
|TbpgrUtils Object#my_methods |return public/protected/private self define methods |
|
@@ -407,6 +408,39 @@ output
|
|
407
408
|
"hoge-hige2" + "add" + message # => "hoge-hige2addmsg"
|
408
409
|
~~~
|
409
410
|
|
411
|
+
### Module.alias_methods
|
412
|
+
create alias methods.
|
413
|
+
|
414
|
+
~~~ruby
|
415
|
+
require "tbpgr_utils"
|
416
|
+
|
417
|
+
class Hoge
|
418
|
+
def hoge
|
419
|
+
"hoge"
|
420
|
+
end
|
421
|
+
|
422
|
+
alias_methods [:hige, :hege, :huge], :hoge
|
423
|
+
end
|
424
|
+
|
425
|
+
Hoge.new.hoge # => "hoge"
|
426
|
+
Hoge.new.hige # => "hoge"
|
427
|
+
Hoge.new.hege # => "hoge"
|
428
|
+
Hoge.new.huge # => "hoge"
|
429
|
+
~~~
|
430
|
+
|
431
|
+
same code is...
|
432
|
+
~~~
|
433
|
+
class Hoge
|
434
|
+
def hoge
|
435
|
+
"hoge"
|
436
|
+
end
|
437
|
+
|
438
|
+
alias_method :hige, :hoge
|
439
|
+
alias_method :hege, :hoge
|
440
|
+
alias_method :huge, :hoge
|
441
|
+
end
|
442
|
+
~~~
|
443
|
+
|
410
444
|
### Object#any_of?
|
411
445
|
~~~ruby
|
412
446
|
require 'tbpgr_utils'
|
@@ -580,6 +614,7 @@ if you are Sublime Text2 user, you can use snippet for TbpgrUtils.
|
|
580
614
|
https://github.com/tbpgr/tbpgr_utils_snippets
|
581
615
|
|
582
616
|
## History
|
617
|
+
* version 0.0.15 : add Module.alias_methods
|
583
618
|
* version 0.0.14 : add Array#together_map(aliases => [tmap, together_collect, tcollect])
|
584
619
|
* version 0.0.13 : add Array#together_with_index, Kernel#bulk_puts_eval
|
585
620
|
* version 0.0.12 : AttributesInitializable::ClassMethods.attr_reader_init,attr_writer_init
|
data/lib/open_classes/array.rb
CHANGED
@@ -1,98 +1,98 @@
|
|
1
|
-
# encoding: utf-8
|
2
|
-
require 'open_classes/object'
|
3
|
-
|
4
|
-
# Array
|
5
|
-
class Array
|
6
|
-
# Arrays loop together.
|
7
|
-
#
|
8
|
-
# alpha = %w{one two three}
|
9
|
-
# numbers = %w{1 2 3}
|
10
|
-
# [alpha, numbers].together do |first, second|
|
11
|
-
# print "#{first}:#{second}\n" # => output one:1, two:2, three:3
|
12
|
-
# end
|
13
|
-
def together
|
14
|
-
if_not_contain_array_rails_type_error
|
15
|
-
first.each_with_index do |i_v, i|
|
16
|
-
eval_each_str = get_args_str_for_together i
|
17
|
-
instance_eval "yield(#{eval_each_str})"
|
18
|
-
end
|
19
|
-
end
|
20
|
-
|
21
|
-
# Arrays loop together with index.
|
22
|
-
#
|
23
|
-
# alpha = %w{one two three}
|
24
|
-
# numbers = %w{1 2 3}
|
25
|
-
# [alpha, numbers].together_with_index do |first, second, index|
|
26
|
-
# print "#{index.to_s}:#{first}:#{second}\n" # => output 0:one:1, 1:two:2, 2:three:3
|
27
|
-
# end
|
28
|
-
def together_with_index
|
29
|
-
if_not_contain_array_rails_type_error
|
30
|
-
first.each_with_index do |i_v, i|
|
31
|
-
eval_each_str = get_args_str_for_together i, true
|
32
|
-
instance_eval "yield(#{eval_each_str})"
|
33
|
-
end
|
34
|
-
end
|
35
|
-
|
36
|
-
# Arrays together map.
|
37
|
-
#
|
38
|
-
# together_map has aliases [:tmap, :together_collect, :tcollect]
|
39
|
-
#
|
40
|
-
# if you want to single Array return
|
41
|
-
# alpha = %w{one two three}
|
42
|
-
# numbers = %w{1 2 3}
|
43
|
-
# print [alpha, numbers].together_map do |first, second|
|
44
|
-
# "#{first}:#{second}\n"
|
45
|
-
# end # => output one:1, two:2, three:3
|
46
|
-
#
|
47
|
-
# if you want to multi Array return
|
48
|
-
# alpha = %w{one two three}
|
49
|
-
# numbers = %w{1 2 3}
|
50
|
-
# print [alpha, numbers].together_map do |first, second|
|
51
|
-
# ["#{first}:#{second}", "#{second}:#{first}"]
|
52
|
-
# end # => output [['1:one', '2:two', '3:three'], ['one:1', 'two:2', 'three:3']]
|
53
|
-
def together_map
|
54
|
-
if_not_contain_array_rails_type_error
|
55
|
-
ret = []
|
56
|
-
first.each_with_index do |i_v, i|
|
57
|
-
eval_each_str = get_args_str_for_together i
|
58
|
-
each_ret = instance_eval "yield(#{eval_each_str})"
|
59
|
-
ret = set_together_each_return(ret, each_ret, i)
|
60
|
-
end
|
61
|
-
ret
|
62
|
-
end
|
63
|
-
|
64
|
-
alias_method :together_collect, :together_map
|
65
|
-
alias_method :tmap, :together_map
|
66
|
-
alias_method :tcollect, :together_map
|
67
|
-
|
68
|
-
private
|
69
|
-
|
70
|
-
def if_not_contain_array_rails_type_error
|
71
|
-
each { |f|fail TypeError, "you have to use [Array1, Array2, ...] | #{f.class} is invalid" unless f.class == Array }
|
72
|
-
end
|
73
|
-
|
74
|
-
def get_args_for_together(i)
|
75
|
-
eval_each = []
|
76
|
-
each_with_index {|j_v, j|eval_each << "self[#{j}][#{i}]"}
|
77
|
-
eval_each
|
78
|
-
end
|
79
|
-
|
80
|
-
def get_args_str_for_together(i, with_index = false)
|
81
|
-
each_eval = with_index ? get_args_for_together(i) << i : get_args_for_together(i)
|
82
|
-
each_eval.join(',')
|
83
|
-
end
|
84
|
-
|
85
|
-
def set_together_each_return(ret, each_ret, index)
|
86
|
-
if together_return_multi?(each_ret)
|
87
|
-
|
88
|
-
(0..(
|
89
|
-
else
|
90
|
-
ret << each_ret
|
91
|
-
end
|
92
|
-
ret
|
93
|
-
end
|
94
|
-
|
95
|
-
def together_return_multi?(list)
|
96
|
-
(list.class == Array && list.size ==
|
97
|
-
end
|
98
|
-
end
|
1
|
+
# encoding: utf-8
|
2
|
+
require 'open_classes/object'
|
3
|
+
|
4
|
+
# Array
|
5
|
+
class Array
|
6
|
+
# Arrays loop together.
|
7
|
+
#
|
8
|
+
# alpha = %w{one two three}
|
9
|
+
# numbers = %w{1 2 3}
|
10
|
+
# [alpha, numbers].together do |first, second|
|
11
|
+
# print "#{first}:#{second}\n" # => output one:1, two:2, three:3
|
12
|
+
# end
|
13
|
+
def together
|
14
|
+
if_not_contain_array_rails_type_error
|
15
|
+
first.each_with_index do |i_v, i|
|
16
|
+
eval_each_str = get_args_str_for_together i
|
17
|
+
instance_eval "yield(#{eval_each_str})"
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
# Arrays loop together with index.
|
22
|
+
#
|
23
|
+
# alpha = %w{one two three}
|
24
|
+
# numbers = %w{1 2 3}
|
25
|
+
# [alpha, numbers].together_with_index do |first, second, index|
|
26
|
+
# print "#{index.to_s}:#{first}:#{second}\n" # => output 0:one:1, 1:two:2, 2:three:3
|
27
|
+
# end
|
28
|
+
def together_with_index
|
29
|
+
if_not_contain_array_rails_type_error
|
30
|
+
first.each_with_index do |i_v, i|
|
31
|
+
eval_each_str = get_args_str_for_together i, true
|
32
|
+
instance_eval "yield(#{eval_each_str})"
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
# Arrays together map.
|
37
|
+
#
|
38
|
+
# together_map has aliases [:tmap, :together_collect, :tcollect]
|
39
|
+
#
|
40
|
+
# if you want to single Array return
|
41
|
+
# alpha = %w{one two three}
|
42
|
+
# numbers = %w{1 2 3}
|
43
|
+
# print [alpha, numbers].together_map do |first, second|
|
44
|
+
# "#{first}:#{second}\n"
|
45
|
+
# end # => output one:1, two:2, three:3
|
46
|
+
#
|
47
|
+
# if you want to multi Array return
|
48
|
+
# alpha = %w{one two three}
|
49
|
+
# numbers = %w{1 2 3}
|
50
|
+
# print [alpha, numbers].together_map do |first, second|
|
51
|
+
# ["#{first}:#{second}", "#{second}:#{first}"]
|
52
|
+
# end # => output [['1:one', '2:two', '3:three'], ['one:1', 'two:2', 'three:3']]
|
53
|
+
def together_map
|
54
|
+
if_not_contain_array_rails_type_error
|
55
|
+
ret = []
|
56
|
+
first.each_with_index do |i_v, i|
|
57
|
+
eval_each_str = get_args_str_for_together i
|
58
|
+
each_ret = instance_eval "yield(#{eval_each_str})"
|
59
|
+
ret = set_together_each_return(ret, each_ret, i)
|
60
|
+
end
|
61
|
+
ret
|
62
|
+
end
|
63
|
+
|
64
|
+
alias_method :together_collect, :together_map
|
65
|
+
alias_method :tmap, :together_map
|
66
|
+
alias_method :tcollect, :together_map
|
67
|
+
|
68
|
+
private
|
69
|
+
|
70
|
+
def if_not_contain_array_rails_type_error
|
71
|
+
each { |f|fail TypeError, "you have to use [Array1, Array2, ...] | #{f.class} is invalid" unless f.class == Array }
|
72
|
+
end
|
73
|
+
|
74
|
+
def get_args_for_together(i)
|
75
|
+
eval_each = []
|
76
|
+
each_with_index { |j_v, j|eval_each << "self[#{j}][#{i}]" }
|
77
|
+
eval_each
|
78
|
+
end
|
79
|
+
|
80
|
+
def get_args_str_for_together(i, with_index = false)
|
81
|
+
each_eval = with_index ? get_args_for_together(i) << i : get_args_for_together(i)
|
82
|
+
each_eval.join(',')
|
83
|
+
end
|
84
|
+
|
85
|
+
def set_together_each_return(ret, each_ret, index)
|
86
|
+
if together_return_multi?(each_ret)
|
87
|
+
size.times { |i|ret << [] } if index == 0
|
88
|
+
(0..(size - 1)).each { |i|ret[i] << each_ret[i] }
|
89
|
+
else
|
90
|
+
ret << each_ret
|
91
|
+
end
|
92
|
+
ret
|
93
|
+
end
|
94
|
+
|
95
|
+
def together_return_multi?(list)
|
96
|
+
(list.class == Array && list.size == size).to_bool
|
97
|
+
end
|
98
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
# Module
|
4
|
+
class Module
|
5
|
+
# create alias methods.
|
6
|
+
#
|
7
|
+
# create hoge's aliases [:hige, :huge] case
|
8
|
+
# alias_methods [:hige, :fuge], :hoge
|
9
|
+
def alias_methods(new_methods, old_method)
|
10
|
+
fail TypeError, "invalid type #{new_methods.class}" unless new_methods.class == Array
|
11
|
+
new_methods.each { |new_method|alias_method new_method, old_method }
|
12
|
+
end
|
13
|
+
end
|
data/lib/tbpgr_utils.rb
CHANGED
data/lib/tbpgr_utils/version.rb
CHANGED
@@ -1,175 +1,175 @@
|
|
1
|
-
# encoding: utf-8
|
2
|
-
require 'spec_helper'
|
3
|
-
require 'tbpgr_utils'
|
4
|
-
|
5
|
-
describe Array do
|
6
|
-
context :together do
|
7
|
-
cases = [
|
8
|
-
{
|
9
|
-
case_no: 1,
|
10
|
-
case_title: 'valid case',
|
11
|
-
inputs: [[1, 2, 3], %w{one two three}],
|
12
|
-
expected: ['1:one', '2:two', '3:three'],
|
13
|
-
},
|
14
|
-
{
|
15
|
-
case_no: 2,
|
16
|
-
case_title: 'contain nil case',
|
17
|
-
inputs: [[1, 2, 3], %w{one two}],
|
18
|
-
expected: ['1:one', '2:two', '3:'],
|
19
|
-
},
|
20
|
-
]
|
21
|
-
|
22
|
-
cases.each do |c|
|
23
|
-
it "|case_no=#{c[:case_no]}|case_title=#{c[:case_title]}" do
|
24
|
-
begin
|
25
|
-
case_before c
|
26
|
-
|
27
|
-
# -- given --
|
28
|
-
# nothing
|
29
|
-
|
30
|
-
# -- when/then --
|
31
|
-
actual = []
|
32
|
-
c[:inputs].together do |first, second|
|
33
|
-
actual << "#{first}:#{second}"
|
34
|
-
end
|
35
|
-
|
36
|
-
expect(actual).to eq(c[:expected])
|
37
|
-
ensure
|
38
|
-
case_after c
|
39
|
-
end
|
40
|
-
end
|
41
|
-
|
42
|
-
def case_before(c)
|
43
|
-
# implement each case before
|
44
|
-
end
|
45
|
-
|
46
|
-
def case_after(c)
|
47
|
-
# implement each case after
|
48
|
-
end
|
49
|
-
end
|
50
|
-
end
|
51
|
-
|
52
|
-
context :together_with_index do
|
53
|
-
cases = [
|
54
|
-
{
|
55
|
-
case_no: 1,
|
56
|
-
case_title: 'valid case',
|
57
|
-
inputs: [[1, 2, 3], %w{one two three}],
|
58
|
-
expected: ['0:1:one', '1:2:two', '2:3:three'],
|
59
|
-
},
|
60
|
-
{
|
61
|
-
case_no: 2,
|
62
|
-
case_title: 'contain nil case',
|
63
|
-
inputs: [[1, 2, 3], %w{one two}],
|
64
|
-
expected: ['0:1:one', '1:2:two', '2:3:'],
|
65
|
-
},
|
66
|
-
]
|
67
|
-
|
68
|
-
cases.each do |c|
|
69
|
-
it "|case_no=#{c[:case_no]}|case_title=#{c[:case_title]}" do
|
70
|
-
begin
|
71
|
-
case_before c
|
72
|
-
|
73
|
-
# -- given --
|
74
|
-
# nothing
|
75
|
-
|
76
|
-
# -- when/then --
|
77
|
-
actual = []
|
78
|
-
c[:inputs].together_with_index do |first, second, index|
|
79
|
-
actual << "#{index.to_s}:#{first}:#{second}"
|
80
|
-
end
|
81
|
-
|
82
|
-
expect(actual).to eq(c[:expected])
|
83
|
-
ensure
|
84
|
-
case_after c
|
85
|
-
end
|
86
|
-
end
|
87
|
-
|
88
|
-
def case_before(c)
|
89
|
-
# implement each case before
|
90
|
-
end
|
91
|
-
|
92
|
-
def case_after(c)
|
93
|
-
# implement each case after
|
94
|
-
end
|
95
|
-
end
|
96
|
-
end
|
97
|
-
|
98
|
-
context :together_map do
|
99
|
-
cases = [
|
100
|
-
{
|
101
|
-
case_no: 1,
|
102
|
-
case_title: 'valid case',
|
103
|
-
method_name: :together_map,
|
104
|
-
inputs: [[1, 2, 3], %w{one two three}],
|
105
|
-
expected: ['1:one', '2:two', '3:three'],
|
106
|
-
},
|
107
|
-
{
|
108
|
-
case_no: 2,
|
109
|
-
case_title: 'contain nil case',
|
110
|
-
method_name: :together_map,
|
111
|
-
inputs: [[1, 2, 3], %w{one two}],
|
112
|
-
expected: ['1:one', '2:two', '3:'],
|
113
|
-
},
|
114
|
-
{
|
115
|
-
case_no: 3,
|
116
|
-
case_title: 'valid case',
|
117
|
-
method_name: :together_map,
|
118
|
-
inputs: [[1, 2, 3], %w{one two three}],
|
119
|
-
expected: [['1:one', '2:two', '3:three'], ['one:1', 'two:2', 'three:3']],
|
120
|
-
is_multi: true,
|
121
|
-
},
|
122
|
-
{
|
123
|
-
case_no: 4,
|
124
|
-
case_title: 'valid case(alias together_collect)',
|
125
|
-
method_name: :together_collect,
|
126
|
-
inputs: [[1, 2, 3], %w{one two three}],
|
127
|
-
expected: ['1:one', '2:two', '3:three'],
|
128
|
-
},
|
129
|
-
{
|
130
|
-
case_no: 5,
|
131
|
-
case_title: 'valid case(alias tmap)',
|
132
|
-
method_name: :tmap,
|
133
|
-
inputs: [[1, 2, 3], %w{one two three}],
|
134
|
-
expected: ['1:one', '2:two', '3:three'],
|
135
|
-
},
|
136
|
-
{
|
137
|
-
case_no: 6,
|
138
|
-
case_title: 'valid case(alias tcollect)',
|
139
|
-
method_name: :tcollect,
|
140
|
-
inputs: [[1, 2, 3], %w{one two three}],
|
141
|
-
expected: ['1:one', '2:two', '3:three'],
|
142
|
-
},
|
143
|
-
]
|
144
|
-
|
145
|
-
cases.each do |c|
|
146
|
-
it "|case_no=#{c[:case_no]}|case_title=#{c[:case_title]}" do
|
147
|
-
begin
|
148
|
-
case_before c
|
149
|
-
|
150
|
-
# -- given --
|
151
|
-
# nothing
|
152
|
-
|
153
|
-
# -- when/then --
|
154
|
-
if c[:is_multi]
|
155
|
-
actual = c[:inputs].method(c[:method_name]).call {|first, second|["#{first}:#{second}", "#{second}:#{first}"]}
|
156
|
-
else
|
157
|
-
actual = c[:inputs].method(c[:method_name]).call {|first, second|"#{first}:#{second}"}
|
158
|
-
end
|
159
|
-
|
160
|
-
expect(actual).to eq(c[:expected])
|
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
|
1
|
+
# encoding: utf-8
|
2
|
+
require 'spec_helper'
|
3
|
+
require 'tbpgr_utils'
|
4
|
+
|
5
|
+
describe Array do
|
6
|
+
context :together do
|
7
|
+
cases = [
|
8
|
+
{
|
9
|
+
case_no: 1,
|
10
|
+
case_title: 'valid case',
|
11
|
+
inputs: [[1, 2, 3], %w{one two three}],
|
12
|
+
expected: ['1:one', '2:two', '3:three'],
|
13
|
+
},
|
14
|
+
{
|
15
|
+
case_no: 2,
|
16
|
+
case_title: 'contain nil case',
|
17
|
+
inputs: [[1, 2, 3], %w{one two}],
|
18
|
+
expected: ['1:one', '2:two', '3:'],
|
19
|
+
},
|
20
|
+
]
|
21
|
+
|
22
|
+
cases.each do |c|
|
23
|
+
it "|case_no=#{c[:case_no]}|case_title=#{c[:case_title]}" do
|
24
|
+
begin
|
25
|
+
case_before c
|
26
|
+
|
27
|
+
# -- given --
|
28
|
+
# nothing
|
29
|
+
|
30
|
+
# -- when/then --
|
31
|
+
actual = []
|
32
|
+
c[:inputs].together do |first, second|
|
33
|
+
actual << "#{first}:#{second}"
|
34
|
+
end
|
35
|
+
|
36
|
+
expect(actual).to eq(c[:expected])
|
37
|
+
ensure
|
38
|
+
case_after c
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
def case_before(c)
|
43
|
+
# implement each case before
|
44
|
+
end
|
45
|
+
|
46
|
+
def case_after(c)
|
47
|
+
# implement each case after
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
context :together_with_index do
|
53
|
+
cases = [
|
54
|
+
{
|
55
|
+
case_no: 1,
|
56
|
+
case_title: 'valid case',
|
57
|
+
inputs: [[1, 2, 3], %w{one two three}],
|
58
|
+
expected: ['0:1:one', '1:2:two', '2:3:three'],
|
59
|
+
},
|
60
|
+
{
|
61
|
+
case_no: 2,
|
62
|
+
case_title: 'contain nil case',
|
63
|
+
inputs: [[1, 2, 3], %w{one two}],
|
64
|
+
expected: ['0:1:one', '1:2:two', '2:3:'],
|
65
|
+
},
|
66
|
+
]
|
67
|
+
|
68
|
+
cases.each do |c|
|
69
|
+
it "|case_no=#{c[:case_no]}|case_title=#{c[:case_title]}" do
|
70
|
+
begin
|
71
|
+
case_before c
|
72
|
+
|
73
|
+
# -- given --
|
74
|
+
# nothing
|
75
|
+
|
76
|
+
# -- when/then --
|
77
|
+
actual = []
|
78
|
+
c[:inputs].together_with_index do |first, second, index|
|
79
|
+
actual << "#{index.to_s}:#{first}:#{second}"
|
80
|
+
end
|
81
|
+
|
82
|
+
expect(actual).to eq(c[:expected])
|
83
|
+
ensure
|
84
|
+
case_after c
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
def case_before(c)
|
89
|
+
# implement each case before
|
90
|
+
end
|
91
|
+
|
92
|
+
def case_after(c)
|
93
|
+
# implement each case after
|
94
|
+
end
|
95
|
+
end
|
96
|
+
end
|
97
|
+
|
98
|
+
context :together_map do
|
99
|
+
cases = [
|
100
|
+
{
|
101
|
+
case_no: 1,
|
102
|
+
case_title: 'valid case',
|
103
|
+
method_name: :together_map,
|
104
|
+
inputs: [[1, 2, 3], %w{one two three}],
|
105
|
+
expected: ['1:one', '2:two', '3:three'],
|
106
|
+
},
|
107
|
+
{
|
108
|
+
case_no: 2,
|
109
|
+
case_title: 'contain nil case',
|
110
|
+
method_name: :together_map,
|
111
|
+
inputs: [[1, 2, 3], %w{one two}],
|
112
|
+
expected: ['1:one', '2:two', '3:'],
|
113
|
+
},
|
114
|
+
{
|
115
|
+
case_no: 3,
|
116
|
+
case_title: 'valid case',
|
117
|
+
method_name: :together_map,
|
118
|
+
inputs: [[1, 2, 3], %w{one two three}],
|
119
|
+
expected: [['1:one', '2:two', '3:three'], ['one:1', 'two:2', 'three:3']],
|
120
|
+
is_multi: true,
|
121
|
+
},
|
122
|
+
{
|
123
|
+
case_no: 4,
|
124
|
+
case_title: 'valid case(alias together_collect)',
|
125
|
+
method_name: :together_collect,
|
126
|
+
inputs: [[1, 2, 3], %w{one two three}],
|
127
|
+
expected: ['1:one', '2:two', '3:three'],
|
128
|
+
},
|
129
|
+
{
|
130
|
+
case_no: 5,
|
131
|
+
case_title: 'valid case(alias tmap)',
|
132
|
+
method_name: :tmap,
|
133
|
+
inputs: [[1, 2, 3], %w{one two three}],
|
134
|
+
expected: ['1:one', '2:two', '3:three'],
|
135
|
+
},
|
136
|
+
{
|
137
|
+
case_no: 6,
|
138
|
+
case_title: 'valid case(alias tcollect)',
|
139
|
+
method_name: :tcollect,
|
140
|
+
inputs: [[1, 2, 3], %w{one two three}],
|
141
|
+
expected: ['1:one', '2:two', '3:three'],
|
142
|
+
},
|
143
|
+
]
|
144
|
+
|
145
|
+
cases.each do |c|
|
146
|
+
it "|case_no=#{c[:case_no]}|case_title=#{c[:case_title]}" do
|
147
|
+
begin
|
148
|
+
case_before c
|
149
|
+
|
150
|
+
# -- given --
|
151
|
+
# nothing
|
152
|
+
|
153
|
+
# -- when/then --
|
154
|
+
if c[:is_multi]
|
155
|
+
actual = c[:inputs].method(c[:method_name]).call { |first, second|["#{first}:#{second}", "#{second}:#{first}"] }
|
156
|
+
else
|
157
|
+
actual = c[:inputs].method(c[:method_name]).call { |first, second|"#{first}:#{second}" }
|
158
|
+
end
|
159
|
+
|
160
|
+
expect(actual).to eq(c[:expected])
|
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,71 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
require 'spec_helper'
|
3
|
+
require 'open_classes/module'
|
4
|
+
|
5
|
+
describe Module do
|
6
|
+
context :alias_methods do
|
7
|
+
class Hoge
|
8
|
+
def hoge
|
9
|
+
'hoge'
|
10
|
+
end
|
11
|
+
|
12
|
+
alias_methods [:hige, :hege, :huge], :hoge
|
13
|
+
end
|
14
|
+
cases = [
|
15
|
+
{
|
16
|
+
case_no: 1,
|
17
|
+
case_title: 'valid case',
|
18
|
+
klass: Hoge,
|
19
|
+
target: :hoge,
|
20
|
+
inputs: [:hige, :hege, :huge],
|
21
|
+
expected: 'hoge',
|
22
|
+
},
|
23
|
+
{
|
24
|
+
case_no: 2,
|
25
|
+
case_title: 'not Array error case',
|
26
|
+
klass: Hoge,
|
27
|
+
expect_error: true,
|
28
|
+
},
|
29
|
+
]
|
30
|
+
|
31
|
+
cases.each do |c|
|
32
|
+
it "|case_no=#{c[:case_no]}|case_title=#{c[:case_title]}" do
|
33
|
+
begin
|
34
|
+
case_before c
|
35
|
+
|
36
|
+
# -- given --
|
37
|
+
hoge = c[:klass].new
|
38
|
+
|
39
|
+
# -- when --
|
40
|
+
if c[:expect_error]
|
41
|
+
lambda do
|
42
|
+
class Hige
|
43
|
+
def hige
|
44
|
+
'hige'
|
45
|
+
end
|
46
|
+
alias_methods 'invalid type', :hige
|
47
|
+
end
|
48
|
+
end.should raise_error(TypeError)
|
49
|
+
next
|
50
|
+
end
|
51
|
+
|
52
|
+
c[:inputs].each do |m|
|
53
|
+
actual = hoge.send m
|
54
|
+
# -- then --
|
55
|
+
expect(actual).to eq(c[:expected])
|
56
|
+
end
|
57
|
+
ensure
|
58
|
+
case_after c
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
def case_before(c)
|
63
|
+
# implement each case before
|
64
|
+
end
|
65
|
+
|
66
|
+
def case_after(c)
|
67
|
+
# implement each case after
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
71
|
+
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.
|
4
|
+
version: 0.0.15
|
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-01-
|
12
|
+
date: 2014-01-22 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activesupport
|
16
|
-
requirement: &
|
16
|
+
requirement: &21078144 !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: *
|
24
|
+
version_requirements: *21078144
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: bundler
|
27
|
-
requirement: &
|
27
|
+
requirement: &21077832 !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: *
|
35
|
+
version_requirements: *21077832
|
36
36
|
- !ruby/object:Gem::Dependency
|
37
37
|
name: rake
|
38
|
-
requirement: &
|
38
|
+
requirement: &21077604 !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: *
|
46
|
+
version_requirements: *21077604
|
47
47
|
- !ruby/object:Gem::Dependency
|
48
48
|
name: rspec
|
49
|
-
requirement: &
|
49
|
+
requirement: &21077256 !ruby/object:Gem::Requirement
|
50
50
|
none: false
|
51
51
|
requirements:
|
52
52
|
- - ~>
|
@@ -54,7 +54,7 @@ dependencies:
|
|
54
54
|
version: 2.14.1
|
55
55
|
type: :development
|
56
56
|
prerelease: false
|
57
|
-
version_requirements: *
|
57
|
+
version_requirements: *21077256
|
58
58
|
description: Utilities
|
59
59
|
email:
|
60
60
|
- tbpgr@tbpgr.jp
|
@@ -72,6 +72,7 @@ files:
|
|
72
72
|
- lib/ghostable.rb
|
73
73
|
- lib/open_classes/array.rb
|
74
74
|
- lib/open_classes/kernel.rb
|
75
|
+
- lib/open_classes/module.rb
|
75
76
|
- lib/open_classes/object.rb
|
76
77
|
- lib/open_classes/string.rb
|
77
78
|
- lib/tbpgr_utils.rb
|
@@ -84,6 +85,7 @@ files:
|
|
84
85
|
- spec/ghostable_spec.rb
|
85
86
|
- spec/open_classes/array_spec.rb
|
86
87
|
- spec/open_classes/kernel_spec.rb
|
88
|
+
- spec/open_classes/module_spec.rb
|
87
89
|
- spec/open_classes/object_spec.rb
|
88
90
|
- spec/open_classes/string_spec.rb
|
89
91
|
- spec/spec_helper.rb
|
@@ -121,6 +123,7 @@ test_files:
|
|
121
123
|
- spec/ghostable_spec.rb
|
122
124
|
- spec/open_classes/array_spec.rb
|
123
125
|
- spec/open_classes/kernel_spec.rb
|
126
|
+
- spec/open_classes/module_spec.rb
|
124
127
|
- spec/open_classes/object_spec.rb
|
125
128
|
- spec/open_classes/string_spec.rb
|
126
129
|
- spec/spec_helper.rb
|