enumeradical 0.9.0 → 6.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.travis.yml +7 -1
- data/License.txt +23 -0
- data/README.markdown +35 -6
- data/lib/enumeradical/core_extensions/enumerable.rb +7 -0
- data/lib/enumeradical/version.rb +1 -1
- data/spec/core_extensions/enumerable_spec.rb +53 -10
- data/spec/core_extensions/object_spec.rb +6 -6
- metadata +26 -21
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: acf9796bb177925f8de88750686227c4833b11b2
|
4
|
+
data.tar.gz: 4f621336fbde5a32ad76af6f21242d80bc1c565e
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: bad863424536ef1614c4731a329d0029ba7fcd56400ba94fab117bb39903f4deb14a5a7f9cb554b983a179450c570d5658df048aa1380c3102ea39a056436952
|
7
|
+
data.tar.gz: e21d60e69ecd467215e8f567e040d4cca4abdbf6ca521b9115817641408056e9551314a7cfec066db2cb0a9aaac77d472d176e0c69b3c3cff551d1d82f5a39f1
|
data/.travis.yml
CHANGED
data/License.txt
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
(The MIT License)
|
2
|
+
|
3
|
+
Copyright (c) 2012 Corey Haines and Ryan Briones
|
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 NONINFRINGEMENT.
|
19
|
+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
20
|
+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
21
|
+
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
22
|
+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
23
|
+
|
data/README.markdown
CHANGED
@@ -1,4 +1,5 @@
|
|
1
|
-
[![Build Status](https://
|
1
|
+
[![Build Status](https://travis-ci.org/coreyhaines/enumeradical.svg?branch=master)](https://travis-ci.org/coreyhaines/enumeradical)
|
2
|
+
[![Code Climate](https://codeclimate.com/github/coreyhaines/enumeradical/badges/gpa.svg)](https://codeclimate.com/github/coreyhaines/enumeradical)
|
2
3
|
|
3
4
|
# Enumeradical
|
4
5
|
## A most amazing collection of useful functions filling common tasks when iterating over collections
|
@@ -7,9 +8,16 @@
|
|
7
8
|
I love enumerable. I really do. I use the functions it provides with the utmost alacrity. Nothing makes me sadder than seeing a #each used to populate an array. Once you start using them a lot in production systems, you notice a bunch of common patterns.
|
8
9
|
|
9
10
|
## How do I use it?
|
10
|
-
|
11
|
+
It is a gem, so just do
|
12
|
+
|
13
|
+
gem install enumeradical
|
14
|
+
|
15
|
+
then rock the house by requiring it. It sets itself up!
|
16
|
+
|
11
17
|
require 'enumeradical'
|
12
18
|
|
19
|
+
## Why would I use it?
|
20
|
+
|
13
21
|
###I have an array of objects, and I need to convert them to another type.
|
14
22
|
|
15
23
|
class MyNumberPresenter
|
@@ -22,7 +30,7 @@ Install it, then
|
|
22
30
|
# => [#<MyNumberPresenter:0x0000010086b9c8 @number=1>, #<MyNumberPresenter:0x0000010086b630 @number=2>,
|
23
31
|
#<MyNumberPresenter:0x0000010086b540 @number=3>]
|
24
32
|
|
25
|
-
NO MORE
|
33
|
+
**NO MORE!** Use Enumerable#map_to(type)
|
26
34
|
|
27
35
|
class MyNumberPresenter
|
28
36
|
def initialize(number)
|
@@ -39,7 +47,7 @@ NO MORE! Use Enumerable#map_to(type)
|
|
39
47
|
require 'date'
|
40
48
|
[1,2,3].map { |index| Date::ABBR_DAYNAMES[index] } # => ["Mon", "Tue", "Wed"]
|
41
49
|
|
42
|
-
NO MORE
|
50
|
+
**NO MORE!** Use Enumerable#map_into
|
43
51
|
|
44
52
|
require 'date'
|
45
53
|
[1,2,3].map_into Date::ABBR_DAYNAMES # => ["Mon", "Tue", "Wed"]
|
@@ -58,7 +66,7 @@ NO MORE! Use Enumerable#map_into
|
|
58
66
|
[1,2,3].map { |times| converter.hellos(times) }
|
59
67
|
# => ["hello", "hellohello", "hellohellohello"]
|
60
68
|
|
61
|
-
NO MORE
|
69
|
+
**NO MORE!** Use Object#map_over
|
62
70
|
|
63
71
|
class Converter
|
64
72
|
def hellos(times)
|
@@ -71,8 +79,29 @@ NO MORE! Use Object#map_over
|
|
71
79
|
converter.map_over [1,2,3], :hellos
|
72
80
|
# => ["hello", "hellohello", "hellohellohello"]
|
73
81
|
|
82
|
+
###I have an array of objects, and I'd like to sort them based on another enumerable.
|
83
|
+
|
84
|
+
class Thingy
|
85
|
+
def initialize(foo)
|
86
|
+
self.foo = foo
|
87
|
+
end
|
88
|
+
attr_accessor :foo
|
89
|
+
end
|
90
|
+
|
91
|
+
thingies = [Thingy.new("abc"), Thingy.new("def"), Thingy.new("ghi")]
|
92
|
+
arry = ["def", "abc", "ghi"]
|
93
|
+
|
94
|
+
thingies.sort_like(arry, :foo)
|
95
|
+
# OR
|
96
|
+
thingies.sort_like(arry) { |t| t.foo }
|
97
|
+
|
74
98
|
## Is this useful?
|
75
99
|
YES!!!!! Use it.
|
76
100
|
|
77
101
|
## Who built this
|
78
|
-
|
102
|
+
[Corey Haines](http://github.com/coreyhaines) and [Ryan Briones](http://github.com/ryanbriones)
|
103
|
+
|
104
|
+
## License
|
105
|
+
|
106
|
+
MIT. See [LICENSE](https://github.com/coreyhaines/enumeradical/blob/master/License.txt)
|
107
|
+
|
@@ -9,6 +9,13 @@ module Enumerable
|
|
9
9
|
do_map to, :new
|
10
10
|
end
|
11
11
|
|
12
|
+
def sort_like(arr, method = nil, &block)
|
13
|
+
sorted = arr.map { |a|
|
14
|
+
find { |s| (method ? s.send(method) : Proc.new(&block).call(s)) == a }
|
15
|
+
}.compact
|
16
|
+
sorted + (self - sorted)
|
17
|
+
end
|
18
|
+
|
12
19
|
private
|
13
20
|
def do_map(obj, method)
|
14
21
|
return self unless obj
|
data/lib/enumeradical/version.rb
CHANGED
@@ -4,23 +4,23 @@ Object.send :include, Enumeradical::CoreExtensions::Object
|
|
4
4
|
describe "CoreExtensions::Enumerable" do
|
5
5
|
describe "#map_into" do
|
6
6
|
example "empty array maps into empty array" do
|
7
|
-
[].map_into.
|
7
|
+
expect([].map_into).to be_empty
|
8
8
|
end
|
9
9
|
|
10
10
|
example "not passing an object is considered identity map" do
|
11
|
-
[1].map_into.
|
11
|
+
expect([1].map_into).to eq([1])
|
12
12
|
end
|
13
13
|
|
14
14
|
it "looks up the value from given object" do
|
15
|
-
[1].map_into({1 => "foo"}).
|
15
|
+
expect([1].map_into({1 => "foo"})).to eq(["foo"])
|
16
16
|
end
|
17
17
|
|
18
18
|
context "invalid situations" do
|
19
19
|
context "mapping into a non-indexable object" do
|
20
20
|
it "raises ArgumentError" do
|
21
|
-
|
21
|
+
expect {
|
22
22
|
[1].map_into(Object.new)
|
23
|
-
}.
|
23
|
+
}.to raise_error(ArgumentError, "argument must respond to []")
|
24
24
|
end
|
25
25
|
end
|
26
26
|
end
|
@@ -35,27 +35,70 @@ describe "CoreExtensions::Enumerable" do
|
|
35
35
|
end
|
36
36
|
|
37
37
|
example "empty array maps into empty array" do
|
38
|
-
[].map_to.
|
38
|
+
expect([].map_to).to be_empty
|
39
39
|
end
|
40
40
|
|
41
41
|
example "not passing an object is considered identity map" do
|
42
|
-
[1].map_to.
|
42
|
+
expect([1].map_to).to eq([1])
|
43
43
|
end
|
44
44
|
|
45
45
|
it "instantiates the given object with item from array" do
|
46
46
|
created_object = [2].map_to(ObjectWithOnConstructorArgument).first
|
47
|
-
created_object.arg.
|
47
|
+
expect(created_object.arg).to eq(2)
|
48
48
|
end
|
49
49
|
|
50
50
|
context "invalid situations" do
|
51
51
|
context "mapping into a object without .new" do
|
52
52
|
it "raises ArgumentError" do
|
53
|
-
|
53
|
+
expect {
|
54
54
|
[1].map_to(Object.new)
|
55
|
-
}.
|
55
|
+
}.to raise_error(ArgumentError, "argument must respond to new")
|
56
56
|
end
|
57
57
|
end
|
58
58
|
end
|
59
59
|
end
|
60
|
+
|
61
|
+
describe "#sort_like" do
|
62
|
+
|
63
|
+
class Sortable
|
64
|
+
def initialize(arg)
|
65
|
+
self.arg = arg
|
66
|
+
end
|
67
|
+
attr_accessor :arg
|
68
|
+
end
|
69
|
+
|
70
|
+
before do
|
71
|
+
@sample_array = [Sortable.new(14), Sortable.new(24), Sortable.new(34), Sortable.new(44)]
|
72
|
+
@template_array = [24, 44, 14, 34]
|
73
|
+
end
|
74
|
+
|
75
|
+
example "sorts like the template using the given method name" do
|
76
|
+
sorted = @sample_array.sort_like(@template_array, :arg)
|
77
|
+
expect(sorted.map(&:arg)).to eq(@template_array)
|
78
|
+
end
|
79
|
+
|
80
|
+
example "sorts like the template using the given block" do
|
81
|
+
sorted = @sample_array.sort_like(@template_array) { |x| x.arg }
|
82
|
+
expect(sorted.map(&:arg)).to eq(@template_array)
|
83
|
+
end
|
84
|
+
|
85
|
+
example "sorts correctly even if template array includes extra items" do
|
86
|
+
@template_array = ([54] + @template_array + [4])
|
87
|
+
sorted = @sample_array.sort_like(@template_array, :arg)
|
88
|
+
expect(sorted.map(&:arg)).to eq(@template_array - [4, 54])
|
89
|
+
end
|
90
|
+
|
91
|
+
example "sorts correctly even if the template array includes fewer items" do
|
92
|
+
@template_array = (@template_array - [14])
|
93
|
+
sorted = @sample_array.sort_like(@template_array) { |x| x.arg }
|
94
|
+
expect(sorted.map(&:arg)).to eq([24, 44, 34, 14])
|
95
|
+
end
|
96
|
+
|
97
|
+
example "raises an error if the item doesn't respond to the given method" do
|
98
|
+
expect {
|
99
|
+
@sample_array.sort_like(@template_array, :foo)
|
100
|
+
}.to raise_error(NoMethodError)
|
101
|
+
end
|
102
|
+
end
|
60
103
|
end
|
61
104
|
|
@@ -12,22 +12,22 @@ describe "CoreExtensions::Object" do
|
|
12
12
|
let(:object) { MixedIn.new }
|
13
13
|
let(:list) { [1,2,3] }
|
14
14
|
it "maps the method over each element of the given list" do
|
15
|
-
object.map_over(list, :multiple_by_two).
|
15
|
+
expect(object.map_over(list, :multiple_by_two)).to eq([2,4,6])
|
16
16
|
end
|
17
17
|
|
18
18
|
it "supports passing a string as the name of the method" do
|
19
|
-
object.map_over(list, "multiple_by_two").
|
19
|
+
expect(object.map_over(list, "multiple_by_two")).to eq([2,4,6])
|
20
20
|
end
|
21
21
|
|
22
22
|
context "invalid usage" do
|
23
23
|
it "requires a list that can be iterated over" do
|
24
|
-
|
25
|
-
object.map_over(Object.new, :multiple_by_two).
|
26
|
-
}.
|
24
|
+
expect {
|
25
|
+
expect(object.map_over(Object.new, :multiple_by_two)).to eq([2,4,6])
|
26
|
+
}.to raise_error(ArgumentError, "given list must support map")
|
27
27
|
end
|
28
28
|
|
29
29
|
it "returns an empty list if list is nil" do
|
30
|
-
object.map_over(nil, :multiple_by_two).
|
30
|
+
expect(object.map_over(nil, :multiple_by_two)).to be_empty
|
31
31
|
end
|
32
32
|
end
|
33
33
|
end
|
metadata
CHANGED
@@ -1,38 +1,43 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: enumeradical
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
5
|
-
prerelease:
|
4
|
+
version: 6.0.0
|
6
5
|
platform: ruby
|
7
6
|
authors:
|
8
7
|
- coreyhaines
|
9
8
|
autorequire:
|
10
9
|
bindir: bin
|
11
10
|
cert_chain: []
|
12
|
-
date:
|
11
|
+
date: 2015-02-14 00:00:00.000000000 Z
|
13
12
|
dependencies:
|
14
13
|
- !ruby/object:Gem::Dependency
|
15
14
|
name: travis-lint
|
16
|
-
requirement:
|
17
|
-
none: false
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
18
16
|
requirements:
|
19
|
-
- -
|
17
|
+
- - ">="
|
20
18
|
- !ruby/object:Gem::Version
|
21
19
|
version: '0'
|
22
20
|
type: :development
|
23
21
|
prerelease: false
|
24
|
-
version_requirements:
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
25
27
|
- !ruby/object:Gem::Dependency
|
26
28
|
name: rspec
|
27
|
-
requirement:
|
28
|
-
none: false
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
29
30
|
requirements:
|
30
|
-
- -
|
31
|
+
- - ">="
|
31
32
|
- !ruby/object:Gem::Version
|
32
33
|
version: '0'
|
33
34
|
type: :development
|
34
35
|
prerelease: false
|
35
|
-
version_requirements:
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
36
41
|
description: Enumeradical is a collection of useful functions for iterating over collections
|
37
42
|
in common ways.
|
38
43
|
email:
|
@@ -41,11 +46,12 @@ executables: []
|
|
41
46
|
extensions: []
|
42
47
|
extra_rdoc_files: []
|
43
48
|
files:
|
44
|
-
- .gitignore
|
45
|
-
- .rspec
|
46
|
-
- .rvmrc
|
47
|
-
- .travis.yml
|
49
|
+
- ".gitignore"
|
50
|
+
- ".rspec"
|
51
|
+
- ".rvmrc"
|
52
|
+
- ".travis.yml"
|
48
53
|
- Gemfile
|
54
|
+
- License.txt
|
49
55
|
- README.markdown
|
50
56
|
- Rakefile
|
51
57
|
- enumeradical.gemspec
|
@@ -57,27 +63,26 @@ files:
|
|
57
63
|
- spec/core_extensions/object_spec.rb
|
58
64
|
homepage:
|
59
65
|
licenses: []
|
66
|
+
metadata: {}
|
60
67
|
post_install_message:
|
61
68
|
rdoc_options: []
|
62
69
|
require_paths:
|
63
70
|
- lib
|
64
71
|
required_ruby_version: !ruby/object:Gem::Requirement
|
65
|
-
none: false
|
66
72
|
requirements:
|
67
|
-
- -
|
73
|
+
- - ">="
|
68
74
|
- !ruby/object:Gem::Version
|
69
75
|
version: '0'
|
70
76
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
71
|
-
none: false
|
72
77
|
requirements:
|
73
|
-
- -
|
78
|
+
- - ">="
|
74
79
|
- !ruby/object:Gem::Version
|
75
80
|
version: '0'
|
76
81
|
requirements: []
|
77
82
|
rubyforge_project: enumeradical
|
78
|
-
rubygems_version:
|
83
|
+
rubygems_version: 2.4.5
|
79
84
|
signing_key:
|
80
|
-
specification_version:
|
85
|
+
specification_version: 4
|
81
86
|
summary: Enumeradical takes bland enumerable functions and uses them to create AMAZINGALITY!
|
82
87
|
test_files:
|
83
88
|
- spec/core_extensions/enumerable_spec.rb
|