multi_enumerator 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +18 -0
- data/.rspec +2 -0
- data/.travis.yml +5 -0
- data/Gemfile +7 -0
- data/LICENSE.txt +22 -0
- data/README.md +68 -0
- data/Rakefile +6 -0
- data/lib/multi_enumerator.rb +8 -0
- data/lib/multi_enumerator/multi_enumerator.rb +45 -0
- data/lib/multi_enumerator/version.rb +3 -0
- data/multi_enumerator.gemspec +25 -0
- data/spec/multi_enumerator_spec.rb +39 -0
- data/spec/spec_helper.rb +5 -0
- metadata +115 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: a8056ddfc0ac106027a1f134328cb74b678bb35d
|
4
|
+
data.tar.gz: dd912600f45d8df1c2b7a9a244bf6e98d6b7f0de
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 5c9ccd6aed24204998f2b05889dfce85d55ae044f16a078caa0ccbf4282642e87c9c3a88e9ee0c6b74b08f32855fcb4a4ce3a38595164e1b9ac19dabc4cb70cd
|
7
|
+
data.tar.gz: bb3fd4caec9372d033b6dde9968858cbbf9527a3b8f8c4856beddc2b5bb537105fc3ebac6aee45807b906a985e1b7b1513d68630ef24784c2016d6b16efa334a
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Alexander Pavlenko
|
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,68 @@
|
|
1
|
+
# MultiEnumerator
|
2
|
+
|
3
|
+
Simultaneously process multiple enumerators.
|
4
|
+
|
5
|
+
[![Travis CI](https://secure.travis-ci.org/AlexanderPavlenko/multi_enumerator.png)](https://travis-ci.org/AlexanderPavlenko/multi_enumerator)
|
6
|
+
[![Coverage Status](https://coveralls.io/repos/AlexanderPavlenko/multi_enumerator/badge.png?branch=master)](https://coveralls.io/r/AlexanderPavlenko/multi_enumerator)
|
7
|
+
|
8
|
+
## Installation
|
9
|
+
|
10
|
+
Add this line to your application's Gemfile:
|
11
|
+
|
12
|
+
gem 'multi_enumerator'
|
13
|
+
|
14
|
+
And then execute:
|
15
|
+
|
16
|
+
$ bundle
|
17
|
+
|
18
|
+
Or install it yourself as:
|
19
|
+
|
20
|
+
$ gem install multi_enumerator
|
21
|
+
|
22
|
+
## Usage
|
23
|
+
|
24
|
+
Main goal of MultiEnumerator is to reduce Array#zip abuse and simplify IO streams handling.
|
25
|
+
|
26
|
+
a = [1, 2, 3].each
|
27
|
+
b = [10, 20].each
|
28
|
+
c = [30].each
|
29
|
+
MultiEnumerator.build([a, b, c]).each do |*slice|
|
30
|
+
puts slice.inspect
|
31
|
+
end
|
32
|
+
|
33
|
+
=>
|
34
|
+
[1, 10, 30]
|
35
|
+
[2, 20, #<StopIteration: iteration reached an end>]
|
36
|
+
[3, #<StopIteration: iteration reached an end>, #<StopIteration: iteration reached an end>]
|
37
|
+
|
38
|
+
Instead of getting tons of StopIteration you may wish to specify a replacement.
|
39
|
+
|
40
|
+
a = [1, 2, 3].each
|
41
|
+
b = [10, 20].each
|
42
|
+
c = [30].each
|
43
|
+
MultiEnumerator.build(a, b, c, :ljust_with => nil).each do |*slice|
|
44
|
+
puts slice.inspect
|
45
|
+
end
|
46
|
+
|
47
|
+
=>
|
48
|
+
[1, 10, 30]
|
49
|
+
[2, 20, nil]
|
50
|
+
[3, nil, nil]
|
51
|
+
|
52
|
+
By default all enumerators are rewound on before the iterating.
|
53
|
+
This may be prevented by passing ```:rewind``` option.
|
54
|
+
|
55
|
+
enum = [1, 2, 3].each
|
56
|
+
enum.next # skip first elem
|
57
|
+
puts MultiEnumerator.build(enum, :rewind => false).each.to_a.inspect
|
58
|
+
|
59
|
+
=>
|
60
|
+
[2, 3]
|
61
|
+
|
62
|
+
## Contributing
|
63
|
+
|
64
|
+
1. Fork it
|
65
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
66
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
67
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
68
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
class MultiEnumerator::MultiEnumerator < Enumerator
|
2
|
+
|
3
|
+
attr_reader :enums
|
4
|
+
|
5
|
+
# @param [Enumerator, ...] enums List of enumerators
|
6
|
+
# @param [Hash] options
|
7
|
+
# @option options [Boolean] :rewind Indicates whether enumerators should be rewound before traversal. Defaults to the +true+.
|
8
|
+
# @option options [Object] :ljust_with Object for filling tails. Defaults to the corresponding +StopIteration+ exception.
|
9
|
+
def initialize(*args)
|
10
|
+
@options = args.last.is_a?(::Hash) ? args.pop : {}
|
11
|
+
@options[:rewind] = true unless @options.key?(:rewind)
|
12
|
+
@enums = args.flatten.freeze
|
13
|
+
end
|
14
|
+
|
15
|
+
# @yield [a, b, c, ...] items from the corresponding enumerators
|
16
|
+
def each
|
17
|
+
return to_enum(:each) unless block_given?
|
18
|
+
|
19
|
+
@enums.each(&:rewind) if @options[:rewind]
|
20
|
+
|
21
|
+
loop do
|
22
|
+
entry = @enums.map do |enum|
|
23
|
+
begin
|
24
|
+
enum.next
|
25
|
+
rescue => ex
|
26
|
+
ex
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
break if entry.all?{|item| item.is_a? ::StopIteration }
|
31
|
+
|
32
|
+
if @options.key?(:ljust_with)
|
33
|
+
entry.map!{|item| item.is_a?(::StopIteration) ? @options[:ljust_with] : item }
|
34
|
+
end
|
35
|
+
|
36
|
+
yield *entry
|
37
|
+
end
|
38
|
+
|
39
|
+
self
|
40
|
+
end
|
41
|
+
|
42
|
+
def inspect
|
43
|
+
"#<#{self.class.name}: #{@enums.inspect}>"
|
44
|
+
end
|
45
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'multi_enumerator/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "multi_enumerator"
|
8
|
+
spec.version = MultiEnumerator::VERSION
|
9
|
+
spec.authors = ["AlexanderPavlenko"]
|
10
|
+
spec.email = ["alerticus@gmail.com"]
|
11
|
+
spec.description = %q{Simultaneously process multiple enumerators}
|
12
|
+
spec.summary = %q{MultiEnumerator}
|
13
|
+
spec.homepage = ""
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files`.split($/)
|
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.3"
|
22
|
+
spec.add_development_dependency "rake"
|
23
|
+
spec.add_development_dependency "rspec"
|
24
|
+
spec.add_development_dependency "coveralls"
|
25
|
+
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe MultiEnumerator do
|
4
|
+
|
5
|
+
it "iterates over given enumerators" do
|
6
|
+
a = [1, 2, 3].each
|
7
|
+
b = [10, 20].each
|
8
|
+
c = [30].each
|
9
|
+
result = MultiEnumerator.build(a, b, c).each.to_a
|
10
|
+
result[0].should == [1, 10, 30]
|
11
|
+
result[1][0..1].should == [2, 20]
|
12
|
+
result[2][0].should == 3
|
13
|
+
result[1][2].class.should == StopIteration
|
14
|
+
result[2][1..2].map(&:class).uniq.should == [StopIteration]
|
15
|
+
end
|
16
|
+
|
17
|
+
it "iterates over given enumerators, filling tails" do
|
18
|
+
a = [1, 2, 3].each
|
19
|
+
b = [10, 20].each
|
20
|
+
c = [30].each
|
21
|
+
result = MultiEnumerator.build([a, b, c], :ljust_with => nil).each.to_a
|
22
|
+
result[0].should == [1, 10, 30]
|
23
|
+
result[1][0..1].should == [2, 20]
|
24
|
+
result[2][0].should == 3
|
25
|
+
result[1][2].should == nil
|
26
|
+
result[2][1..2].uniq.should == [nil]
|
27
|
+
end
|
28
|
+
|
29
|
+
it "respects :rewind option" do
|
30
|
+
enum = [1,2,3].each
|
31
|
+
enum.next
|
32
|
+
result = MultiEnumerator.build(enum, :rewind => false).each.to_a
|
33
|
+
result.should == [2, 3]
|
34
|
+
end
|
35
|
+
|
36
|
+
it "is inspectable" do
|
37
|
+
MultiEnumerator.build([].each).inspect
|
38
|
+
end
|
39
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,115 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: multi_enumerator
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- AlexanderPavlenko
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2013-09-15 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.3'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ~>
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.3'
|
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: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - '>='
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: coveralls
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - '>='
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
description: Simultaneously process multiple enumerators
|
70
|
+
email:
|
71
|
+
- alerticus@gmail.com
|
72
|
+
executables: []
|
73
|
+
extensions: []
|
74
|
+
extra_rdoc_files: []
|
75
|
+
files:
|
76
|
+
- .gitignore
|
77
|
+
- .rspec
|
78
|
+
- .travis.yml
|
79
|
+
- Gemfile
|
80
|
+
- LICENSE.txt
|
81
|
+
- README.md
|
82
|
+
- Rakefile
|
83
|
+
- lib/multi_enumerator.rb
|
84
|
+
- lib/multi_enumerator/multi_enumerator.rb
|
85
|
+
- lib/multi_enumerator/version.rb
|
86
|
+
- multi_enumerator.gemspec
|
87
|
+
- spec/multi_enumerator_spec.rb
|
88
|
+
- spec/spec_helper.rb
|
89
|
+
homepage: ''
|
90
|
+
licenses:
|
91
|
+
- MIT
|
92
|
+
metadata: {}
|
93
|
+
post_install_message:
|
94
|
+
rdoc_options: []
|
95
|
+
require_paths:
|
96
|
+
- lib
|
97
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
98
|
+
requirements:
|
99
|
+
- - '>='
|
100
|
+
- !ruby/object:Gem::Version
|
101
|
+
version: '0'
|
102
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
103
|
+
requirements:
|
104
|
+
- - '>='
|
105
|
+
- !ruby/object:Gem::Version
|
106
|
+
version: '0'
|
107
|
+
requirements: []
|
108
|
+
rubyforge_project:
|
109
|
+
rubygems_version: 2.0.3
|
110
|
+
signing_key:
|
111
|
+
specification_version: 4
|
112
|
+
summary: MultiEnumerator
|
113
|
+
test_files:
|
114
|
+
- spec/multi_enumerator_spec.rb
|
115
|
+
- spec/spec_helper.rb
|