tbpgr_utils 0.0.19 → 0.0.20

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -21,6 +21,7 @@ Or install it yourself as:
21
21
  | class/module/method | mean |
22
22
  |:----------- |:------------ |
23
23
  |[TbpgrUtils Array#together](#arraytogether) |loop all arrays by block |
24
+ |[TbpgrUtils Array#together_at](#arraytogether_at) |together version of Array#at. together_at has alias :tat |
24
25
  |[TbpgrUtils Array#together_concat](#arraytogether_concat) |together version of Array#concat. together_concat has alias :tconcat |
25
26
  |[TbpgrUtils Array#together_map](#arraytogether_mapor-tmap-together_collect-tcollect) |together version of Enumerable#map. together_map has aliases [:tmap, :together_collect, :tcollect] |
26
27
  |[TbpgrUtils Array#together_reduce](#arraytogether_reduceor-treduce-together_inject-tinject) |together version of Enumerable#reduce. together_reduce has aliases [:treduce, :together_inject, :tinject] |
@@ -59,6 +60,23 @@ end
59
60
 
60
61
  [back to list](#list)
61
62
 
63
+ ### Array#together_at
64
+ ~~~ruby
65
+ require 'tbpgr_utils'
66
+
67
+ # same elements size case
68
+ alpha = %w{one two three}
69
+ numbers = %w{1 2 3}
70
+ print [alpha, numbers].together_at 2 # => output ['three', 3]
71
+
72
+ # different elements size case
73
+ alpha = %w{one two three}
74
+ numbers = %w{1 2}
75
+ print [alpha, numbers].together_at 2 # => output ['three', nil]
76
+ ~~~
77
+
78
+ [back to list](#list)
79
+
62
80
  ### Array#together_concat
63
81
  ~~~ruby
64
82
  require 'tbpgr_utils'
@@ -766,8 +784,9 @@ if you are Sublime Text2 user, you can use snippet for TbpgrUtils.
766
784
  https://github.com/tbpgr/tbpgr_utils_snippets
767
785
 
768
786
  ## History
787
+ * version 0.0.20 : add Array#together_at. together_at has alias :tat
769
788
  * version 0.0.19 : add AttributesHashable module.
770
- * version 0.0.18 : add Array#together_concat. together_concat has alias :tconcat
789
+ * version 0.0.18 : add Array#together_concat. together_concat has alias :tconcat
771
790
  * version 0.0.17 : add Array#together_reduce(or :treduce, :together_inject, :tinject)
772
791
  * version 0.0.16 : add Array#together_select(or tselect, together_find_all, tfindall)
773
792
  * version 0.0.15 : add Module.alias_methods
@@ -133,6 +133,8 @@ class Array
133
133
 
134
134
  # Arrays bulk concat.
135
135
  #
136
+ # together_concat has alias :tconcat
137
+ #
136
138
  # alpha = %w{one two three}
137
139
  # numbers = %w{1 2 3}
138
140
  # [alpha, numbers].together do |first, second|
@@ -143,6 +145,24 @@ class Array
143
145
  each { |list|list.concat other }
144
146
  end
145
147
 
148
+ # Arrays bulk at.
149
+ #
150
+ # together_at has alias :tat
151
+ #
152
+ # same elements size case
153
+ # alpha = %w{one two three}
154
+ # numbers = %w{1 2 3}
155
+ # [alpha, numbers].together_at 2 # => output ['three', 3]
156
+ #
157
+ # different elements size case
158
+ # alpha = %w{one two three}
159
+ # numbers = %w{1 2 nil}
160
+ # [alpha, numbers].together_at 2 # => output ['three', nil]
161
+ def together_at(index)
162
+ if_not_contain_array_rails_type_error
163
+ reduce([]) { |ats, list|ats << list.at(index) }
164
+ end
165
+
146
166
  private
147
167
 
148
168
  def if_not_contain_array_rails_type_error
@@ -191,6 +211,7 @@ class Array
191
211
  end
192
212
 
193
213
  alias_method :tconcat, :together_concat
214
+ alias_method :tat, :together_at
194
215
  alias_methods [:together_collect, :tmap, :tcollect], :together_map
195
216
  alias_methods [:together_find_all, :tselect, :tfindall], :together_select
196
217
  alias_methods [:together_inject, :treduce, :tinject], :together_reduce
@@ -2,5 +2,5 @@
2
2
 
3
3
  # Tbpgr Utilities
4
4
  module TbpgrUtils
5
- VERSION = '0.0.19'
5
+ VERSION = '0.0.20'
6
6
  end
@@ -1,389 +1,445 @@
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
-
176
- context :together_select do
177
- cases = [
178
- {
179
- case_no: 1,
180
- case_title: 'single valid case',
181
- inputs: [[1, 2, 3, 4], [4, 2, 3, 1]],
182
- condition: 'first == second',
183
- method_name: :together_select,
184
- expected: [[2, 3], [2, 3]],
185
- },
186
- {
187
- case_no: 2,
188
- case_title: 'multi valid case',
189
- inputs: [[1, 2, 3, 4], [4, 2, 3, 1]],
190
- condition: '[first.odd?, second.even?]',
191
- expected: [[1, 3], [4, 2]],
192
- method_name: :together_select,
193
- },
194
- {
195
- case_no: 3,
196
- case_title: 'multi valid case',
197
- inputs: [[1, 2, 3, 4], [4, 2, 3, 1]],
198
- condition: '[first.odd?, second.even?]',
199
- expected: [[1, 3], [4, 2]],
200
- method_name: :tselect,
201
- },
202
- {
203
- case_no: 4,
204
- case_title: 'multi valid case',
205
- inputs: [[1, 2, 3, 4], [4, 2, 3, 1]],
206
- condition: '[first.odd?, second.even?]',
207
- expected: [[1, 3], [4, 2]],
208
- method_name: :together_find_all,
209
- },
210
- {
211
- case_no: 5,
212
- case_title: 'multi valid case',
213
- inputs: [[1, 2, 3, 4], [4, 2, 3, 1]],
214
- condition: '[first.odd?, second.even?]',
215
- expected: [[1, 3], [4, 2]],
216
- method_name: :tfindall,
217
- },
218
- ]
219
-
220
- cases.each do |c|
221
- it "|case_no=#{c[:case_no]}|case_title=#{c[:case_title]}" do
222
- begin
223
- case_before c
224
-
225
- # -- given --
226
- # nothing
227
-
228
- # -- when/then --
229
- actual = c[:inputs].method(c[:method_name]).call { |first, second| eval c[:condition], binding }
230
-
231
- expect(actual).to eq(c[:expected])
232
- ensure
233
- case_after c
234
- end
235
- end
236
-
237
- def case_before(c)
238
- # implement each case before
239
- end
240
-
241
- def case_after(c)
242
- # implement each case after
243
- end
244
- end
245
- end
246
-
247
- context :together_reduce do
248
- cases = [
249
- {
250
- case_no: 1,
251
- case_title: 'single valid case',
252
- inputs: [[1, 2, 3, 4], [4, 2, 3, 1]],
253
- init: nil,
254
- logic: 'memo + first + second',
255
- method_name: :together_reduce,
256
- expected: 20,
257
- },
258
- {
259
- case_no: 2,
260
- case_title: 'single with init valid case',
261
- inputs: [[1, 2, 3, 4], [4, 2, 3, 1]],
262
- init: 10,
263
- logic: 'memo + first + second',
264
- method_name: :together_reduce,
265
- expected: 30,
266
- },
267
- {
268
- case_no: 5,
269
- case_title: 'single valid case',
270
- inputs: [[1, 2, 3, 4], [4, 2, 3, 1]],
271
- logic: 'memo + first + second',
272
- expected: 20,
273
- method_name: :treduce,
274
- },
275
- {
276
- case_no: 6,
277
- case_title: 'single valid case',
278
- inputs: [[1, 2, 3, 4], [4, 2, 3, 1]],
279
- logic: 'memo + first + second',
280
- expected: 20,
281
- method_name: :together_inject,
282
- },
283
- {
284
- case_no: 7,
285
- case_title: 'multi valid case',
286
- inputs: [[1, 2, 3, 4], [4, 2, 3, 1]],
287
- logic: 'memo + first + second',
288
- expected: 20,
289
- method_name: :tinject,
290
- },
291
- {
292
- case_no: 8,
293
- case_title: 'single with init valid array case',
294
- inputs: [[1, 2, 3, 4], [4, 2, 3, 1]],
295
- init: [],
296
- logic: 'memo << first + second',
297
- method_name: :together_reduce,
298
- expected: [5, 4, 6, 5],
299
- },
300
- {
301
- case_no: 9,
302
- case_title: 'single with init valid array case',
303
- inputs: [[1, 2, 3, 4], [4, 2, 3, 1]],
304
- init: {},
305
- logic: 'memo[first] = second; memo',
306
- method_name: :together_reduce,
307
- expected: { 1 => 4, 2 => 2, 3 => 3, 4 => 1 },
308
- },
309
- ]
310
-
311
- cases.each do |c|
312
- it "|case_no=#{c[:case_no]}|case_title=#{c[:case_title]}" do
313
- begin
314
- case_before c
315
-
316
- # -- given --
317
- # nothing
318
-
319
- # -- when/then --
320
- if c[:init]
321
- actual = c[:inputs].method(c[:method_name]).call(c[:init]) { |memo, first, second| eval c[:logic], binding }
322
- else
323
- actual = c[:inputs].method(c[:method_name]).call { |memo, first, second| eval c[:logic], binding }
324
- end
325
-
326
- expect(actual).to eq(c[:expected])
327
- ensure
328
- case_after c
329
- end
330
- end
331
-
332
- def case_before(c)
333
- # implement each case before
334
- end
335
-
336
- def case_after(c)
337
- # implement each case after
338
- end
339
- end
340
- end
341
-
342
- context :together_concat do
343
- cases = [
344
- {
345
- case_no: 1,
346
- case_title: 'valid case',
347
- inputs: [[1, 2, 3], %w{one two three}],
348
- add_list: [4, 5, 6],
349
- method_name: :together_concat,
350
- expected: [[1, 2, 3, 4, 5, 6], ['one', 'two', 'three', 4, 5, 6]],
351
- },
352
- {
353
- case_no: 2,
354
- case_title: 'valid case',
355
- inputs: [[1, 2, 3], %w{one two three}],
356
- add_list: [4, 5, 6],
357
- method_name: :tconcat,
358
- expected: [[1, 2, 3, 4, 5, 6], ['one', 'two', 'three', 4, 5, 6]],
359
- },
360
- ]
361
-
362
- cases.each do |c|
363
- it "|case_no=#{c[:case_no]}|case_title=#{c[:case_title]}" do
364
- begin
365
- case_before c
366
-
367
- # -- given --
368
- # nothing
369
-
370
- # -- when --
371
- c[:inputs].send c[:method_name], c[:add_list]
372
-
373
- # -- then --
374
- expect(c[:inputs]).to eq(c[:expected])
375
- ensure
376
- case_after c
377
- end
378
- end
379
-
380
- def case_before(c)
381
- # implement each case before
382
- end
383
-
384
- def case_after(c)
385
- # implement each case after
386
- end
387
- end
388
- end
389
- 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
+
176
+ context :together_select do
177
+ cases = [
178
+ {
179
+ case_no: 1,
180
+ case_title: 'single valid case',
181
+ inputs: [[1, 2, 3, 4], [4, 2, 3, 1]],
182
+ condition: 'first == second',
183
+ method_name: :together_select,
184
+ expected: [[2, 3], [2, 3]],
185
+ },
186
+ {
187
+ case_no: 2,
188
+ case_title: 'multi valid case',
189
+ inputs: [[1, 2, 3, 4], [4, 2, 3, 1]],
190
+ condition: '[first.odd?, second.even?]',
191
+ expected: [[1, 3], [4, 2]],
192
+ method_name: :together_select,
193
+ },
194
+ {
195
+ case_no: 3,
196
+ case_title: 'multi valid case',
197
+ inputs: [[1, 2, 3, 4], [4, 2, 3, 1]],
198
+ condition: '[first.odd?, second.even?]',
199
+ expected: [[1, 3], [4, 2]],
200
+ method_name: :tselect,
201
+ },
202
+ {
203
+ case_no: 4,
204
+ case_title: 'multi valid case',
205
+ inputs: [[1, 2, 3, 4], [4, 2, 3, 1]],
206
+ condition: '[first.odd?, second.even?]',
207
+ expected: [[1, 3], [4, 2]],
208
+ method_name: :together_find_all,
209
+ },
210
+ {
211
+ case_no: 5,
212
+ case_title: 'multi valid case',
213
+ inputs: [[1, 2, 3, 4], [4, 2, 3, 1]],
214
+ condition: '[first.odd?, second.even?]',
215
+ expected: [[1, 3], [4, 2]],
216
+ method_name: :tfindall,
217
+ },
218
+ ]
219
+
220
+ cases.each do |c|
221
+ it "|case_no=#{c[:case_no]}|case_title=#{c[:case_title]}" do
222
+ begin
223
+ case_before c
224
+
225
+ # -- given --
226
+ # nothing
227
+
228
+ # -- when/then --
229
+ actual = c[:inputs].method(c[:method_name]).call { |first, second| eval c[:condition], binding }
230
+
231
+ expect(actual).to eq(c[:expected])
232
+ ensure
233
+ case_after c
234
+ end
235
+ end
236
+
237
+ def case_before(c)
238
+ # implement each case before
239
+ end
240
+
241
+ def case_after(c)
242
+ # implement each case after
243
+ end
244
+ end
245
+ end
246
+
247
+ context :together_reduce do
248
+ cases = [
249
+ {
250
+ case_no: 1,
251
+ case_title: 'single valid case',
252
+ inputs: [[1, 2, 3, 4], [4, 2, 3, 1]],
253
+ init: nil,
254
+ logic: 'memo + first + second',
255
+ method_name: :together_reduce,
256
+ expected: 20,
257
+ },
258
+ {
259
+ case_no: 2,
260
+ case_title: 'single with init valid case',
261
+ inputs: [[1, 2, 3, 4], [4, 2, 3, 1]],
262
+ init: 10,
263
+ logic: 'memo + first + second',
264
+ method_name: :together_reduce,
265
+ expected: 30,
266
+ },
267
+ {
268
+ case_no: 5,
269
+ case_title: 'single valid case',
270
+ inputs: [[1, 2, 3, 4], [4, 2, 3, 1]],
271
+ logic: 'memo + first + second',
272
+ expected: 20,
273
+ method_name: :treduce,
274
+ },
275
+ {
276
+ case_no: 6,
277
+ case_title: 'single valid case',
278
+ inputs: [[1, 2, 3, 4], [4, 2, 3, 1]],
279
+ logic: 'memo + first + second',
280
+ expected: 20,
281
+ method_name: :together_inject,
282
+ },
283
+ {
284
+ case_no: 7,
285
+ case_title: 'multi valid case',
286
+ inputs: [[1, 2, 3, 4], [4, 2, 3, 1]],
287
+ logic: 'memo + first + second',
288
+ expected: 20,
289
+ method_name: :tinject,
290
+ },
291
+ {
292
+ case_no: 8,
293
+ case_title: 'single with init valid array case',
294
+ inputs: [[1, 2, 3, 4], [4, 2, 3, 1]],
295
+ init: [],
296
+ logic: 'memo << first + second',
297
+ method_name: :together_reduce,
298
+ expected: [5, 4, 6, 5],
299
+ },
300
+ {
301
+ case_no: 9,
302
+ case_title: 'single with init valid array case',
303
+ inputs: [[1, 2, 3, 4], [4, 2, 3, 1]],
304
+ init: {},
305
+ logic: 'memo[first] = second; memo',
306
+ method_name: :together_reduce,
307
+ expected: { 1 => 4, 2 => 2, 3 => 3, 4 => 1 },
308
+ },
309
+ ]
310
+
311
+ cases.each do |c|
312
+ it "|case_no=#{c[:case_no]}|case_title=#{c[:case_title]}" do
313
+ begin
314
+ case_before c
315
+
316
+ # -- given --
317
+ # nothing
318
+
319
+ # -- when/then --
320
+ if c[:init]
321
+ actual = c[:inputs].method(c[:method_name]).call(c[:init]) { |memo, first, second| eval c[:logic], binding }
322
+ else
323
+ actual = c[:inputs].method(c[:method_name]).call { |memo, first, second| eval c[:logic], binding }
324
+ end
325
+
326
+ expect(actual).to eq(c[:expected])
327
+ ensure
328
+ case_after c
329
+ end
330
+ end
331
+
332
+ def case_before(c)
333
+ # implement each case before
334
+ end
335
+
336
+ def case_after(c)
337
+ # implement each case after
338
+ end
339
+ end
340
+ end
341
+
342
+ context :together_concat do
343
+ cases = [
344
+ {
345
+ case_no: 1,
346
+ case_title: 'valid case',
347
+ inputs: [[1, 2, 3], %w{one two three}],
348
+ add_list: [4, 5, 6],
349
+ method_name: :together_concat,
350
+ expected: [[1, 2, 3, 4, 5, 6], ['one', 'two', 'three', 4, 5, 6]],
351
+ },
352
+ {
353
+ case_no: 2,
354
+ case_title: 'valid case',
355
+ inputs: [[1, 2, 3], %w{one two three}],
356
+ add_list: [4, 5, 6],
357
+ method_name: :tconcat,
358
+ expected: [[1, 2, 3, 4, 5, 6], ['one', 'two', 'three', 4, 5, 6]],
359
+ },
360
+ ]
361
+
362
+ cases.each do |c|
363
+ it "|case_no=#{c[:case_no]}|case_title=#{c[:case_title]}" do
364
+ begin
365
+ case_before c
366
+
367
+ # -- given --
368
+ # nothing
369
+
370
+ # -- when --
371
+ c[:inputs].send c[:method_name], c[:add_list]
372
+
373
+ # -- then --
374
+ expect(c[:inputs]).to eq(c[:expected])
375
+ ensure
376
+ case_after c
377
+ end
378
+ end
379
+
380
+ def case_before(c)
381
+ # implement each case before
382
+ end
383
+
384
+ def case_after(c)
385
+ # implement each case after
386
+ end
387
+ end
388
+ end
389
+
390
+ context :together_at do
391
+ cases = [
392
+ {
393
+ case_no: 1,
394
+ case_title: 'valid case',
395
+ inputs: [[1, 2, 3], %w{one two three}],
396
+ index: 2,
397
+ method_name: :together_at,
398
+ expected: [3, 'three'],
399
+ },
400
+ {
401
+ case_no: 2,
402
+ case_title: 'contain nil case',
403
+ inputs: [[1, 2, 3], %w{one two}],
404
+ index: 2,
405
+ method_name: :together_at,
406
+ expected: [3, nil],
407
+ },
408
+ {
409
+ case_no: 3,
410
+ case_title: 'valid case',
411
+ inputs: [[1, 2, 3], %w{one two three}],
412
+ index: 2,
413
+ method_name: :tat,
414
+ expected: [3, 'three'],
415
+ },
416
+ ]
417
+
418
+ cases.each do |c|
419
+ it "|case_no=#{c[:case_no]}|case_title=#{c[:case_title]}" do
420
+ begin
421
+ case_before c
422
+
423
+ # -- given --
424
+ # nothing
425
+
426
+ # -- when --
427
+ actual = c[:inputs].send c[:method_name], c[:index]
428
+
429
+ # -- then --
430
+ expect(actual).to eq(c[:expected])
431
+ ensure
432
+ case_after c
433
+ end
434
+ end
435
+
436
+ def case_before(c)
437
+ # implement each case before
438
+ end
439
+
440
+ def case_after(c)
441
+ # implement each case after
442
+ end
443
+ end
444
+ end
445
+ 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.19
4
+ version: 0.0.20
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-26 00:00:00.000000000 Z
12
+ date: 2014-01-27 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activesupport
16
- requirement: &21860136 !ruby/object:Gem::Requirement
16
+ requirement: &28991616 !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: *21860136
24
+ version_requirements: *28991616
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: bundler
27
- requirement: &21859524 !ruby/object:Gem::Requirement
27
+ requirement: &28991088 !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: *21859524
35
+ version_requirements: *28991088
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: rake
38
- requirement: &21859044 !ruby/object:Gem::Requirement
38
+ requirement: &28990692 !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: *21859044
46
+ version_requirements: *28990692
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: rspec
49
- requirement: &21858420 !ruby/object:Gem::Requirement
49
+ requirement: &28989852 !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: *21858420
57
+ version_requirements: *28989852
58
58
  - !ruby/object:Gem::Dependency
59
59
  name: simplecov
60
- requirement: &21857544 !ruby/object:Gem::Requirement
60
+ requirement: &29005188 !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: *21857544
68
+ version_requirements: *29005188
69
69
  description: Utilities
70
70
  email:
71
71
  - tbpgr@tbpgr.jp