caching_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/.editorconfig +16 -0
- data/.gitignore +9 -0
- data/Gemfile +6 -0
- data/LICENSE.txt +21 -0
- data/README.md +77 -0
- data/Rakefile +10 -0
- data/caching_enumerator.gemspec +25 -0
- data/lib/caching_enumerator.rb +29 -0
- data/lib/caching_enumerator/version.rb +3 -0
- metadata +95 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: c9bc29bb0bb6f9df27227cd84c9f2de58204e959
|
4
|
+
data.tar.gz: cc376a2504e6fd73cdd8b80a1ce98aacea7bc4c4
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: c4e9049d083c190800e6e26784493b7eecc39fcea3e67541368c46795bf646ba8977d14ad8f0a65f0a2af22ea126cd767bd02f11101c44fddfbe8061c079dab4
|
7
|
+
data.tar.gz: dab81d816a63b700509c88b7c5147bf369792b753539c7751fc162232173e87fdd88f3de2dd3ba84e3863d37f7c88b7e41c3cf67b1f26c3a42dc5b13d21ef2b1
|
data/.editorconfig
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
root = true
|
2
|
+
|
3
|
+
# Unix-style newlines with a newline ending every file
|
4
|
+
[*]
|
5
|
+
end_of_line = lf
|
6
|
+
insert_final_newline = true
|
7
|
+
|
8
|
+
|
9
|
+
# Matches multiple files with brace expansion notation
|
10
|
+
# Set default charset
|
11
|
+
[*.rb]
|
12
|
+
charset = utf-8
|
13
|
+
indent_style = tab
|
14
|
+
indent_size = 4
|
15
|
+
trim_trailing_whitespace = true
|
16
|
+
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2017 TODO: Write your name
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,77 @@
|
|
1
|
+
# CachingEnumerator
|
2
|
+
|
3
|
+
A caching wrapper for Enumerator
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'caching_enumerator'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install caching_enumerator
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
```ruby
|
24
|
+
require 'caching_enumerator'
|
25
|
+
|
26
|
+
enum = CachingEnumerator.new do |yielder|
|
27
|
+
5.times do |i|
|
28
|
+
puts "very expensive operation #{i}"
|
29
|
+
yielder.yield i
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
enum.take 2
|
34
|
+
# very expensive operation 0
|
35
|
+
# very expensive operation 1
|
36
|
+
# => [0, 1]
|
37
|
+
|
38
|
+
enum.take 3
|
39
|
+
# very expensive operation 2
|
40
|
+
# => [0, 1, 2]
|
41
|
+
|
42
|
+
enum.next
|
43
|
+
# => 0
|
44
|
+
|
45
|
+
enum.next
|
46
|
+
# => 1
|
47
|
+
|
48
|
+
enum.next
|
49
|
+
# => 2
|
50
|
+
|
51
|
+
enum.next
|
52
|
+
# very expensive operation 3
|
53
|
+
# => 3
|
54
|
+
|
55
|
+
enum.rewind.next
|
56
|
+
# => 0
|
57
|
+
|
58
|
+
# calling `reset` will clear the cache and rewind the internal pointers
|
59
|
+
enum.reset.next
|
60
|
+
# very expensive operation 0
|
61
|
+
# => 0
|
62
|
+
|
63
|
+
```
|
64
|
+
|
65
|
+
## Development
|
66
|
+
|
67
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests.
|
68
|
+
|
69
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
70
|
+
|
71
|
+
## Contributing
|
72
|
+
|
73
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/cyclotron3k/caching_enumerator.
|
74
|
+
|
75
|
+
## License
|
76
|
+
|
77
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
@@ -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 "caching_enumerator/version"
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "caching_enumerator"
|
8
|
+
spec.version = CachingEnumerator::VERSION
|
9
|
+
spec.authors = "cyclotron3k"
|
10
|
+
|
11
|
+
spec.summary = "A caching wrapper for Enumerator"
|
12
|
+
spec.description = "If you're putting expensive operations in Enumerator, use this to cache and re-use the results"
|
13
|
+
spec.homepage = "https://github.com/cyclotron3k/caching_enumerator"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
17
|
+
f.match(%r{^(test|spec|features)/})
|
18
|
+
end
|
19
|
+
|
20
|
+
spec.require_paths = ["lib"]
|
21
|
+
|
22
|
+
spec.add_development_dependency "bundler", "~> 1.15"
|
23
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
24
|
+
spec.add_development_dependency "minitest", "~> 5.0"
|
25
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
class CachingEnumerator < Enumerator
|
2
|
+
def initialize(&block)
|
3
|
+
@cache = []
|
4
|
+
@enum = Enumerator.new(&block)
|
5
|
+
|
6
|
+
super do |yielder|
|
7
|
+
@cache.each do |person|
|
8
|
+
yielder.yield person
|
9
|
+
end
|
10
|
+
|
11
|
+
begin
|
12
|
+
loop do
|
13
|
+
next_val = @enum.next
|
14
|
+
@cache.push next_val
|
15
|
+
yielder.yield next_val
|
16
|
+
end
|
17
|
+
rescue StopIteration
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
def reset
|
23
|
+
@cache = []
|
24
|
+
@enum.rewind
|
25
|
+
self.rewind
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
require "caching_enumerator/version"
|
metadata
ADDED
@@ -0,0 +1,95 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: caching_enumerator
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- cyclotron3k
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2017-10-17 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.15'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.15'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.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.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '5.0'
|
55
|
+
description: If you're putting expensive operations in Enumerator, use this to cache
|
56
|
+
and re-use the results
|
57
|
+
email:
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- ".editorconfig"
|
63
|
+
- ".gitignore"
|
64
|
+
- Gemfile
|
65
|
+
- LICENSE.txt
|
66
|
+
- README.md
|
67
|
+
- Rakefile
|
68
|
+
- caching_enumerator.gemspec
|
69
|
+
- lib/caching_enumerator.rb
|
70
|
+
- lib/caching_enumerator/version.rb
|
71
|
+
homepage: https://github.com/cyclotron3k/caching_enumerator
|
72
|
+
licenses:
|
73
|
+
- MIT
|
74
|
+
metadata: {}
|
75
|
+
post_install_message:
|
76
|
+
rdoc_options: []
|
77
|
+
require_paths:
|
78
|
+
- lib
|
79
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
80
|
+
requirements:
|
81
|
+
- - ">="
|
82
|
+
- !ruby/object:Gem::Version
|
83
|
+
version: '0'
|
84
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
85
|
+
requirements:
|
86
|
+
- - ">="
|
87
|
+
- !ruby/object:Gem::Version
|
88
|
+
version: '0'
|
89
|
+
requirements: []
|
90
|
+
rubyforge_project:
|
91
|
+
rubygems_version: 2.5.1
|
92
|
+
signing_key:
|
93
|
+
specification_version: 4
|
94
|
+
summary: A caching wrapper for Enumerator
|
95
|
+
test_files: []
|