merge_enum 0.4.0 → 0.6.0
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.
- checksums.yaml +4 -4
- data/README.md +49 -8
- data/lib/merge_enum/merge_enumerable.rb +98 -15
- data/lib/merge_enum/version.rb +1 -1
- data/spec/merge_enum/merge_enumerable_spec.rb +320 -17
- data/spec/spec_helper.rb +5 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a35388e5d97c57ace35fe87acd5d49240ed8e2ab
|
4
|
+
data.tar.gz: 3bfcd7d2e5134d5ef8130cda8b0b9096ed67acbf
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f67bf685781bce9dd9583cea2c706948cca6892ce88d578e7adfd025ca704c11384ed6c0d83413edab0a2b5e1cb5b2a030fb2460dbf5dfca90b6bbb9818c09ed
|
7
|
+
data.tar.gz: 9b7a441d6e75fcf7b683426cee580f7750766d7bfd8b2971255dcb82231f00cc4443272ce814189a12afce17217896ce20a5c8d8ed953041ed2a0f1c41b03916
|
data/README.md
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
# MergeEnum
|
2
|
-
###### ver 0.
|
2
|
+
###### ver 0.5.0
|
3
3
|
|
4
4
|
複数のEnumerale(*1)を連結(合成)するEnumerableです。
|
5
5
|
要素が必要になった時点で追加するように動作します。
|
@@ -42,32 +42,55 @@ Or install it yourself as:
|
|
42
42
|
> m_enum.count
|
43
43
|
=> 55
|
44
44
|
|
45
|
-
#### Enumerable#merge_enum
|
45
|
+
#### `Enumerable#merge_enum` から生成
|
46
46
|
|
47
47
|
> m_enum = (0...10).merge_enum(first: 13)
|
48
48
|
=> #<MergeEnum::MergeEnumerable:0x007fa...>
|
49
49
|
|
50
|
-
#### concat
|
50
|
+
#### `concat`, `concat!` で連結
|
51
51
|
|
52
52
|
> m_enum = (0...10).merge_enum(first: 13)
|
53
|
-
=> #<MergeEnum::MergeEnumerable:0x007fb...>
|
54
53
|
> m_enum = m_enum.concat(10...15)
|
55
|
-
=> #<MergeEnum::MergeEnumerable:0x008fb...>
|
56
54
|
> m_enum.to_a
|
57
55
|
=> [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
|
58
56
|
|
59
57
|
> m_enum = (0...10).merge_enum(first: 13)
|
60
|
-
=> #<MergeEnum::MergeEnumerable:0x007fc...>
|
61
58
|
> m_enum.concat!(10...15)
|
62
|
-
=> #<MergeEnum::MergeEnumerable:0x007fc...>
|
63
59
|
> m_enum.to_a
|
64
60
|
=> [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
|
65
61
|
|
62
|
+
#### `options`(alias: `merge_options`), `options!`(alias: `merge_options!`) でoptionsを追加
|
63
|
+
|
64
|
+
> m_enum = MergeEnum::MergeEnumerable.new(0...100, { first: 10, select: -> (c) { c.odd? } })
|
65
|
+
> m_enum = m_enum.merge_options({ first: 5 })
|
66
|
+
> m_enum.to_a
|
67
|
+
=> [1, 3, 5, 7, 9]
|
68
|
+
|
69
|
+
> m_enum = MergeEnum::MergeEnumerable.new(0...100, { first: 10, select: -> (c) { c.odd? } })
|
70
|
+
> m_enum.merge_options!({ first: 5 })
|
71
|
+
> m_enum.to_a
|
72
|
+
=> [1, 3, 5, 7, 9]
|
73
|
+
|
74
|
+
#### `replace_options`, `replace_options!` でoptionsを変更
|
75
|
+
|
76
|
+
> m_enum = MergeEnum::MergeEnumerable.new(0...100, { first: 10, select: -> (c) { c.odd? } })
|
77
|
+
> m_enum = m_enum.replace_options({ first: 5 })
|
78
|
+
> m_enum.to_a
|
79
|
+
=> [0, 1, 2, 3, 4]
|
80
|
+
|
81
|
+
> m_enum = MergeEnum::MergeEnumerable.new(0...100, { first: 10, select: -> (c) { c.odd? } })
|
82
|
+
> m_enum.replace_options!({ first: 5 })
|
83
|
+
> m_enum.to_a
|
84
|
+
=> [0, 1, 2, 3, 4]
|
85
|
+
|
66
86
|
### メソッド定義
|
67
87
|
|
68
88
|
* `MergeEnum::MergeEnumerable.new(enum_1, enum_2, ... , options = {})`
|
69
89
|
`enum_x` : Enumerable(*1)、もしくは、Enumerable(*1)を返すProcオブジェクト
|
70
|
-
`options` : ハッシュ形式のオプション
|
90
|
+
`options` : ハッシュ形式のオプション
|
91
|
+
- `:first` : `Integer`: 要素を取得する最大サイズ
|
92
|
+
- `:compact` : `Proc|bool`: nilを判定して除去する
|
93
|
+
- `:select` : `Proc`: nil,falseを返す要素を除去する
|
71
94
|
|
72
95
|
* `Enumerable#merge_enum(enum_2, enum_3, ... , options = {})`
|
73
96
|
レシーバ自身を`enum_1`として、`MergeEnumerable.new(enum_1, enum_2, enum_3, ... , options)`を返します
|
@@ -78,6 +101,24 @@ Or install it yourself as:
|
|
78
101
|
* `MergeEnumerable#concat!(enum)`
|
79
102
|
末尾に`enum`を追加し、`self`を返します
|
80
103
|
|
104
|
+
* `MergeEnumerable#options(opts)`
|
105
|
+
オプションに`opts`をマージした`MergeEnumerable`を生成して返します
|
106
|
+
|
107
|
+
* `MergeEnumerable#options!(opts)`
|
108
|
+
オプションに`opts`をマージし、`self`を返します
|
109
|
+
|
110
|
+
* `MergeEnumerable#merge_options(opts)`
|
111
|
+
`#options`のエイリアスです
|
112
|
+
|
113
|
+
* `MergeEnumerable#merge_options!(opts)`
|
114
|
+
`#options!`のエイリアスです
|
115
|
+
|
116
|
+
* `MergeEnumerable#replace_options(opts)`
|
117
|
+
オプションを`opts`に置き換えた`MergeEnumerable`を生成して返します
|
118
|
+
|
119
|
+
* `MergeEnumerable#replace_options!(opts)`
|
120
|
+
オプションを`opts`に置き換え、`self`を返します
|
121
|
+
|
81
122
|
## Contributing
|
82
123
|
|
83
124
|
1. Fork it ( http://github.com/<my-github-username>/merge_enum/fork )
|
@@ -17,14 +17,26 @@ module MergeEnum
|
|
17
17
|
def each &block
|
18
18
|
return self.to_enum unless block_given?
|
19
19
|
|
20
|
+
opt_cache = @options[:cache]
|
21
|
+
if opt_cache
|
22
|
+
if @cache
|
23
|
+
@cache.each &block
|
24
|
+
return self
|
25
|
+
end
|
26
|
+
elsif @cache
|
27
|
+
@cache = nil
|
28
|
+
end
|
29
|
+
|
20
30
|
# options
|
21
31
|
opt_fst = @options[:first]
|
22
32
|
opt_fst = opt_fst.to_i if opt_fst
|
23
33
|
(opt_proc, _proc) = merge_options_proc @options
|
34
|
+
opt_map = map_proc @options
|
24
35
|
|
25
36
|
# local variables
|
26
37
|
cnt = 0
|
27
38
|
fst = nil
|
39
|
+
cache = [] if opt_cache
|
28
40
|
|
29
41
|
@collections.each do |c|
|
30
42
|
if opt_fst
|
@@ -35,15 +47,20 @@ module MergeEnum
|
|
35
47
|
# get enumerable
|
36
48
|
called_first = false
|
37
49
|
if c.is_a? Proc
|
38
|
-
|
50
|
+
arity = c.arity.abs
|
51
|
+
case arity
|
39
52
|
when 0
|
40
53
|
c = c.call
|
41
54
|
when 1
|
42
55
|
c = c.call fst
|
43
56
|
called_first = true
|
44
|
-
|
57
|
+
when 2
|
45
58
|
c = c.call fst, opt_proc
|
46
59
|
called_first = true
|
60
|
+
else
|
61
|
+
args = Array.new arity - 2, nil
|
62
|
+
c = c.call fst, opt_proc, *args
|
63
|
+
called_first = true
|
47
64
|
end
|
48
65
|
end
|
49
66
|
|
@@ -52,20 +69,28 @@ module MergeEnum
|
|
52
69
|
c = c.first fst unless called_first or _proc
|
53
70
|
_cnt = 0
|
54
71
|
c.each do |e|
|
55
|
-
next if _proc and not opt_proc.call
|
72
|
+
next if _proc and not opt_proc.call(e, cnt)
|
73
|
+
e = opt_map.call(e, cnt)
|
56
74
|
block.call e
|
75
|
+
cache.push e if cache
|
76
|
+
cnt += 1
|
57
77
|
_cnt += 1
|
58
78
|
break if fst <= _cnt
|
59
79
|
end
|
60
|
-
cnt += _cnt
|
61
80
|
else
|
62
81
|
# without first option
|
63
82
|
c.each do |e|
|
64
|
-
next if _proc and not opt_proc.call
|
83
|
+
next if _proc and not opt_proc.call(e, cnt)
|
84
|
+
e = opt_map.call(e, cnt)
|
65
85
|
block.call e
|
86
|
+
cache.push e if cache
|
87
|
+
cnt += 1
|
66
88
|
end
|
67
89
|
end
|
68
90
|
end
|
91
|
+
|
92
|
+
@cache = cache if cache
|
93
|
+
|
69
94
|
self
|
70
95
|
end
|
71
96
|
|
@@ -76,36 +101,72 @@ module MergeEnum
|
|
76
101
|
|
77
102
|
opt_cmpct = options[:compact]
|
78
103
|
opts << if opt_cmpct.is_a? Proc
|
79
|
-
|
80
|
-
|
104
|
+
arity = opt_cmpct.arity.abs
|
105
|
+
case arity
|
106
|
+
when 0
|
107
|
+
[-> (e, i) { not opt_cmpct.call }, true]
|
108
|
+
when 1
|
109
|
+
[-> (e, i) { not opt_cmpct.call e }, true]
|
110
|
+
when 2
|
111
|
+
[-> (e, i) { not opt_cmpct.call e, i }, true]
|
81
112
|
else
|
82
|
-
|
113
|
+
args = Array.new arity - 2, nil
|
114
|
+
[-> (e, i) { not opt_cmpct.call e, i, *args }, true]
|
83
115
|
end
|
84
116
|
elsif opt_cmpct
|
85
|
-
[-> (e) { not e.nil? }, true]
|
117
|
+
[-> (e, i) { not e.nil? }, true]
|
86
118
|
else
|
87
|
-
[-> (e) { true }, false]
|
119
|
+
[-> (e, i) { true }, false]
|
88
120
|
end
|
89
121
|
|
90
122
|
opt_slct = options[:select]
|
91
123
|
opts << if opt_slct.is_a? Proc
|
92
|
-
|
93
|
-
|
124
|
+
arity = opt_slct.arity.abs
|
125
|
+
case arity
|
126
|
+
when 0
|
127
|
+
[-> (e, i) { opt_slct.call }, true]
|
128
|
+
when 1
|
129
|
+
[-> (e, i) { opt_slct.call e }, true]
|
130
|
+
when 2
|
131
|
+
[-> (e, i) { opt_slct.call e, i }, true]
|
94
132
|
else
|
95
|
-
|
133
|
+
args = Array.new arity - 2, nil
|
134
|
+
[-> (e, i) { opt_slct.call e, i, *args }, true]
|
96
135
|
end
|
97
136
|
elsif opt_slct
|
98
137
|
raise ":select is must be a Proc"
|
99
138
|
else
|
100
|
-
[-> (e) { true }, false]
|
139
|
+
[-> (e, i) { true }, false]
|
101
140
|
end
|
102
141
|
|
103
142
|
[
|
104
|
-
-> (e) { opts.all?{ |opt| opt[0].call e } },
|
143
|
+
-> (e, i) { opts.all?{ |opt| opt[0].call e, i } },
|
105
144
|
opts.any?{ |opt| opt[1] }
|
106
145
|
]
|
107
146
|
end
|
108
147
|
|
148
|
+
def map_proc options
|
149
|
+
opt_map = options[:map]
|
150
|
+
if opt_map
|
151
|
+
unless opt_map.is_a? Proc
|
152
|
+
raise ":map is must be a Proc"
|
153
|
+
end
|
154
|
+
case opt_map.arity
|
155
|
+
when 0
|
156
|
+
-> (e, i) { opt_map.call }
|
157
|
+
when 1
|
158
|
+
-> (e, i) { opt_map.call e }
|
159
|
+
when 2
|
160
|
+
opt_map
|
161
|
+
else
|
162
|
+
args = Array.new arity - 2, nil
|
163
|
+
-> (e, i) { opt_map.call e, i, *args }
|
164
|
+
end
|
165
|
+
else
|
166
|
+
-> (e, i) { e }
|
167
|
+
end
|
168
|
+
end
|
169
|
+
|
109
170
|
public
|
110
171
|
|
111
172
|
def concat arg
|
@@ -117,6 +178,28 @@ module MergeEnum
|
|
117
178
|
self
|
118
179
|
end
|
119
180
|
|
181
|
+
def merge_options opts
|
182
|
+
self.class.new *@collections, (@options.merge opts || {})
|
183
|
+
end
|
184
|
+
|
185
|
+
def merge_options! opts
|
186
|
+
@options.merge! opts || {}
|
187
|
+
self
|
188
|
+
end
|
189
|
+
|
190
|
+
def replace_options opts
|
191
|
+
self.class.new *@collections, opts || {}
|
192
|
+
end
|
193
|
+
|
194
|
+
def replace_options! opts
|
195
|
+
@options = opts || {}
|
196
|
+
self
|
197
|
+
end
|
198
|
+
|
199
|
+
alias_method :options, :merge_options
|
200
|
+
|
201
|
+
alias_method :options!, :merge_options!
|
202
|
+
|
120
203
|
end
|
121
204
|
|
122
205
|
end
|
data/lib/merge_enum/version.rb
CHANGED
@@ -608,12 +608,12 @@ describe MergeEnum::MergeEnumerable do
|
|
608
608
|
describe "50" do
|
609
609
|
let(:ary1) {
|
610
610
|
ary = Proc.new { |c| 0...100 }
|
611
|
-
|
611
|
+
allow(ary).to receive(:call).once.with(50).and_return(0...100)
|
612
612
|
ary
|
613
613
|
}
|
614
614
|
let(:ary2) {
|
615
615
|
ary = Proc.new { |c| 200...250 }
|
616
|
-
|
616
|
+
allow(ary).to receive(:call).never
|
617
617
|
ary
|
618
618
|
}
|
619
619
|
let(:first) { 50 }
|
@@ -623,7 +623,7 @@ describe MergeEnum::MergeEnumerable do
|
|
623
623
|
describe "130" do
|
624
624
|
let(:ary1) {
|
625
625
|
ary = Proc.new { |c| 0...100 }
|
626
|
-
|
626
|
+
allow(ary).to receive(:call).once.with(130).and_return(0...100)
|
627
627
|
ary
|
628
628
|
}
|
629
629
|
let(:first) { 130 }
|
@@ -631,7 +631,7 @@ describe MergeEnum::MergeEnumerable do
|
|
631
631
|
context "<all>" do
|
632
632
|
let(:ary2) {
|
633
633
|
ary = Proc.new { |c| 200...250 }
|
634
|
-
|
634
|
+
allow(ary).to receive(:call).once.with(30).and_return(200...250)
|
635
635
|
ary
|
636
636
|
}
|
637
637
|
test_equal self, :enum, (0...100).to_a + (200...230).to_a, line: __LINE__
|
@@ -640,7 +640,7 @@ describe MergeEnum::MergeEnumerable do
|
|
640
640
|
context "<interrupt>" do
|
641
641
|
let(:ary2) {
|
642
642
|
ary = Proc.new { |c| 200...250 }
|
643
|
-
|
643
|
+
allow(ary).to receive(:call).never
|
644
644
|
ary
|
645
645
|
}
|
646
646
|
test_interrupt self, :enum, (0...100).to_a + (200...230).to_a, line: __LINE__
|
@@ -650,7 +650,7 @@ describe MergeEnum::MergeEnumerable do
|
|
650
650
|
describe "200" do
|
651
651
|
let(:ary1) {
|
652
652
|
ary = Proc.new { |c| 0...100 }
|
653
|
-
|
653
|
+
allow(ary).to receive(:call).once.with(200).and_return(0...100)
|
654
654
|
ary
|
655
655
|
}
|
656
656
|
let(:ary2) { Proc.new { |c| 200...250 } }
|
@@ -687,12 +687,12 @@ describe MergeEnum::MergeEnumerable do
|
|
687
687
|
describe "50" do
|
688
688
|
let(:ary1) {
|
689
689
|
ary = -> (c) { 0...100 }
|
690
|
-
|
690
|
+
allow(ary).to receive(:call).once.with(50).and_return(0...100)
|
691
691
|
ary
|
692
692
|
}
|
693
693
|
let(:ary2) {
|
694
694
|
ary = -> (c) { 200...250 }
|
695
|
-
|
695
|
+
allow(ary).to receive(:call).never
|
696
696
|
ary
|
697
697
|
}
|
698
698
|
let(:first) { 50 }
|
@@ -702,7 +702,7 @@ describe MergeEnum::MergeEnumerable do
|
|
702
702
|
describe "130" do
|
703
703
|
let(:ary1) {
|
704
704
|
ary = -> (c) { 0...100 }
|
705
|
-
|
705
|
+
allow(ary).to receive(:call).once.with(130).and_return(0...100)
|
706
706
|
ary
|
707
707
|
}
|
708
708
|
let(:first) { 130 }
|
@@ -710,7 +710,7 @@ describe MergeEnum::MergeEnumerable do
|
|
710
710
|
context "<all>" do
|
711
711
|
let(:ary2) {
|
712
712
|
ary = -> (c) { 200...250 }
|
713
|
-
|
713
|
+
allow(ary).to receive(:call).once.with(30).and_return(200...250)
|
714
714
|
ary
|
715
715
|
}
|
716
716
|
test_equal self, :enum, (0...100).to_a + (200...230).to_a, line: __LINE__
|
@@ -719,7 +719,7 @@ describe MergeEnum::MergeEnumerable do
|
|
719
719
|
context "<interrupt>" do
|
720
720
|
let(:ary2) {
|
721
721
|
ary = -> (c) { 200...250 }
|
722
|
-
|
722
|
+
allow(ary).to receive(:call).never
|
723
723
|
ary
|
724
724
|
}
|
725
725
|
test_interrupt self, :enum, (0...100).to_a + (200...230).to_a, line: __LINE__
|
@@ -729,7 +729,7 @@ describe MergeEnum::MergeEnumerable do
|
|
729
729
|
describe "200" do
|
730
730
|
let(:ary1) {
|
731
731
|
ary = -> (c) { 0...100 }
|
732
|
-
|
732
|
+
allow(ary).to receive(:call).once.with(200).and_return(0...100)
|
733
733
|
ary
|
734
734
|
}
|
735
735
|
let(:ary2) { -> (c) { 200...250 } }
|
@@ -788,17 +788,17 @@ describe MergeEnum::MergeEnumerable do
|
|
788
788
|
describe "50" do
|
789
789
|
let(:ary1) {
|
790
790
|
ary = Proc.new { |c| 0...100 }
|
791
|
-
|
791
|
+
allow(ary).to receive(:call).once.with(50).and_return(0...100)
|
792
792
|
ary
|
793
793
|
}
|
794
794
|
let(:ary2) {
|
795
795
|
ary = Proc.new { |c| 200...250 }
|
796
|
-
|
796
|
+
allow(ary).to receive(:call).never
|
797
797
|
ary
|
798
798
|
}
|
799
799
|
let(:ary3) {
|
800
800
|
ary = Proc.new { |c| 300...330 }
|
801
|
-
|
801
|
+
allow(ary).to receive(:call).never
|
802
802
|
ary
|
803
803
|
}
|
804
804
|
let(:first) { 50 }
|
@@ -1046,12 +1046,65 @@ describe MergeEnum::MergeEnumerable do
|
|
1046
1046
|
end
|
1047
1047
|
end
|
1048
1048
|
|
1049
|
+
describe ".new" do
|
1050
|
+
describe "0...100, Array.new(10, nil), Proc.new { |c| 200...250 }, first: 130, compact: true, map: -> (e) { e * 2 }" do
|
1051
|
+
let(:enum) { Target.new 0...100, Array.new(10, nil), Proc.new { |c| 200...250 }, first: 130, compact: true, map: map }
|
1052
|
+
let(:map) {
|
1053
|
+
_proc = -> (e) { raise "using stub" }
|
1054
|
+
expect(_proc).to receive(:call).exactly(130).times do |e|
|
1055
|
+
e * 2
|
1056
|
+
end
|
1057
|
+
_proc
|
1058
|
+
}
|
1059
|
+
subject { enum.to_a }
|
1060
|
+
it {
|
1061
|
+
is_expected.to eq ((0...100).to_a + (200...230).to_a).map{ |e| e * 2 }
|
1062
|
+
}
|
1063
|
+
end
|
1064
|
+
end
|
1065
|
+
|
1066
|
+
describe ".new" do
|
1067
|
+
describe "-> { 0...100 }," do
|
1068
|
+
let(:enum) { Target.new ary, cache: cache }
|
1069
|
+
|
1070
|
+
describe "cache: false" do
|
1071
|
+
let(:cache) { false }
|
1072
|
+
let(:ary) {
|
1073
|
+
_proc = -> (e) { raise "using stub" }
|
1074
|
+
expect(_proc).to receive(:call).exactly(10).times.and_return 0...100
|
1075
|
+
_proc
|
1076
|
+
}
|
1077
|
+
it {
|
1078
|
+
10.times do
|
1079
|
+
enum.to_a
|
1080
|
+
end
|
1081
|
+
}
|
1082
|
+
end
|
1083
|
+
|
1084
|
+
describe "cache: true" do
|
1085
|
+
let(:cache) { true }
|
1086
|
+
let(:ary) {
|
1087
|
+
_proc = -> (e) { raise "using stub" }
|
1088
|
+
expect(_proc).to receive(:call).exactly(2).times.and_return 0...100
|
1089
|
+
_proc
|
1090
|
+
}
|
1091
|
+
it {
|
1092
|
+
10.times do
|
1093
|
+
enum.to_a
|
1094
|
+
end
|
1095
|
+
enum.options! cache: false
|
1096
|
+
enum.to_a
|
1097
|
+
}
|
1098
|
+
end
|
1099
|
+
end
|
1100
|
+
end
|
1101
|
+
|
1049
1102
|
describe ".new" do
|
1050
1103
|
describe "0...100, Proc.new { |c| 200...250 }, first: 160" do
|
1051
1104
|
let(:enum) { Target.new 0...100, Proc.new { |c| 200...250 }, first: 160 }
|
1052
1105
|
it { expect(enum).to be enum }
|
1053
1106
|
|
1054
|
-
|
1107
|
+
describe "#concat -> (c) { 300...330 }" do
|
1055
1108
|
it { expect(enum.concat []).not_to be enum }
|
1056
1109
|
it { expect(enum.concat([]).to_a).to eq enum.to_a }
|
1057
1110
|
|
@@ -1061,7 +1114,7 @@ describe MergeEnum::MergeEnumerable do
|
|
1061
1114
|
it { is_expected.to eq (0...100).to_a + (200...250).to_a + (300...310).to_a }
|
1062
1115
|
end
|
1063
1116
|
|
1064
|
-
|
1117
|
+
describe "#concat! -> (c) { 300...330 }" do
|
1065
1118
|
it { expect(enum.concat! [1, 2, 3]).to eq enum }
|
1066
1119
|
|
1067
1120
|
let(:ary) { -> (c) { 300...330 } }
|
@@ -1072,5 +1125,255 @@ describe MergeEnum::MergeEnumerable do
|
|
1072
1125
|
end
|
1073
1126
|
end
|
1074
1127
|
|
1128
|
+
describe ".new" do
|
1129
|
+
|
1130
|
+
def options enum
|
1131
|
+
enum.instance_eval { @options }
|
1132
|
+
end
|
1133
|
+
|
1134
|
+
describe "0...100, 200...250, 300...330" do
|
1135
|
+
let(:enum) { Target.new 0...100, 200...250, 300...330 }
|
1136
|
+
let(:ary) { (0...100).to_a + (200...250).to_a + (300...330).to_a }
|
1137
|
+
|
1138
|
+
context "<eq>" do
|
1139
|
+
subject { enum.to_a }
|
1140
|
+
it { is_expected.not_to be ary }
|
1141
|
+
it { is_expected.to eq ary }
|
1142
|
+
end
|
1143
|
+
|
1144
|
+
context "#merge_optons nil" do
|
1145
|
+
subject { enum.merge_options(nil).to_a }
|
1146
|
+
it { is_expected.to eq ary }
|
1147
|
+
end
|
1148
|
+
|
1149
|
+
context "#merge_optons! nil" do
|
1150
|
+
subject { enum.merge_options!(nil).to_a }
|
1151
|
+
it { is_expected.to eq ary }
|
1152
|
+
end
|
1153
|
+
|
1154
|
+
context "#replace_optons nil" do
|
1155
|
+
subject { enum.replace_options(nil).to_a }
|
1156
|
+
it { is_expected.to eq ary }
|
1157
|
+
end
|
1158
|
+
|
1159
|
+
context "#replace_optons! nil" do
|
1160
|
+
subject { enum.replace_options!(nil).to_a }
|
1161
|
+
it { is_expected.to eq ary }
|
1162
|
+
end
|
1163
|
+
|
1164
|
+
context "#merge_optons {}" do
|
1165
|
+
subject { enum.merge_options({}).to_a }
|
1166
|
+
it { is_expected.to eq ary }
|
1167
|
+
end
|
1168
|
+
|
1169
|
+
context "#merge_optons! {}" do
|
1170
|
+
subject { enum.merge_options!({}).to_a }
|
1171
|
+
it { is_expected.to eq ary }
|
1172
|
+
end
|
1173
|
+
|
1174
|
+
context "#replace_optons {}" do
|
1175
|
+
subject { enum.replace_options({}).to_a }
|
1176
|
+
it { is_expected.to eq ary }
|
1177
|
+
end
|
1178
|
+
|
1179
|
+
context "#replace_optons! {}" do
|
1180
|
+
subject { enum.replace_options!({}).to_a }
|
1181
|
+
it { is_expected.to eq ary }
|
1182
|
+
end
|
1183
|
+
|
1184
|
+
context "#merge_optons({ first: 160 })" do
|
1185
|
+
subject { enum.merge_options({ first: 160 }).to_a }
|
1186
|
+
it { is_expected.to eq ary[0...160] }
|
1187
|
+
end
|
1188
|
+
|
1189
|
+
context "#merge_optons!({ first: 160 })" do
|
1190
|
+
subject { enum.merge_options!({ first: 160 }).to_a }
|
1191
|
+
it { is_expected.to eq ary[0...160] }
|
1192
|
+
end
|
1193
|
+
|
1194
|
+
context "#replace_optons({ first: 160 })" do
|
1195
|
+
subject { enum.replace_options({ first: 160 }).to_a }
|
1196
|
+
it { is_expected.to eq ary[0...160] }
|
1197
|
+
end
|
1198
|
+
|
1199
|
+
context "#replace_optons!({ first: 160 })" do
|
1200
|
+
subject { enum.replace_options!({ first: 160 }).to_a }
|
1201
|
+
it { is_expected.to eq ary[0...160] }
|
1202
|
+
end
|
1203
|
+
end
|
1204
|
+
|
1205
|
+
describe "0...100, 200...250, 300...330, { first: 80 }" do
|
1206
|
+
let(:enum) { Target.new 0...100, 200...250, 300...330, { first: 80 } }
|
1207
|
+
let(:ary) { (0...100).to_a + (200...250).to_a + (300...330).to_a }
|
1208
|
+
|
1209
|
+
context "<eq>" do
|
1210
|
+
subject { enum.to_a }
|
1211
|
+
it { is_expected.to eq ary[0...80] }
|
1212
|
+
end
|
1213
|
+
|
1214
|
+
context "#merge_optons nil" do
|
1215
|
+
subject { enum.merge_options(nil).to_a }
|
1216
|
+
it { is_expected.to eq ary[0...80] }
|
1217
|
+
end
|
1218
|
+
|
1219
|
+
context "#merge_optons! nil" do
|
1220
|
+
subject { enum.merge_options!(nil).to_a }
|
1221
|
+
it { is_expected.to eq ary[0...80] }
|
1222
|
+
end
|
1223
|
+
|
1224
|
+
context "#replace_optons nil" do
|
1225
|
+
subject { enum.replace_options(nil).to_a }
|
1226
|
+
it { is_expected.to eq ary }
|
1227
|
+
end
|
1228
|
+
|
1229
|
+
context "#replace_optons! nil" do
|
1230
|
+
subject { enum.replace_options!(nil).to_a }
|
1231
|
+
it { is_expected.to eq ary }
|
1232
|
+
end
|
1233
|
+
|
1234
|
+
context "#merge_optons {}" do
|
1235
|
+
subject { enum.merge_options({}).to_a }
|
1236
|
+
it { is_expected.to eq ary[0...80] }
|
1237
|
+
end
|
1238
|
+
|
1239
|
+
context "#merge_optons! {}" do
|
1240
|
+
subject { enum.merge_options!({}).to_a }
|
1241
|
+
it { is_expected.to eq ary[0...80] }
|
1242
|
+
end
|
1243
|
+
|
1244
|
+
context "#replace_optons {}" do
|
1245
|
+
subject { enum.replace_options({}).to_a }
|
1246
|
+
it { is_expected.to eq ary }
|
1247
|
+
end
|
1248
|
+
|
1249
|
+
context "#replace_optons! {}" do
|
1250
|
+
subject { enum.replace_options!({}).to_a }
|
1251
|
+
it { is_expected.to eq ary }
|
1252
|
+
end
|
1253
|
+
|
1254
|
+
context "#merge_optons({ first: 160 })" do
|
1255
|
+
subject { enum.merge_options({ first: 160 }).to_a }
|
1256
|
+
it { is_expected.to eq ary[0...160] }
|
1257
|
+
end
|
1258
|
+
|
1259
|
+
context "#merge_optons!({ first: 160 })" do
|
1260
|
+
subject { enum.merge_options!({ first: 160 }).to_a }
|
1261
|
+
it { is_expected.to eq ary[0...160] }
|
1262
|
+
end
|
1263
|
+
|
1264
|
+
context "#replace_optons({ first: 160 })" do
|
1265
|
+
subject { enum.replace_options({ first: 160 }).to_a }
|
1266
|
+
it { is_expected.to eq ary[0...160] }
|
1267
|
+
end
|
1268
|
+
|
1269
|
+
context "#replace_optons!({ first: 160 })" do
|
1270
|
+
subject { enum.replace_options!({ first: 160 }).to_a }
|
1271
|
+
it { is_expected.to eq ary[0...160] }
|
1272
|
+
end
|
1273
|
+
end
|
1274
|
+
|
1275
|
+
context "<whitebox>" do
|
1276
|
+
[
|
1277
|
+
{}, { first: 100 }, { compact: true },
|
1278
|
+
{ hoge: "hoge", foo: "foo" }
|
1279
|
+
].each do |opts|
|
1280
|
+
describe "0...100, #{opts}" do
|
1281
|
+
let(:enum) { Target.new 0...100, opts }
|
1282
|
+
|
1283
|
+
context "<@options>" do
|
1284
|
+
subject { options enum }
|
1285
|
+
it { is_expected.to eq opts }
|
1286
|
+
end
|
1287
|
+
|
1288
|
+
[
|
1289
|
+
nil, {},
|
1290
|
+
{ first: 100 }, { compact: true },
|
1291
|
+
{ hoge: "hoge", foo: "foo" }
|
1292
|
+
].each do |opts_2|
|
1293
|
+
describe "#merge_options(#{opts_2})" do
|
1294
|
+
let(:enum_2) { enum.merge_options opts_2 }
|
1295
|
+
|
1296
|
+
context "<eq>" do
|
1297
|
+
subject { enum_2 }
|
1298
|
+
it { is_expected.not_to be enum }
|
1299
|
+
end
|
1300
|
+
|
1301
|
+
context "<@options>" do
|
1302
|
+
subject { options enum_2 }
|
1303
|
+
it { is_expected.not_to be opts }
|
1304
|
+
it { is_expected.not_to be opts_2 }
|
1305
|
+
it { is_expected.to eq (opts || {}).merge opts_2 || {} }
|
1306
|
+
end
|
1307
|
+
end
|
1308
|
+
|
1309
|
+
describe "#merge_options!(#{opts_2})" do
|
1310
|
+
let(:enum_2) { enum.merge_options! opts_2 }
|
1311
|
+
|
1312
|
+
context "<eq>" do
|
1313
|
+
subject { enum_2 }
|
1314
|
+
it { is_expected.to be enum }
|
1315
|
+
end
|
1316
|
+
|
1317
|
+
context "<@options>" do
|
1318
|
+
subject { options enum_2 }
|
1319
|
+
it { is_expected.to be opts }
|
1320
|
+
it { is_expected.not_to be opts_2 }
|
1321
|
+
it { is_expected.to eq (opts || {}).merge opts_2 || {} }
|
1322
|
+
end
|
1323
|
+
end
|
1324
|
+
|
1325
|
+
describe "#replace_options(#{opts_2})" do
|
1326
|
+
let(:enum_2) { enum.replace_options opts_2 }
|
1327
|
+
|
1328
|
+
context "<eq>" do
|
1329
|
+
subject { enum_2 }
|
1330
|
+
it { is_expected.not_to be enum }
|
1331
|
+
end
|
1332
|
+
|
1333
|
+
context "<@options>" do
|
1334
|
+
subject { options enum_2 }
|
1335
|
+
it { is_expected.not_to be opts }
|
1336
|
+
if opts_2
|
1337
|
+
it { is_expected.to be opts_2 }
|
1338
|
+
else
|
1339
|
+
it { is_expected.to eq({}) }
|
1340
|
+
end
|
1341
|
+
end
|
1342
|
+
end
|
1343
|
+
|
1344
|
+
describe "#replace_options!(#{opts_2})" do
|
1345
|
+
let(:enum_2) { enum.replace_options! opts_2 }
|
1346
|
+
|
1347
|
+
context "<eq>" do
|
1348
|
+
subject { enum_2 }
|
1349
|
+
it { is_expected.to be enum }
|
1350
|
+
end
|
1351
|
+
|
1352
|
+
context "<@options>" do
|
1353
|
+
subject { options enum_2 }
|
1354
|
+
it { is_expected.not_to be opts }
|
1355
|
+
if opts_2
|
1356
|
+
it { is_expected.to be opts_2 }
|
1357
|
+
else
|
1358
|
+
it { is_expected.to eq({}) }
|
1359
|
+
end
|
1360
|
+
end
|
1361
|
+
end
|
1362
|
+
end
|
1363
|
+
end
|
1364
|
+
end
|
1365
|
+
|
1366
|
+
context "<alias>" do
|
1367
|
+
[
|
1368
|
+
[:merge_options, :options], [:merge_options!, :options!]
|
1369
|
+
].each do |methods|
|
1370
|
+
context do
|
1371
|
+
subject { Target.instance_method methods[0] }
|
1372
|
+
it { is_expected.to eq Target.instance_method methods[1] }
|
1373
|
+
end
|
1374
|
+
end
|
1375
|
+
end
|
1376
|
+
end
|
1377
|
+
end
|
1075
1378
|
end
|
1076
1379
|
|
data/spec/spec_helper.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: merge_enum
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.6.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- brightgenerous
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-05-
|
11
|
+
date: 2014-05-30 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|