self_enumerable 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/.rspec +3 -0
- data/.travis.yml +5 -0
- data/Gemfile +25 -0
- data/LICENSE.txt +22 -0
- data/README.md +63 -0
- data/Rakefile +11 -0
- data/benchmark/benchmark_self_enumerable.rb +53 -0
- data/bin/console +14 -0
- data/bin/setup +7 -0
- data/lib/self_enumerable/version.rb +3 -0
- data/lib/self_enumerable.rb +34 -0
- data/self_enumerable.gemspec +18 -0
- metadata +57 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 8b69d1e85f92b518c2c93e8542f627f88e08a628
|
4
|
+
data.tar.gz: f204806b3600cf93306bb3d49fe538e5dd14359e
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: c935152429fc1b265254e453a1e0e9e748c1d31ed9dd26d80444bb5a52b201a69753c1d98a1017ce4548519cae0875a2969144471f51e903092d649304a3a626
|
7
|
+
data.tar.gz: e519d3c72be0eb4a50aaa82fa6372b527d330cd9ad6eb4c8bfd3242d7292699e74bbcbdf15482785224065b5251f393e00939c820be84b174404c67e74d75682
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
source "https://rubygems.org"
|
2
|
+
|
3
|
+
# Specify your gem's dependencies in your gemspec
|
4
|
+
gemspec
|
5
|
+
|
6
|
+
group :development do
|
7
|
+
gem "bundler"
|
8
|
+
gem "rake"
|
9
|
+
gem "rspec", ">= 3.0.0", "< 4.0.0"
|
10
|
+
gem "simplecov", ">= 0.8.0"
|
11
|
+
|
12
|
+
if !ENV["CI"]
|
13
|
+
gem "benchmark-ips"
|
14
|
+
gem "hashdiff"
|
15
|
+
gem "pry", "~> 0.9.12.6"
|
16
|
+
gem "pry-byebug", "<= 1.3.2"
|
17
|
+
gem "pry-rescue", "~> 1.4.2"
|
18
|
+
gem "pry-stack_explorer", "~> 0.4.9.1"
|
19
|
+
gem "pry-syntax-hacks", "~> 0.0.6"
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
group :test do
|
24
|
+
gem "codeclimate-test-reporter", require: nil
|
25
|
+
end
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2015 Michael Sievers
|
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,63 @@
|
|
1
|
+
# SelfEnumerable
|
2
|
+
|
3
|
+
## Usage
|
4
|
+
|
5
|
+
```ruby
|
6
|
+
require "self_enumerable"
|
7
|
+
|
8
|
+
class DataSet
|
9
|
+
include SelfEnumerable
|
10
|
+
|
11
|
+
# constructor needs to be able to handle array parameter
|
12
|
+
def initialize(values = [])
|
13
|
+
@values = values
|
14
|
+
end
|
15
|
+
|
16
|
+
def each
|
17
|
+
return enum_for(__method__) unless block_given?
|
18
|
+
|
19
|
+
@values.each do |value|
|
20
|
+
yield value
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
data_set = DataSet.new([1,2,3,4,5])
|
26
|
+
|
27
|
+
#
|
28
|
+
# instead of returning plain arrays, instances of objects class are returned
|
29
|
+
#
|
30
|
+
|
31
|
+
data_set.select { |element| element < 3 }
|
32
|
+
# => #<DataSet:0x00000000f0f0a0 @values=[1, 2]>
|
33
|
+
|
34
|
+
data_set.partition { |element| element <= 3 }
|
35
|
+
# => [#<DataSet:0x00000000ed2c40 @values=[1, 2, 3]>, #<DataSet:0x00000000ed2c18 @values=[4, 5]>]
|
36
|
+
```
|
37
|
+
|
38
|
+
## Installation
|
39
|
+
|
40
|
+
Add this line to your application's Gemfile:
|
41
|
+
|
42
|
+
```ruby
|
43
|
+
gem 'self_enumerable'
|
44
|
+
```
|
45
|
+
|
46
|
+
And then execute:
|
47
|
+
|
48
|
+
$ bundle
|
49
|
+
|
50
|
+
Or install it yourself as:
|
51
|
+
|
52
|
+
$ gem install self_enumerable
|
53
|
+
|
54
|
+
## Development
|
55
|
+
|
56
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
57
|
+
|
58
|
+
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).
|
59
|
+
|
60
|
+
## Contributing
|
61
|
+
|
62
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/msievers/self_enumerable.
|
63
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,11 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
2
|
+
require "rspec/core/rake_task"
|
3
|
+
|
4
|
+
RSpec::Core::RakeTask.new(:spec)
|
5
|
+
|
6
|
+
task :default => :spec
|
7
|
+
|
8
|
+
desc "Benchmark some methods against plain Enumerable"
|
9
|
+
task :benchmark do
|
10
|
+
load File.expand_path("./benchmark/benchmark_self_enumerable.rb")
|
11
|
+
end
|
@@ -0,0 +1,53 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "self_enumerable"
|
5
|
+
|
6
|
+
class BenchmarkSelfEnumerable
|
7
|
+
class DataSet
|
8
|
+
def initialize(values)
|
9
|
+
@values = values
|
10
|
+
end
|
11
|
+
|
12
|
+
def each
|
13
|
+
return enum_for(__method__) unless block_given?
|
14
|
+
|
15
|
+
@values.each do |_value|
|
16
|
+
yield _value
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
class EnumerableDataSet < DataSet
|
22
|
+
include Enumerable
|
23
|
+
end
|
24
|
+
|
25
|
+
class SelfEnumerableDataSet < DataSet
|
26
|
+
include SelfEnumerable
|
27
|
+
end
|
28
|
+
|
29
|
+
def call
|
30
|
+
require "benchmark/ips"
|
31
|
+
|
32
|
+
values = [1,2,3,4,5,6,7,8,9,10]
|
33
|
+
|
34
|
+
enumerable_data_set = EnumerableDataSet.new(values)
|
35
|
+
self_enumerable_data_set = SelfEnumerableDataSet.new(values)
|
36
|
+
|
37
|
+
Benchmark.ips do |x|
|
38
|
+
callable = -> (_el) { _el }
|
39
|
+
|
40
|
+
x.report("Enumerable#map") do
|
41
|
+
enumerable_data_set.map(&callable)
|
42
|
+
end
|
43
|
+
|
44
|
+
x.report("SelfEnumerable#map") do
|
45
|
+
self_enumerable_data_set.map(&callable)
|
46
|
+
end
|
47
|
+
|
48
|
+
x.compare!
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
BenchmarkSelfEnumerable.new.call
|
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "self_enumerable"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start
|
data/bin/setup
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
require "self_enumerable/version"
|
2
|
+
|
3
|
+
module SelfEnumerable
|
4
|
+
WRAPPED_METHODS = [
|
5
|
+
:collect, :collect_concat, :drop, :drop_while, :first, :find_all, :flat_map,
|
6
|
+
:grep, :map, :max, :min, :min_by, :partition, :reject, :select, :sort, :sort_by,
|
7
|
+
:take, :take_while
|
8
|
+
]
|
9
|
+
|
10
|
+
def self.included(klass)
|
11
|
+
klass.class_eval <<-eoruby
|
12
|
+
include Enumerable
|
13
|
+
eoruby
|
14
|
+
|
15
|
+
# for every Enumerable method, e.g. #map, #select ...
|
16
|
+
Enumerable.public_instance_methods.each do |_enum_method|
|
17
|
+
if WRAPPED_METHODS.include?(_enum_method)
|
18
|
+
if _enum_method == :partition # some special handling is needed
|
19
|
+
klass.class_eval <<-eoruby
|
20
|
+
def #{_enum_method}(*)
|
21
|
+
(result = super).is_a?(Array) ? [self.class.new(result[0]), self.class.new(result[1])] : result
|
22
|
+
end
|
23
|
+
eoruby
|
24
|
+
else
|
25
|
+
klass.class_eval <<-eoruby
|
26
|
+
def #{_enum_method}(*)
|
27
|
+
(result = super).is_a?(Array) ? self.class.new(result) : result
|
28
|
+
end
|
29
|
+
eoruby
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path("../lib", __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require "self_enumerable/version"
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "self_enumerable"
|
8
|
+
spec.version = SelfEnumerable::VERSION
|
9
|
+
spec.authors = ["Michael Sievers"]
|
10
|
+
spec.summary = %q{Extended Enumerable module which returns instances of an objects class instead of plain arrays.}
|
11
|
+
spec.licenses = ["MIT"]
|
12
|
+
spec.homepage = "https://github.com/msievers/self_enumerable"
|
13
|
+
|
14
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
15
|
+
spec.bindir = "exe"
|
16
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
17
|
+
spec.require_paths = ["lib"]
|
18
|
+
end
|
metadata
ADDED
@@ -0,0 +1,57 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: self_enumerable
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Michael Sievers
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-08-28 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description:
|
14
|
+
email:
|
15
|
+
executables: []
|
16
|
+
extensions: []
|
17
|
+
extra_rdoc_files: []
|
18
|
+
files:
|
19
|
+
- ".gitignore"
|
20
|
+
- ".rspec"
|
21
|
+
- ".travis.yml"
|
22
|
+
- Gemfile
|
23
|
+
- LICENSE.txt
|
24
|
+
- README.md
|
25
|
+
- Rakefile
|
26
|
+
- benchmark/benchmark_self_enumerable.rb
|
27
|
+
- bin/console
|
28
|
+
- bin/setup
|
29
|
+
- lib/self_enumerable.rb
|
30
|
+
- lib/self_enumerable/version.rb
|
31
|
+
- self_enumerable.gemspec
|
32
|
+
homepage: https://github.com/msievers/self_enumerable
|
33
|
+
licenses:
|
34
|
+
- MIT
|
35
|
+
metadata: {}
|
36
|
+
post_install_message:
|
37
|
+
rdoc_options: []
|
38
|
+
require_paths:
|
39
|
+
- lib
|
40
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
41
|
+
requirements:
|
42
|
+
- - ">="
|
43
|
+
- !ruby/object:Gem::Version
|
44
|
+
version: '0'
|
45
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
46
|
+
requirements:
|
47
|
+
- - ">="
|
48
|
+
- !ruby/object:Gem::Version
|
49
|
+
version: '0'
|
50
|
+
requirements: []
|
51
|
+
rubyforge_project:
|
52
|
+
rubygems_version: 2.4.8
|
53
|
+
signing_key:
|
54
|
+
specification_version: 4
|
55
|
+
summary: Extended Enumerable module which returns instances of an objects class instead
|
56
|
+
of plain arrays.
|
57
|
+
test_files: []
|