find_and_map 0.0.1 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.ruby-gemset +1 -0
- data/.ruby-version +1 -0
- data/Gemfile +1 -1
- data/README.markdown +32 -6
- data/find_and_map.gemspec +6 -0
- data/lib/find_and_map.rb +2 -1
- data/lib/find_and_map/core_ext.rb +8 -0
- data/lib/find_and_map/version.rb +1 -1
- data/spec/core_ext.rb +8 -0
- data/spec/core_ext_spec.rb +15 -0
- data/spec/find_and_map_spec.rb +12 -5
- metadata +85 -11
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: ec8bda77640bab8d4459ced41874cd2f3ab65cf3
|
4
|
+
data.tar.gz: 2006dfe86966a77770c4c481705d80cb116bd830
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 2ef7aaec1f5c473a9f50008b7c7dbc8f1151dad0eaf22171116a9a36266340fdfca8fcb4051dc72312fc71d671621044d0a41075bcfdf559497130b07231f159
|
7
|
+
data.tar.gz: 7ffb9f11e3aa8c99b338f2f65dd357ec86d130f7cca348b37dd65eddde118afd11ac3f075fa75cd153c1ac2f4f88836edf834a9e00f1d860058fe269a94d5b6d
|
data/.ruby-gemset
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
find_and_map
|
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
ruby-2.0.0-p247
|
data/Gemfile
CHANGED
data/README.markdown
CHANGED
@@ -1,19 +1,45 @@
|
|
1
1
|
Find and Map
|
2
2
|
============
|
3
3
|
|
4
|
-
Anthony M. Cook (
|
4
|
+
Anthony M. Cook (2013)
|
5
5
|
|
6
|
-
|
6
|
+
Finds and retuns the value of the block.
|
7
7
|
|
8
|
-
|
8
|
+
Usage
|
9
|
+
-----
|
9
10
|
|
10
|
-
|
11
|
+
Put it in your Gemfile:
|
12
|
+
|
13
|
+
```ruby
|
14
|
+
gem 'find_and_map'
|
15
|
+
```
|
16
|
+
|
17
|
+
Then you can include it on your own Enumerable object:
|
18
|
+
|
19
|
+
```ruby
|
20
|
+
include FindAndMap
|
21
|
+
```
|
22
|
+
|
23
|
+
Or you can use the core extension directly the Gemfile:
|
24
|
+
|
25
|
+
```ruby
|
26
|
+
gem 'find_and_map', require: 'find_and_map/core_ext'
|
27
|
+
```
|
28
|
+
|
29
|
+
Then you can use it like this:
|
30
|
+
|
31
|
+
```ruby
|
11
32
|
{a: 1, b: 2}.find_and_map do |key, value|
|
12
33
|
value if key == :a
|
13
34
|
end #=> 1
|
14
|
-
|
35
|
+
```
|
36
|
+
|
37
|
+
Background
|
38
|
+
----------
|
39
|
+
|
40
|
+
I was looking for a way to simply find the first matching element in an enumerable and return a mapped result.
|
15
41
|
|
16
|
-
Using the same block above `find` returns `[:a, 1]` and `map` returns `[1, nil]`. But I want just the value, or even some other object based on it, but only that and nothing else.
|
42
|
+
Using the same block above (in the Usage section) `find` returns `[:a, 1]` and `map` returns `[1, nil]`. But I want just the value, or even some other object based on it, but only that and nothing else.
|
17
43
|
|
18
44
|
This method finds the first match (like `Enumerable#find`) and then return the result of the block rather than the matching element (like `Enumerable#map`).
|
19
45
|
|
data/find_and_map.gemspec
CHANGED
@@ -16,4 +16,10 @@ Gem::Specification.new do |gem|
|
|
16
16
|
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
17
17
|
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
18
18
|
gem.require_paths = ["lib"]
|
19
|
+
|
20
|
+
gem.add_development_dependency 'bundler', '~> 1.3'
|
21
|
+
gem.add_development_dependency 'rake'
|
22
|
+
gem.add_development_dependency 'rspec', '~> 2.13'
|
23
|
+
gem.add_development_dependency 'pry'
|
24
|
+
gem.add_development_dependency 'pry-theme'
|
19
25
|
end
|
data/lib/find_and_map.rb
CHANGED
data/lib/find_and_map/version.rb
CHANGED
data/spec/core_ext.rb
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
require 'rspec'
|
2
|
+
|
3
|
+
describe "Core Extension" do
|
4
|
+
before :all do
|
5
|
+
require_relative '../lib/find_and_map/core_ext'
|
6
|
+
end
|
7
|
+
|
8
|
+
after :all do
|
9
|
+
Enumerable.send :remove_method, :find_and_map
|
10
|
+
end
|
11
|
+
|
12
|
+
it 'is automatically included on Enumerable objects' do
|
13
|
+
Hash.instance_methods.should include(:find_and_map)
|
14
|
+
end
|
15
|
+
end
|
data/spec/find_and_map_spec.rb
CHANGED
@@ -1,17 +1,24 @@
|
|
1
1
|
require 'rspec'
|
2
|
-
require 'pry'
|
3
2
|
require_relative '../lib/find_and_map'
|
4
3
|
|
5
4
|
describe FindAndMap do
|
6
|
-
|
5
|
+
let(:data) { [1, 2, 3] }
|
6
|
+
let(:collection) { data.extend subject; data }
|
7
|
+
it { should be_a(Module) }
|
7
8
|
|
8
9
|
it 'finds the first item that returns non-nil and returns the result' do
|
9
|
-
|
10
|
-
a.extend FindAndMap
|
11
|
-
a.find_and_map do |element|
|
10
|
+
collection.find_and_map do |element|
|
12
11
|
if element == 2 then
|
13
12
|
'hi!'
|
14
13
|
end
|
15
14
|
end.should == 'hi!'
|
16
15
|
end
|
16
|
+
|
17
|
+
it 'returns nil if nothing found' do
|
18
|
+
collection.find_and_map do |element|
|
19
|
+
if element == 500 then
|
20
|
+
'BOGUS'
|
21
|
+
end
|
22
|
+
end.should == nil
|
23
|
+
end
|
17
24
|
end
|
metadata
CHANGED
@@ -1,16 +1,85 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: find_and_map
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
5
|
-
prerelease:
|
4
|
+
version: 1.0.0
|
6
5
|
platform: ruby
|
7
6
|
authors:
|
8
7
|
- Anthony Cook
|
9
8
|
autorequire:
|
10
9
|
bindir: bin
|
11
10
|
cert_chain: []
|
12
|
-
date: 2013-
|
13
|
-
dependencies:
|
11
|
+
date: 2013-08-10 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: '2.13'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ~>
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '2.13'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: pry
|
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
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: pry-theme
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - '>='
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - '>='
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
14
83
|
description: Finds the first element for which the block returns non-falsy and returns
|
15
84
|
the value.
|
16
85
|
email:
|
@@ -21,38 +90,43 @@ extra_rdoc_files: []
|
|
21
90
|
files:
|
22
91
|
- .gitignore
|
23
92
|
- .rspec
|
93
|
+
- .ruby-gemset
|
94
|
+
- .ruby-version
|
24
95
|
- Gemfile
|
25
|
-
- Gemfile.lock
|
26
96
|
- LICENSE.txt
|
27
97
|
- README.markdown
|
28
98
|
- Rakefile
|
29
99
|
- find_and_map.gemspec
|
30
100
|
- lib/find_and_map.rb
|
101
|
+
- lib/find_and_map/core_ext.rb
|
31
102
|
- lib/find_and_map/version.rb
|
103
|
+
- spec/core_ext.rb
|
104
|
+
- spec/core_ext_spec.rb
|
32
105
|
- spec/find_and_map_spec.rb
|
33
106
|
homepage: http://github.com/acook/find_and_map
|
34
107
|
licenses: []
|
108
|
+
metadata: {}
|
35
109
|
post_install_message:
|
36
110
|
rdoc_options: []
|
37
111
|
require_paths:
|
38
112
|
- lib
|
39
113
|
required_ruby_version: !ruby/object:Gem::Requirement
|
40
|
-
none: false
|
41
114
|
requirements:
|
42
|
-
- -
|
115
|
+
- - '>='
|
43
116
|
- !ruby/object:Gem::Version
|
44
117
|
version: '0'
|
45
118
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
46
|
-
none: false
|
47
119
|
requirements:
|
48
|
-
- -
|
120
|
+
- - '>='
|
49
121
|
- !ruby/object:Gem::Version
|
50
122
|
version: '0'
|
51
123
|
requirements: []
|
52
124
|
rubyforge_project:
|
53
|
-
rubygems_version:
|
125
|
+
rubygems_version: 2.0.6
|
54
126
|
signing_key:
|
55
|
-
specification_version:
|
127
|
+
specification_version: 4
|
56
128
|
summary: Hybrid of find and map!
|
57
129
|
test_files:
|
130
|
+
- spec/core_ext.rb
|
131
|
+
- spec/core_ext_spec.rb
|
58
132
|
- spec/find_and_map_spec.rb
|