array_enumerator 0.0.9 → 0.0.10
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/Gemfile +4 -4
- data/Gemfile.lock +52 -41
- data/README.md +6 -0
- data/VERSION +1 -1
- data/array_enumerator.gemspec +16 -16
- data/lib/array_enumerator.rb +34 -17
- data/spec/array_enumerator_spec.rb +85 -31
- metadata +13 -13
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 717b631e00dfada746e1fb188835e71463d661ff
|
4
|
+
data.tar.gz: 295e77d737a45b2c44e89b84b27ef53a85c9a218
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: cd6b34c9b01b1db40506eb7daa8299291d501bc714e138d3c7d611e5c805d53395b9feaac593e52d5f6650320bbb8b730c0215a7421a21622931f6aae41bc364
|
7
|
+
data.tar.gz: ba4d6c33d2fe1c8909ea669a6af599232004100b4024cfbd2b5f7a073d091acb2b210de7c8283b903e218202bc383de3fe7b7ed4cbbb1733706a03400f4f9d03
|
data/Gemfile
CHANGED
@@ -6,10 +6,10 @@ source "http://rubygems.org"
|
|
6
6
|
# Add dependencies to develop your gem here.
|
7
7
|
# Include everything needed to run rake, tests, features, etc.
|
8
8
|
group :development do
|
9
|
-
gem "rspec", "~>
|
10
|
-
gem "rdoc", "~>
|
11
|
-
gem "bundler", "
|
12
|
-
gem "jeweler", "~>
|
9
|
+
gem "rspec", "~> 3.3.0"
|
10
|
+
gem "rdoc", "~> 4.2.0"
|
11
|
+
gem "bundler", "~> 1.9.2"
|
12
|
+
gem "jeweler", "~> 2.0.1"
|
13
13
|
end
|
14
14
|
|
15
15
|
gem "codeclimate-test-reporter", group: :test, require: nil
|
data/Gemfile.lock
CHANGED
@@ -1,69 +1,80 @@
|
|
1
1
|
GEM
|
2
2
|
remote: http://rubygems.org/
|
3
3
|
specs:
|
4
|
-
addressable (2.3.
|
4
|
+
addressable (2.3.8)
|
5
5
|
builder (3.2.2)
|
6
|
-
codeclimate-test-reporter (0.4.
|
6
|
+
codeclimate-test-reporter (0.4.7)
|
7
7
|
simplecov (>= 0.7.1, < 1.0.0)
|
8
|
-
|
8
|
+
descendants_tracker (0.0.4)
|
9
|
+
thread_safe (~> 0.3, >= 0.3.1)
|
10
|
+
diff-lcs (1.2.5)
|
9
11
|
docile (1.1.5)
|
10
|
-
faraday (0.
|
11
|
-
multipart-post (
|
12
|
-
git (1.2.
|
13
|
-
github_api (0.
|
14
|
-
addressable
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
12
|
+
faraday (0.9.1)
|
13
|
+
multipart-post (>= 1.2, < 3)
|
14
|
+
git (1.2.9.1)
|
15
|
+
github_api (0.12.3)
|
16
|
+
addressable (~> 2.3)
|
17
|
+
descendants_tracker (~> 0.0.4)
|
18
|
+
faraday (~> 0.8, < 0.10)
|
19
|
+
hashie (>= 3.3)
|
20
|
+
multi_json (>= 1.7.5, < 2.0)
|
21
|
+
nokogiri (~> 1.6.3)
|
19
22
|
oauth2
|
20
|
-
hashie (3.
|
21
|
-
highline (1.
|
22
|
-
jeweler (
|
23
|
+
hashie (3.4.2)
|
24
|
+
highline (1.7.2)
|
25
|
+
jeweler (2.0.1)
|
23
26
|
builder
|
24
|
-
bundler (
|
27
|
+
bundler (>= 1.0)
|
25
28
|
git (>= 1.2.5)
|
26
|
-
github_api
|
29
|
+
github_api
|
27
30
|
highline (>= 1.6.15)
|
28
|
-
nokogiri (
|
31
|
+
nokogiri (>= 1.5.10)
|
29
32
|
rake
|
30
33
|
rdoc
|
31
|
-
json (1.8.
|
32
|
-
jwt (1.
|
33
|
-
|
34
|
+
json (1.8.3)
|
35
|
+
jwt (1.5.1)
|
36
|
+
mini_portile (0.6.2)
|
37
|
+
multi_json (1.11.2)
|
34
38
|
multi_xml (0.5.5)
|
35
|
-
multipart-post (
|
36
|
-
nokogiri (1.
|
39
|
+
multipart-post (2.0.0)
|
40
|
+
nokogiri (1.6.6.2)
|
41
|
+
mini_portile (~> 0.6.0)
|
37
42
|
oauth2 (1.0.0)
|
38
43
|
faraday (>= 0.8, < 0.10)
|
39
44
|
jwt (~> 1.0)
|
40
45
|
multi_json (~> 1.3)
|
41
46
|
multi_xml (~> 0.5)
|
42
47
|
rack (~> 1.2)
|
43
|
-
rack (1.6.
|
48
|
+
rack (1.6.4)
|
44
49
|
rake (10.4.2)
|
45
|
-
rdoc (
|
50
|
+
rdoc (4.2.0)
|
46
51
|
json (~> 1.4)
|
47
|
-
rspec (
|
48
|
-
rspec-core (~>
|
49
|
-
rspec-expectations (~>
|
50
|
-
rspec-mocks (~>
|
51
|
-
rspec-core (
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
52
|
+
rspec (3.3.0)
|
53
|
+
rspec-core (~> 3.3.0)
|
54
|
+
rspec-expectations (~> 3.3.0)
|
55
|
+
rspec-mocks (~> 3.3.0)
|
56
|
+
rspec-core (3.3.1)
|
57
|
+
rspec-support (~> 3.3.0)
|
58
|
+
rspec-expectations (3.3.0)
|
59
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
60
|
+
rspec-support (~> 3.3.0)
|
61
|
+
rspec-mocks (3.3.1)
|
62
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
63
|
+
rspec-support (~> 3.3.0)
|
64
|
+
rspec-support (3.3.0)
|
65
|
+
simplecov (0.10.0)
|
56
66
|
docile (~> 1.1.0)
|
57
|
-
|
58
|
-
simplecov-html (~> 0.
|
59
|
-
simplecov-html (0.
|
67
|
+
json (~> 1.8)
|
68
|
+
simplecov-html (~> 0.10.0)
|
69
|
+
simplecov-html (0.10.0)
|
70
|
+
thread_safe (0.3.5)
|
60
71
|
|
61
72
|
PLATFORMS
|
62
73
|
ruby
|
63
74
|
|
64
75
|
DEPENDENCIES
|
65
|
-
bundler (
|
76
|
+
bundler (~> 1.9.2)
|
66
77
|
codeclimate-test-reporter
|
67
|
-
jeweler (~>
|
68
|
-
rdoc (~>
|
69
|
-
rspec (~>
|
78
|
+
jeweler (~> 2.0.1)
|
79
|
+
rdoc (~> 4.2.0)
|
80
|
+
rspec (~> 3.3.0)
|
data/README.md
CHANGED
@@ -38,12 +38,18 @@ a_enum = ArrayEnumerator.new(enum)
|
|
38
38
|
Call array-methods like you normally would:
|
39
39
|
```ruby
|
40
40
|
a_enum.empty? #=> false
|
41
|
+
a_enum.none? => false
|
42
|
+
a_enum.any? => true
|
41
43
|
a_enum.first #=> 1
|
42
44
|
a_enum.shift #=> 2
|
43
45
|
a_enum[2] #=> 3
|
44
46
|
a_enum.each_index { |count| puts "Count: #{count}" }
|
45
47
|
a_enum.length #=> 3
|
46
48
|
a_enum.select { |element| element.to_f }.to_a #=> [0.0, 1.0, 2.0 etc]
|
49
|
+
a_enum << 1001 # push also works
|
50
|
+
a_enum.unshift -1
|
51
|
+
a_enum.min #=> 0
|
52
|
+
a_enum.max #=> 999
|
47
53
|
```
|
48
54
|
|
49
55
|
### Collect
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.0.
|
1
|
+
0.0.10
|
data/array_enumerator.gemspec
CHANGED
@@ -2,16 +2,16 @@
|
|
2
2
|
# DO NOT EDIT THIS FILE DIRECTLY
|
3
3
|
# Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
|
4
4
|
# -*- encoding: utf-8 -*-
|
5
|
-
# stub: array_enumerator 0.0.
|
5
|
+
# stub: array_enumerator 0.0.10 ruby lib
|
6
6
|
|
7
7
|
Gem::Specification.new do |s|
|
8
8
|
s.name = "array_enumerator"
|
9
|
-
s.version = "0.0.
|
9
|
+
s.version = "0.0.10"
|
10
10
|
|
11
11
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
12
12
|
s.require_paths = ["lib"]
|
13
13
|
s.authors = ["Kasper Johansen"]
|
14
|
-
s.date = "2015-07-
|
14
|
+
s.date = "2015-07-05"
|
15
15
|
s.description = "Enumerator abstraction layer that emulates certain array functionality (methods like empty?, slice, shift and more) by using a small cache and other tricks without loading all the data from the enumerator at the same time."
|
16
16
|
s.email = "k@spernj.org"
|
17
17
|
s.extra_rdoc_files = [
|
@@ -35,28 +35,28 @@ Gem::Specification.new do |s|
|
|
35
35
|
]
|
36
36
|
s.homepage = "http://github.com/kaspernj/array_enumerator"
|
37
37
|
s.licenses = ["MIT"]
|
38
|
-
s.rubygems_version = "2.
|
38
|
+
s.rubygems_version = "2.4.0"
|
39
39
|
s.summary = "Enumerator abstraction layer that emulates certain array functionality (methods)."
|
40
40
|
|
41
41
|
if s.respond_to? :specification_version then
|
42
42
|
s.specification_version = 4
|
43
43
|
|
44
44
|
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
45
|
-
s.add_development_dependency(%q<rspec>, ["~>
|
46
|
-
s.add_development_dependency(%q<rdoc>, ["~>
|
47
|
-
s.add_development_dependency(%q<bundler>, ["
|
48
|
-
s.add_development_dependency(%q<jeweler>, ["~>
|
45
|
+
s.add_development_dependency(%q<rspec>, ["~> 3.3.0"])
|
46
|
+
s.add_development_dependency(%q<rdoc>, ["~> 4.2.0"])
|
47
|
+
s.add_development_dependency(%q<bundler>, ["~> 1.9.2"])
|
48
|
+
s.add_development_dependency(%q<jeweler>, ["~> 2.0.1"])
|
49
49
|
else
|
50
|
-
s.add_dependency(%q<rspec>, ["~>
|
51
|
-
s.add_dependency(%q<rdoc>, ["~>
|
52
|
-
s.add_dependency(%q<bundler>, ["
|
53
|
-
s.add_dependency(%q<jeweler>, ["~>
|
50
|
+
s.add_dependency(%q<rspec>, ["~> 3.3.0"])
|
51
|
+
s.add_dependency(%q<rdoc>, ["~> 4.2.0"])
|
52
|
+
s.add_dependency(%q<bundler>, ["~> 1.9.2"])
|
53
|
+
s.add_dependency(%q<jeweler>, ["~> 2.0.1"])
|
54
54
|
end
|
55
55
|
else
|
56
|
-
s.add_dependency(%q<rspec>, ["~>
|
57
|
-
s.add_dependency(%q<rdoc>, ["~>
|
58
|
-
s.add_dependency(%q<bundler>, ["
|
59
|
-
s.add_dependency(%q<jeweler>, ["~>
|
56
|
+
s.add_dependency(%q<rspec>, ["~> 3.3.0"])
|
57
|
+
s.add_dependency(%q<rdoc>, ["~> 4.2.0"])
|
58
|
+
s.add_dependency(%q<bundler>, ["~> 1.9.2"])
|
59
|
+
s.add_dependency(%q<jeweler>, ["~> 2.0.1"])
|
60
60
|
end
|
61
61
|
end
|
62
62
|
|
data/lib/array_enumerator.rb
CHANGED
@@ -3,6 +3,8 @@ class ArrayEnumerator
|
|
3
3
|
class ArrayCorruptedError < RuntimeError; end
|
4
4
|
class CannotCallBeforeEnd < RuntimeError; end
|
5
5
|
|
6
|
+
include Enumerable
|
7
|
+
|
6
8
|
# Takes an enumerator to work with as argument.
|
7
9
|
def initialize(enum = nil, &blk)
|
8
10
|
if enum
|
@@ -16,6 +18,9 @@ class ArrayEnumerator
|
|
16
18
|
raise "No enum or block was given."
|
17
19
|
end
|
18
20
|
|
21
|
+
@eles = []
|
22
|
+
@end_eles = []
|
23
|
+
|
19
24
|
# Used to calculate length without depending corruption.
|
20
25
|
@length_cache = 0
|
21
26
|
|
@@ -26,17 +31,31 @@ class ArrayEnumerator
|
|
26
31
|
@mutex = Mutex.new
|
27
32
|
end
|
28
33
|
|
34
|
+
def push(object)
|
35
|
+
raise ArrayCorruptedError if @end
|
36
|
+
@end_eles << object
|
37
|
+
end
|
38
|
+
|
39
|
+
def <<(object)
|
40
|
+
push(object)
|
41
|
+
end
|
42
|
+
|
43
|
+
def unshift(object)
|
44
|
+
check_corrupted
|
45
|
+
@eles << object
|
46
|
+
end
|
47
|
+
|
29
48
|
# Cache the first elements (if not cached already) and returns it.
|
30
49
|
def first
|
31
50
|
check_corrupted
|
32
|
-
cache_ele if
|
51
|
+
cache_ele if @eles.empty?
|
33
52
|
return @eles.first
|
34
53
|
end
|
35
54
|
|
36
|
-
# Returns true if the array is empty.
|
55
|
+
# Returns true if the array-enumerator is empty.
|
37
56
|
def empty?
|
38
57
|
if @empty == nil
|
39
|
-
cache_ele if
|
58
|
+
cache_ele if @length_cache == 0
|
40
59
|
|
41
60
|
if @length_cache > 0
|
42
61
|
@empty = false
|
@@ -109,21 +128,23 @@ class ArrayEnumerator
|
|
109
128
|
end
|
110
129
|
end
|
111
130
|
|
112
|
-
# Returns a enumerator that can yield
|
131
|
+
# Returns a enumerator that can yield all the elements (both cached and future un-cached ones).
|
113
132
|
def to_enum
|
114
133
|
check_corrupted
|
115
134
|
@array_corrupted = true
|
116
135
|
|
117
136
|
return Enumerator.new do |yielder|
|
118
|
-
|
119
|
-
|
120
|
-
yielder << ele
|
121
|
-
end
|
137
|
+
while ele = @eles.shift
|
138
|
+
yielder << ele
|
122
139
|
end
|
123
140
|
|
124
141
|
yield_rest do |ele|
|
125
142
|
yielder << ele
|
126
143
|
end
|
144
|
+
|
145
|
+
while ele = @end_eles.shift
|
146
|
+
yielder << ele
|
147
|
+
end
|
127
148
|
end
|
128
149
|
end
|
129
150
|
|
@@ -136,7 +157,7 @@ class ArrayEnumerator
|
|
136
157
|
def select
|
137
158
|
return ArrayEnumerator.new do |y|
|
138
159
|
check_corrupted
|
139
|
-
|
160
|
+
each do |element|
|
140
161
|
y << element if yield(element)
|
141
162
|
end
|
142
163
|
end
|
@@ -165,9 +186,9 @@ class ArrayEnumerator
|
|
165
186
|
elsif args[0] && args[1] && args[0] > 0 && args[1] > 0
|
166
187
|
need_eles = args[0] + args[1]
|
167
188
|
elsif args[0] < 0 || args[1] < 0
|
168
|
-
raise "Slice cant take negative arguments."
|
189
|
+
raise ArgumentError, "Slice cant take negative arguments."
|
169
190
|
else
|
170
|
-
raise "Dont now what to do with args: '#{args}'."
|
191
|
+
raise ArgumentError, "Dont now what to do with args: '#{args}'."
|
171
192
|
end
|
172
193
|
|
173
194
|
@eles ||= []
|
@@ -187,7 +208,7 @@ class ArrayEnumerator
|
|
187
208
|
end
|
188
209
|
|
189
210
|
@eles ||= []
|
190
|
-
cache_ele(amount - @eles.length) if
|
211
|
+
cache_ele(amount - @eles.length) if @eles.length < amount
|
191
212
|
res = @eles.shift(*args)
|
192
213
|
|
193
214
|
# Since we are removing an element, the length should go down with the amount of elements captured.
|
@@ -204,7 +225,7 @@ class ArrayEnumerator
|
|
204
225
|
check_corrupted
|
205
226
|
|
206
227
|
return ArrayEnumerator.new do |y|
|
207
|
-
|
228
|
+
each do |element|
|
208
229
|
y << yield(element)
|
209
230
|
end
|
210
231
|
end
|
@@ -255,8 +276,6 @@ private
|
|
255
276
|
|
256
277
|
# Caches a given amount of elements.
|
257
278
|
def cache_ele(amount = 1)
|
258
|
-
@eles ||= []
|
259
|
-
|
260
279
|
begin
|
261
280
|
@mutex.synchronize do
|
262
281
|
while @eles.length <= amount
|
@@ -271,8 +290,6 @@ private
|
|
271
290
|
|
272
291
|
# Forces the rest of the elements to be cached.
|
273
292
|
def cache_all
|
274
|
-
@eles ||= []
|
275
|
-
|
276
293
|
begin
|
277
294
|
@mutex.synchronize do
|
278
295
|
while ele = @enum.next
|
@@ -17,15 +17,15 @@ describe "ArrayEnumerator" do
|
|
17
17
|
end
|
18
18
|
|
19
19
|
3.times do |count|
|
20
|
-
enum.shift.
|
20
|
+
expect(enum.shift).to eq count
|
21
21
|
end
|
22
22
|
end
|
23
23
|
|
24
24
|
it "#shift" do
|
25
25
|
cont = %w[a b c d e].to_enum
|
26
26
|
ae = ArrayEnumerator.new(cont)
|
27
|
-
ae.shift.
|
28
|
-
ae.to_a.
|
27
|
+
expect(ae.shift).to eq "a"
|
28
|
+
expect(ae.to_a).to eq %w[b c d e]
|
29
29
|
end
|
30
30
|
|
31
31
|
describe "#each" do
|
@@ -36,11 +36,11 @@ describe "ArrayEnumerator" do
|
|
36
36
|
|
37
37
|
count = 0
|
38
38
|
ae.each do |ele|
|
39
|
-
ele.
|
39
|
+
expect(ele).to eq arr[count]
|
40
40
|
count += 1
|
41
41
|
end
|
42
42
|
|
43
|
-
count.
|
43
|
+
expect(count).to eq 3
|
44
44
|
end
|
45
45
|
end
|
46
46
|
|
@@ -50,33 +50,31 @@ describe "ArrayEnumerator" do
|
|
50
50
|
cont = arr.to_enum
|
51
51
|
ae = ArrayEnumerator.new(cont)
|
52
52
|
|
53
|
-
ae.empty
|
54
|
-
ae.none
|
55
|
-
ae.any
|
53
|
+
expect(ae.empty?).to eq false
|
54
|
+
expect(ae.none?).to eq false
|
55
|
+
expect(ae.any?).to eq true
|
56
56
|
|
57
57
|
count = 0
|
58
58
|
ae.each do |ele|
|
59
|
-
ele.
|
59
|
+
expect(ele).to eq arr[count]
|
60
60
|
count += 1
|
61
61
|
end
|
62
62
|
|
63
|
-
count.
|
63
|
+
expect(count).to eq 3
|
64
64
|
|
65
65
|
expect { ae.to_a }.to raise_error(ArrayEnumerator::ArrayCorruptedError)
|
66
66
|
expect { ae.first }.to raise_error(ArrayEnumerator::ArrayCorruptedError)
|
67
67
|
|
68
|
-
expect {
|
69
|
-
ae.each { |ele| raise "Should never get here?" }
|
70
|
-
}.to raise_error(ArrayEnumerator::ArrayCorruptedError)
|
68
|
+
expect { ae.each { |ele| raise "Should never get here?" } }.to raise_error(ArrayEnumerator::ArrayCorruptedError)
|
71
69
|
end
|
72
70
|
|
73
71
|
it "still be able to get the first element after testing if empty" do
|
74
72
|
cont = %w[a b c].to_enum
|
75
73
|
ae = ArrayEnumerator.new(cont)
|
76
|
-
ae.empty
|
77
|
-
ae.none
|
78
|
-
ae.any
|
79
|
-
ae.first.
|
74
|
+
expect(ae.empty?).to eq false
|
75
|
+
expect(ae.none?).to eq false
|
76
|
+
expect(ae.any?).to eq true
|
77
|
+
expect(ae.first).to eq "a"
|
80
78
|
end
|
81
79
|
end
|
82
80
|
|
@@ -90,7 +88,7 @@ describe "ArrayEnumerator" do
|
|
90
88
|
# ignore.
|
91
89
|
end
|
92
90
|
|
93
|
-
ae.length.
|
91
|
+
expect(ae.length).to eq 5
|
94
92
|
end
|
95
93
|
|
96
94
|
it "#slice" do
|
@@ -110,14 +108,14 @@ describe "ArrayEnumerator" do
|
|
110
108
|
]
|
111
109
|
|
112
110
|
runs.each do |args|
|
113
|
-
arr.slice(*args).
|
111
|
+
expect(arr.slice(*args)).to eq ae.slice(*args)
|
114
112
|
end
|
115
113
|
|
116
114
|
fails.each do |args|
|
117
115
|
expect {
|
118
116
|
res2 = ae.slice(*args)
|
119
117
|
raise "Should have failed but didnt."
|
120
|
-
}.to raise_error
|
118
|
+
}.to raise_error(ArgumentError)
|
121
119
|
end
|
122
120
|
end
|
123
121
|
|
@@ -127,38 +125,41 @@ describe "ArrayEnumerator" do
|
|
127
125
|
|
128
126
|
expect = 0
|
129
127
|
ae.each_index do |num|
|
130
|
-
num.
|
131
|
-
ae[num].
|
128
|
+
expect(num).to eq expect
|
129
|
+
expect(ae[num]).to eq arr[num]
|
132
130
|
expect += 1
|
133
131
|
end
|
134
132
|
end
|
135
133
|
|
136
134
|
it "#select" do
|
137
|
-
result = a_enum_10.select { |element| element == 5 || element == 7 }
|
138
|
-
result.
|
135
|
+
result = a_enum_10.select { |element| element == 5 || element == 7 }
|
136
|
+
expect(result.class).to eq ArrayEnumerator
|
137
|
+
expect(result.to_a).to eq [5, 7]
|
139
138
|
end
|
140
139
|
|
141
140
|
it "#reject" do
|
142
|
-
result = a_enum_10.reject { |element| element == 5 || element == 7}
|
143
|
-
result.
|
141
|
+
result = a_enum_10.reject { |element| element == 5 || element == 7}
|
142
|
+
expect(result.class).to eq ArrayEnumerator
|
143
|
+
expect(result.to_a).to eq [0, 1, 2, 3, 4, 6, 8, 9]
|
144
144
|
end
|
145
145
|
|
146
146
|
it "#compact" do
|
147
147
|
a_enum = ArrayEnumerator.new([0, nil, 1, 2, 3, 4, nil, 5].to_enum)
|
148
|
-
a_enum.compact.to_a.
|
148
|
+
expect(a_enum.compact.to_a).to eq [0, 1, 2, 3, 4, 5]
|
149
149
|
end
|
150
150
|
|
151
151
|
describe "#collect" do
|
152
152
|
it "should return a new enumerator yielding the new values one by one" do
|
153
153
|
collected_a_enum = a_enum_10.collect { |element| element + 1000 }
|
154
|
+
expect(collected_a_enum.class).to eq ArrayEnumerator
|
154
155
|
|
155
156
|
count = 0
|
156
157
|
collected_a_enum.each do |number|
|
157
|
-
number.
|
158
|
+
expect(number).to eq (count + 1000)
|
158
159
|
count += 1
|
159
160
|
end
|
160
161
|
|
161
|
-
count.
|
162
|
+
expect(count).to eq 10
|
162
163
|
end
|
163
164
|
|
164
165
|
it "should work with map and block-symbols" do
|
@@ -166,11 +167,64 @@ describe "ArrayEnumerator" do
|
|
166
167
|
|
167
168
|
count = 0
|
168
169
|
collected_a_enum.each do |number|
|
169
|
-
number.
|
170
|
+
expect(number).to eq count.to_f
|
171
|
+
count += 1
|
172
|
+
end
|
173
|
+
|
174
|
+
expect(count).to eq 10
|
175
|
+
end
|
176
|
+
end
|
177
|
+
|
178
|
+
describe '#push' do
|
179
|
+
it 'adds extra objects' do
|
180
|
+
a_enum_10.push 10
|
181
|
+
|
182
|
+
count = 0
|
183
|
+
a_enum_10.each do |number|
|
184
|
+
expect(count).to eq number
|
185
|
+
count += 1
|
186
|
+
end
|
187
|
+
|
188
|
+
expect(count).to eq 11
|
189
|
+
end
|
190
|
+
|
191
|
+
it 'doesnt allow push after end has been reached' do
|
192
|
+
a_enum_10.each do
|
193
|
+
# do nothing
|
194
|
+
end
|
195
|
+
|
196
|
+
expect { a_enum_10 << 10 }.to raise_error(ArrayEnumerator::ArrayCorruptedError)
|
197
|
+
end
|
198
|
+
end
|
199
|
+
|
200
|
+
describe '#unshift' do
|
201
|
+
it 'prepends objects' do
|
202
|
+
a_enum_10.unshift -1
|
203
|
+
|
204
|
+
count = -1
|
205
|
+
a_enum_10.each do |number|
|
206
|
+
expect(count).to eq number
|
170
207
|
count += 1
|
171
208
|
end
|
172
209
|
|
173
|
-
count.
|
210
|
+
expect(count).to eq 10
|
211
|
+
end
|
212
|
+
|
213
|
+
it 'doesnt allow unshift after starting iteration' do
|
214
|
+
a_enum_10.each { }
|
215
|
+
expect { a_enum_10.unshift -1 }.to raise_error(ArrayEnumerator::ArrayCorruptedError)
|
174
216
|
end
|
175
217
|
end
|
218
|
+
|
219
|
+
it '#max' do
|
220
|
+
expect(a_enum_10.max).to eq 9
|
221
|
+
end
|
222
|
+
|
223
|
+
it '#min' do
|
224
|
+
expect(a_enum_10.min).to eq 0
|
225
|
+
end
|
226
|
+
|
227
|
+
it '#minmax' do
|
228
|
+
expect(a_enum_10.minmax).to eq [0, 9]
|
229
|
+
end
|
176
230
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: array_enumerator
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.10
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Kasper Johansen
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-07-
|
11
|
+
date: 2015-07-05 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rspec
|
@@ -16,56 +16,56 @@ dependencies:
|
|
16
16
|
requirements:
|
17
17
|
- - "~>"
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version:
|
19
|
+
version: 3.3.0
|
20
20
|
type: :development
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
24
|
- - "~>"
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version:
|
26
|
+
version: 3.3.0
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: rdoc
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
31
|
- - "~>"
|
32
32
|
- !ruby/object:Gem::Version
|
33
|
-
version:
|
33
|
+
version: 4.2.0
|
34
34
|
type: :development
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
38
|
- - "~>"
|
39
39
|
- !ruby/object:Gem::Version
|
40
|
-
version:
|
40
|
+
version: 4.2.0
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
42
|
name: bundler
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
44
44
|
requirements:
|
45
|
-
- - "
|
45
|
+
- - "~>"
|
46
46
|
- !ruby/object:Gem::Version
|
47
|
-
version: 1.
|
47
|
+
version: 1.9.2
|
48
48
|
type: :development
|
49
49
|
prerelease: false
|
50
50
|
version_requirements: !ruby/object:Gem::Requirement
|
51
51
|
requirements:
|
52
|
-
- - "
|
52
|
+
- - "~>"
|
53
53
|
- !ruby/object:Gem::Version
|
54
|
-
version: 1.
|
54
|
+
version: 1.9.2
|
55
55
|
- !ruby/object:Gem::Dependency
|
56
56
|
name: jeweler
|
57
57
|
requirement: !ruby/object:Gem::Requirement
|
58
58
|
requirements:
|
59
59
|
- - "~>"
|
60
60
|
- !ruby/object:Gem::Version
|
61
|
-
version:
|
61
|
+
version: 2.0.1
|
62
62
|
type: :development
|
63
63
|
prerelease: false
|
64
64
|
version_requirements: !ruby/object:Gem::Requirement
|
65
65
|
requirements:
|
66
66
|
- - "~>"
|
67
67
|
- !ruby/object:Gem::Version
|
68
|
-
version:
|
68
|
+
version: 2.0.1
|
69
69
|
description: Enumerator abstraction layer that emulates certain array functionality
|
70
70
|
(methods like empty?, slice, shift and more) by using a small cache and other tricks
|
71
71
|
without loading all the data from the enumerator at the same time.
|
@@ -109,7 +109,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
109
109
|
version: '0'
|
110
110
|
requirements: []
|
111
111
|
rubyforge_project:
|
112
|
-
rubygems_version: 2.
|
112
|
+
rubygems_version: 2.4.0
|
113
113
|
signing_key:
|
114
114
|
specification_version: 4
|
115
115
|
summary: Enumerator abstraction layer that emulates certain array functionality (methods).
|