look_ahead_iterator 0.0.1
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 +7 -0
- data/.gitignore +55 -0
- data/.travis.yml +7 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +36 -0
- data/Rakefile +22 -0
- data/lib/look_ahead_iterator.rb +110 -0
- data/lib/look_ahead_iterator/version.rb +3 -0
- data/look_ahead_iterator.gemspec +24 -0
- data/test/minitest_helper.rb +4 -0
- data/test/test_look_ahead_iterator.rb +261 -0
- metadata +99 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 8d1df7b4de99de92d267fde8715c983bed263b07
|
4
|
+
data.tar.gz: 2ef9bb0c40a1cac6e1171d12e1026a2061cca87a
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: daad525f977bbba31418be79091d408a01172eaeb153a592ab2de24ca992fa1f54b28129d7fcd096b9085433db584b30f104710be32ea83520f6144f28fe9176
|
7
|
+
data.tar.gz: 7d5b8b95cc8d9cadc7e5e9a302616aad2ed0c5d63bd603449a7881972115a543f6cc69af3c64559bc82c87542de7ca3ce194c46e157b4e97d91890ad5fea1319
|
data/.gitignore
ADDED
@@ -0,0 +1,55 @@
|
|
1
|
+
*.gem
|
2
|
+
*.rbc
|
3
|
+
.bundle
|
4
|
+
.config
|
5
|
+
.yardoc
|
6
|
+
Gemfile.lock
|
7
|
+
InstalledFiles
|
8
|
+
_yardoc
|
9
|
+
coverage
|
10
|
+
doc/
|
11
|
+
lib/bundler/man
|
12
|
+
pkg
|
13
|
+
rdoc
|
14
|
+
spec/reports
|
15
|
+
test/tmp
|
16
|
+
test/version_tmp
|
17
|
+
tmp
|
18
|
+
*.bundle
|
19
|
+
*.so
|
20
|
+
*.o
|
21
|
+
*.a
|
22
|
+
mkmf.log
|
23
|
+
/.config
|
24
|
+
/coverage/
|
25
|
+
/InstalledFiles
|
26
|
+
/pkg/
|
27
|
+
/spec/reports/
|
28
|
+
/test/tmp/
|
29
|
+
/test/version_tmp/
|
30
|
+
/tmp/
|
31
|
+
|
32
|
+
## Specific to RubyMotion:
|
33
|
+
.dat*
|
34
|
+
.repl_history
|
35
|
+
build/
|
36
|
+
|
37
|
+
## Documentation cache and generated files:
|
38
|
+
/.yardoc/
|
39
|
+
/_yardoc/
|
40
|
+
/doc/
|
41
|
+
/rdoc/
|
42
|
+
|
43
|
+
## Environment normalisation:
|
44
|
+
/.bundle/
|
45
|
+
/vendor/bundle
|
46
|
+
/lib/bundler/man/
|
47
|
+
|
48
|
+
# for a library or gem, you might want to ignore these files since the code is
|
49
|
+
# intended to run in multiple environments; otherwise, check them in:
|
50
|
+
# Gemfile.lock
|
51
|
+
# .ruby-version
|
52
|
+
# .ruby-gemset
|
53
|
+
|
54
|
+
# unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
|
55
|
+
.rvmrc
|
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2015 Javier Goizueta
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
# LookAheadIterator
|
2
|
+
|
3
|
+
Ruby iterator with look ahead operations.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'look_ahead_iterator'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install look_ahead_iterator
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
```ruby
|
22
|
+
require 'look_ahead_iterator'
|
23
|
+
i = LookAheadIterator.new((1..4), stop: true)
|
24
|
+
loop do
|
25
|
+
puts "Current_value: #{i.next.inspect}"
|
26
|
+
puts " (next value: #{i.look_ahead.inspect})"
|
27
|
+
end
|
28
|
+
```
|
29
|
+
|
30
|
+
## Contributing
|
31
|
+
|
32
|
+
1. Fork it ( https://github.com/jgoizueta/look_ahead_iterator/fork )
|
33
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
34
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
35
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
36
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
2
|
+
|
3
|
+
require 'rake/testtask'
|
4
|
+
Rake::TestTask.new(:test) do |test|
|
5
|
+
test.libs << 'lib' << 'test'
|
6
|
+
test.pattern = 'test/**/test_*.rb'
|
7
|
+
test.verbose = true
|
8
|
+
end
|
9
|
+
|
10
|
+
require 'rdoc/task'
|
11
|
+
Rake::RDocTask.new do |rdoc|
|
12
|
+
version = LookAheadIterator::VERSION
|
13
|
+
|
14
|
+
rdoc.rdoc_dir = 'rdoc'
|
15
|
+
rdoc.title = "LookAheadIterator #{version}"
|
16
|
+
rdoc.main = "README.md"
|
17
|
+
rdoc.rdoc_files.include('README*')
|
18
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
19
|
+
rdoc.markup = 'markdown' if rdoc.respond_to?(:markup)
|
20
|
+
end
|
21
|
+
|
22
|
+
task :default => :test
|
@@ -0,0 +1,110 @@
|
|
1
|
+
require "look_ahead_iterator/version"
|
2
|
+
|
3
|
+
class LookAheadIterator
|
4
|
+
|
5
|
+
# A LookAheadIterator can be created from any enumerable object.
|
6
|
+
#
|
7
|
+
# Options:
|
8
|
+
#
|
9
|
+
# * :stop : If true, the iteration will stop by throwing +StopIteration+
|
10
|
+
# like other external iterators. This will end a +loop+.
|
11
|
+
# Otherwise the user must check of end of iteration by
|
12
|
+
# * :end : is used to define a special value to be returned
|
13
|
+
# if the iteration has ended (or when looking beyond the end of the
|
14
|
+
# collection). By default it is +nil+.
|
15
|
+
#
|
16
|
+
def initialize(enumerable, options = {})
|
17
|
+
@end_value = options[:end]
|
18
|
+
@stop = options[:stop]
|
19
|
+
@iterator = enumerable.each
|
20
|
+
@iterator_ended = false
|
21
|
+
@buffer = []
|
22
|
+
@current = nil
|
23
|
+
@ended = false
|
24
|
+
end
|
25
|
+
|
26
|
+
attr_reader :current, :end_value
|
27
|
+
|
28
|
+
def each
|
29
|
+
if @stop
|
30
|
+
loop do
|
31
|
+
yield self.next
|
32
|
+
end
|
33
|
+
else
|
34
|
+
yield self.next while next?
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
include Enumerable
|
39
|
+
|
40
|
+
def next
|
41
|
+
if @buffer.size > 0
|
42
|
+
@current = @buffer.shift
|
43
|
+
else
|
44
|
+
if @stop
|
45
|
+
begin
|
46
|
+
@current = @iterator.next
|
47
|
+
rescue StopIteration
|
48
|
+
@iterator_ended = @ended = true
|
49
|
+
@current = @end_value
|
50
|
+
raise
|
51
|
+
end
|
52
|
+
else
|
53
|
+
@current = next_or_end
|
54
|
+
@ended = @iterator_ended
|
55
|
+
@current
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
def ended?
|
61
|
+
# If @end_value cannot be contained in the collection,
|
62
|
+
# then this is equivalent to:
|
63
|
+
# @buffer.empty? && @current = @end_value
|
64
|
+
@ended
|
65
|
+
end
|
66
|
+
|
67
|
+
def next?
|
68
|
+
# If @end_value cannot be contained in the collection,
|
69
|
+
# then this is equivalent to:
|
70
|
+
# !ended? && (look_ahead(1) != @end_value)
|
71
|
+
!ended? && (look_ahead(1); !(@iterator_ended && @buffer.size==0))
|
72
|
+
end
|
73
|
+
|
74
|
+
# This method is used to obtained the next value to be
|
75
|
+
# returned by +each+. If a parameter +n+ is passed
|
76
|
+
# the value to be returned after +n+ calls to +each+ is
|
77
|
+
# returned. The value 0 can be used to return the current
|
78
|
+
# value (from the previous call to +each+),
|
79
|
+
def look_ahead(n=1)
|
80
|
+
raise "Can't look back!" if n < 0
|
81
|
+
return @current if n == 0
|
82
|
+
|
83
|
+
while n > @buffer.size && !@iterator_ended
|
84
|
+
value = next_or_end
|
85
|
+
@buffer << value unless @iterator_ended
|
86
|
+
end
|
87
|
+
n <= @buffer.size ? @buffer[n-1] : @end_value
|
88
|
+
end
|
89
|
+
|
90
|
+
def is_end?(value)
|
91
|
+
value == @end_value
|
92
|
+
end
|
93
|
+
|
94
|
+
def is_valid?(value)
|
95
|
+
value != @end_value
|
96
|
+
end
|
97
|
+
|
98
|
+
private
|
99
|
+
|
100
|
+
def next_or_end
|
101
|
+
return @end_value if @ended
|
102
|
+
begin
|
103
|
+
@iterator.next
|
104
|
+
rescue StopIteration
|
105
|
+
@iterator_ended = true
|
106
|
+
@end_value
|
107
|
+
end
|
108
|
+
end
|
109
|
+
|
110
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'look_ahead_iterator/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "look_ahead_iterator"
|
8
|
+
spec.version = LookAheadIterator::VERSION
|
9
|
+
spec.authors = ["Javier Goizueta"]
|
10
|
+
spec.email = ["jgoizueta@gmail.com"]
|
11
|
+
spec.summary = %q{Iterator with look ahead operation}
|
12
|
+
spec.description = %q{Look ahead when iterating over an enumerable collection}
|
13
|
+
spec.homepage = "https://github.com/jgoizueta/look_ahead_iterator"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0")
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.6"
|
22
|
+
spec.add_development_dependency "rake"
|
23
|
+
spec.add_development_dependency "minitest", "~> 5.7"
|
24
|
+
end
|
@@ -0,0 +1,261 @@
|
|
1
|
+
require 'minitest_helper'
|
2
|
+
|
3
|
+
class TestLookAheadIterator < Minitest::Test
|
4
|
+
|
5
|
+
def test_simple_iteration
|
6
|
+
it = LookAheadIterator.new([11,22,33,44,55], end: :end, stop: false)
|
7
|
+
refute it.ended?
|
8
|
+
assert_equal 11, it.next
|
9
|
+
refute it.ended?
|
10
|
+
assert_equal 22, it.next
|
11
|
+
refute it.ended?
|
12
|
+
assert_equal 33, it.next
|
13
|
+
refute it.ended?
|
14
|
+
assert_equal 44, it.next
|
15
|
+
refute it.ended?
|
16
|
+
assert_equal 55, it.next
|
17
|
+
refute it.ended?
|
18
|
+
assert_equal :end, it.next
|
19
|
+
assert it.ended?
|
20
|
+
end
|
21
|
+
|
22
|
+
def test_iterate_and_look_ahead_with_end_value
|
23
|
+
it = LookAheadIterator.new([11,22,33,44,55], end: :end, stop: false)
|
24
|
+
assert it.next?
|
25
|
+
refute it.ended?
|
26
|
+
assert_equal 11, it.next
|
27
|
+
assert_equal 33, it.look_ahead(2)
|
28
|
+
assert_equal 22, it.look_ahead(1)
|
29
|
+
assert_equal 44, it.look_ahead(3)
|
30
|
+
assert_equal 22, it.next
|
31
|
+
assert it.next?
|
32
|
+
refute it.ended?
|
33
|
+
assert_equal 33, it.next
|
34
|
+
assert_equal 44, it.next
|
35
|
+
assert_equal 55, it.next
|
36
|
+
assert_equal :end, it.next
|
37
|
+
assert it.ended?
|
38
|
+
|
39
|
+
it = LookAheadIterator.new([11,22,33,44,55], end: :end, stop: false)
|
40
|
+
assert it.next?
|
41
|
+
refute it.ended?
|
42
|
+
assert_equal 11, it.next
|
43
|
+
assert_equal 33, it.look_ahead(2)
|
44
|
+
assert_equal 22, it.look_ahead(1)
|
45
|
+
assert_equal 44, it.look_ahead(3)
|
46
|
+
assert_equal 22, it.next
|
47
|
+
assert it.next?
|
48
|
+
refute it.ended?
|
49
|
+
assert_equal 33, it.next
|
50
|
+
assert_equal 44, it.next
|
51
|
+
assert it.next?
|
52
|
+
assert_equal 55, it.next
|
53
|
+
refute it.next?
|
54
|
+
refute it.ended?
|
55
|
+
assert_equal :end, it.next
|
56
|
+
assert it.ended?
|
57
|
+
end
|
58
|
+
|
59
|
+
def test_iterate_and_look_ahead_past_end
|
60
|
+
it = LookAheadIterator.new([11,22,33,44,55], end: :end, stop: false)
|
61
|
+
assert it.next?
|
62
|
+
refute it.ended?
|
63
|
+
assert_equal 11, it.next
|
64
|
+
assert_equal 33, it.look_ahead(2)
|
65
|
+
assert_equal 22, it.look_ahead(1)
|
66
|
+
assert_equal 44, it.look_ahead(3)
|
67
|
+
assert_equal 22, it.next
|
68
|
+
assert it.next?
|
69
|
+
refute it.ended?
|
70
|
+
assert_equal 33, it.next
|
71
|
+
assert_equal 44, it.next
|
72
|
+
assert_equal 55, it.look_ahead(1)
|
73
|
+
refute it.ended?
|
74
|
+
assert it.next?
|
75
|
+
assert_equal 55, it.look_ahead(1)
|
76
|
+
assert_equal :end, it.look_ahead(2)
|
77
|
+
assert_equal :end, it.look_ahead(4)
|
78
|
+
refute it.ended?
|
79
|
+
assert it.next?
|
80
|
+
assert_equal 55, it.next
|
81
|
+
refute it.ended?
|
82
|
+
assert_equal :end, it.next
|
83
|
+
assert it.ended?
|
84
|
+
refute it.next?
|
85
|
+
end
|
86
|
+
|
87
|
+
def test_iterate_and_look_ahead_with_stop
|
88
|
+
it = LookAheadIterator.new([11,22,33,44,55], end: :end, stop: true)
|
89
|
+
assert it.next?
|
90
|
+
refute it.ended?
|
91
|
+
assert_equal 11, it.next
|
92
|
+
assert_equal 33, it.look_ahead(2)
|
93
|
+
assert_equal 22, it.look_ahead(1)
|
94
|
+
assert_equal 44, it.look_ahead(3)
|
95
|
+
assert_equal 22, it.next
|
96
|
+
assert it.next?
|
97
|
+
refute it.ended?
|
98
|
+
assert_equal 33, it.next
|
99
|
+
assert_equal 44, it.next
|
100
|
+
assert_equal 55, it.next
|
101
|
+
assert_raises(StopIteration){ it.next }
|
102
|
+
assert it.ended?
|
103
|
+
refute it.next?
|
104
|
+
|
105
|
+
it = LookAheadIterator.new([11,22,33,44,55], end: :end, stop: true)
|
106
|
+
assert it.next?
|
107
|
+
refute it.ended?
|
108
|
+
assert_equal 11, it.next
|
109
|
+
assert_equal 33, it.look_ahead(2)
|
110
|
+
assert_equal 22, it.look_ahead(1)
|
111
|
+
assert_equal 44, it.look_ahead(3)
|
112
|
+
assert_equal 22, it.next
|
113
|
+
assert it.next?
|
114
|
+
refute it.ended?
|
115
|
+
assert_equal 33, it.next
|
116
|
+
assert_equal 44, it.next
|
117
|
+
assert_equal 55, it.look_ahead(1)
|
118
|
+
assert_equal :end, it.look_ahead(2)
|
119
|
+
assert_equal :end, it.look_ahead(4)
|
120
|
+
assert_equal 55, it.next
|
121
|
+
assert_equal :end, it.look_ahead(2)
|
122
|
+
assert_equal :end, it.look_ahead(1)
|
123
|
+
assert_raises(StopIteration){ it.next }
|
124
|
+
assert it.ended?
|
125
|
+
refute it.next?
|
126
|
+
end
|
127
|
+
|
128
|
+
def test_overlapping_look_ahead
|
129
|
+
it = LookAheadIterator.new([11,22,33,44,55], end: :end, stop: false)
|
130
|
+
assert it.next?
|
131
|
+
refute it.ended?
|
132
|
+
assert_equal 11, it.look_ahead
|
133
|
+
assert_equal 11, it.next
|
134
|
+
assert_equal 33, it.look_ahead(2)
|
135
|
+
assert_equal 22, it.look_ahead(1)
|
136
|
+
assert_equal 44, it.look_ahead(3)
|
137
|
+
assert_equal 22, it.next
|
138
|
+
assert_equal 33, it.look_ahead(1)
|
139
|
+
assert_equal 44, it.look_ahead(2)
|
140
|
+
assert_equal 55, it.look_ahead(3)
|
141
|
+
assert_equal 33, it.next
|
142
|
+
assert_equal 44, it.look_ahead(1)
|
143
|
+
assert_equal 55, it.look_ahead(2)
|
144
|
+
assert_equal :end, it.look_ahead(3)
|
145
|
+
assert_equal 44, it.next
|
146
|
+
assert_equal 55, it.look_ahead(1)
|
147
|
+
assert_equal :end, it.look_ahead(2)
|
148
|
+
assert_equal :end, it.look_ahead(3)
|
149
|
+
assert_equal 55, it.next
|
150
|
+
assert_equal :end, it.look_ahead(1)
|
151
|
+
assert_equal :end, it.look_ahead(2)
|
152
|
+
assert_equal :end, it.look_ahead(3)
|
153
|
+
assert_equal :end, it.next
|
154
|
+
assert_equal :end, it.look_ahead(1)
|
155
|
+
assert_equal :end, it.look_ahead(2)
|
156
|
+
assert_equal :end, it.look_ahead(3)
|
157
|
+
end
|
158
|
+
|
159
|
+
def test_unreliable_end_value
|
160
|
+
# If the end-marking value is contained in the collection,
|
161
|
+
# then the user may be confused (checking for the end value doesn't
|
162
|
+
# detect the end of the iterator reliably), but the ended? and
|
163
|
+
# next? methods should still work reliably.
|
164
|
+
|
165
|
+
# Check ended? without look ahead
|
166
|
+
it = LookAheadIterator.new([11,22,nil,44,nil], stop: false)
|
167
|
+
assert_nil it.end_value
|
168
|
+
assert_equal 11, it.next
|
169
|
+
assert_equal 22, it.next
|
170
|
+
refute it.ended?
|
171
|
+
assert_nil it.next
|
172
|
+
refute it.ended?
|
173
|
+
assert_equal 44, it.next
|
174
|
+
refute it.ended?
|
175
|
+
assert_nil it.next
|
176
|
+
refute it.ended?
|
177
|
+
assert_nil it.next
|
178
|
+
assert it.ended?
|
179
|
+
|
180
|
+
# Check next? without look ahead
|
181
|
+
it = LookAheadIterator.new([11,22,nil,44,nil], stop: false)
|
182
|
+
assert_nil it.end_value
|
183
|
+
assert_equal 11, it.next
|
184
|
+
assert_equal 22, it.next
|
185
|
+
assert it.next?
|
186
|
+
assert_nil it.next
|
187
|
+
assert it.next?
|
188
|
+
assert_equal 44, it.next
|
189
|
+
assert it.next?
|
190
|
+
assert_nil it.next
|
191
|
+
refute it.next?
|
192
|
+
assert_nil it.next
|
193
|
+
refute it.next?
|
194
|
+
|
195
|
+
# Check ended? with look ahead
|
196
|
+
it = LookAheadIterator.new([11,22,nil,44,nil], stop: false)
|
197
|
+
assert_nil it.end_value
|
198
|
+
assert_equal 11, it.next
|
199
|
+
assert_equal 22, it.next
|
200
|
+
refute it.ended?
|
201
|
+
assert_nil it.look_ahead
|
202
|
+
refute it.ended?
|
203
|
+
assert_nil it.next
|
204
|
+
refute it.ended?
|
205
|
+
assert_equal 44, it.look_ahead(1)
|
206
|
+
assert_nil it.look_ahead(2)
|
207
|
+
assert_nil it.look_ahead(3)
|
208
|
+
refute it.ended?
|
209
|
+
assert_equal 44, it.next
|
210
|
+
refute it.ended?
|
211
|
+
assert_nil it.next
|
212
|
+
refute it.ended?
|
213
|
+
assert_nil it.next
|
214
|
+
assert it.ended?
|
215
|
+
|
216
|
+
# Check next? with look ahead
|
217
|
+
it = LookAheadIterator.new([11,22,nil,44,nil], stop: false)
|
218
|
+
assert_nil it.end_value
|
219
|
+
assert_equal 11, it.next
|
220
|
+
assert_equal 22, it.next
|
221
|
+
assert it.next?
|
222
|
+
assert_nil it.next
|
223
|
+
assert it.next?
|
224
|
+
assert_equal 44, it.look_ahead(1)
|
225
|
+
assert_nil it.look_ahead(2)
|
226
|
+
assert_nil it.look_ahead(3)
|
227
|
+
assert it.next?
|
228
|
+
assert_equal 44, it.next
|
229
|
+
assert it.next?
|
230
|
+
assert_nil it.next
|
231
|
+
refute it.next?
|
232
|
+
assert_nil it.next
|
233
|
+
refute it.next?
|
234
|
+
end
|
235
|
+
|
236
|
+
def test_each_with_stop_exception
|
237
|
+
it = LookAheadIterator.new([11,22,33,44,55], end: :end, stop: true)
|
238
|
+
result = []
|
239
|
+
it.each do |value|
|
240
|
+
result << value
|
241
|
+
end
|
242
|
+
assert_equal [11,22,33,44,55], result
|
243
|
+
end
|
244
|
+
|
245
|
+
def test_each_without_stop_exception
|
246
|
+
it = LookAheadIterator.new([11,22,33,44,55], end: :end, stop: false)
|
247
|
+
result = []
|
248
|
+
it.each do |value|
|
249
|
+
result << value
|
250
|
+
end
|
251
|
+
assert_equal [11,22,33,44,55], result
|
252
|
+
end
|
253
|
+
|
254
|
+
def test_enumerable
|
255
|
+
it = LookAheadIterator.new([11,22,33,44,55], end: :end, stop: false)
|
256
|
+
assert_equal [11,22,33,44,55], it.to_a
|
257
|
+
it = LookAheadIterator.new([11,22,33,44,55], end: :end, stop: false)
|
258
|
+
assert_equal [11,22,33,44,55], it.map{|v| v}
|
259
|
+
end
|
260
|
+
|
261
|
+
end
|
metadata
ADDED
@@ -0,0 +1,99 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: look_ahead_iterator
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Javier Goizueta
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-06-14 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.6'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.6'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: minitest
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '5.7'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '5.7'
|
55
|
+
description: Look ahead when iterating over an enumerable collection
|
56
|
+
email:
|
57
|
+
- jgoizueta@gmail.com
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- ".gitignore"
|
63
|
+
- ".travis.yml"
|
64
|
+
- Gemfile
|
65
|
+
- LICENSE.txt
|
66
|
+
- README.md
|
67
|
+
- Rakefile
|
68
|
+
- lib/look_ahead_iterator.rb
|
69
|
+
- lib/look_ahead_iterator/version.rb
|
70
|
+
- look_ahead_iterator.gemspec
|
71
|
+
- test/minitest_helper.rb
|
72
|
+
- test/test_look_ahead_iterator.rb
|
73
|
+
homepage: https://github.com/jgoizueta/look_ahead_iterator
|
74
|
+
licenses:
|
75
|
+
- MIT
|
76
|
+
metadata: {}
|
77
|
+
post_install_message:
|
78
|
+
rdoc_options: []
|
79
|
+
require_paths:
|
80
|
+
- lib
|
81
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
82
|
+
requirements:
|
83
|
+
- - ">="
|
84
|
+
- !ruby/object:Gem::Version
|
85
|
+
version: '0'
|
86
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
87
|
+
requirements:
|
88
|
+
- - ">="
|
89
|
+
- !ruby/object:Gem::Version
|
90
|
+
version: '0'
|
91
|
+
requirements: []
|
92
|
+
rubyforge_project:
|
93
|
+
rubygems_version: 2.2.2
|
94
|
+
signing_key:
|
95
|
+
specification_version: 4
|
96
|
+
summary: Iterator with look ahead operation
|
97
|
+
test_files:
|
98
|
+
- test/minitest_helper.rb
|
99
|
+
- test/test_look_ahead_iterator.rb
|