enumerable_deep_search 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/Guardfile +9 -0
- data/LICENSE.txt +22 -0
- data/README.md +46 -0
- data/Rakefile +1 -0
- data/enumerable_deep_search.gemspec +25 -0
- data/lib/enumerable_deep_search.rb +100 -0
- data/lib/enumerable_deep_search/version.rb +3 -0
- data/spec/enumerable_deep_search_spec.rb +92 -0
- data/spec/spec_helper.rb +16 -0
- metadata +204 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/Guardfile
ADDED
@@ -0,0 +1,9 @@
|
|
1
|
+
# A sample Guardfile
|
2
|
+
# More info at https://github.com/guard/guard#readme
|
3
|
+
|
4
|
+
guard 'rspec', version: 2, cli: '--color --format documentation' do
|
5
|
+
watch(%r{^spec/.+_spec\.rb$})
|
6
|
+
watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
|
7
|
+
watch('spec/spec_helper.rb') { "spec" }
|
8
|
+
end
|
9
|
+
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Anthony Cook
|
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,46 @@
|
|
1
|
+
# EnumerableDeepSearch
|
2
|
+
|
3
|
+
Recursively searches enumerable objects and their nested objects for a given object or string.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'enumerable_deep_search'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install enumerable_deep_search
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
In practice it's an exploration tool, I recommend using it with pry and extending objects as needed.
|
22
|
+
You can also include it on a class and use it the same way on instances.
|
23
|
+
|
24
|
+
example:
|
25
|
+
|
26
|
+
```ruby
|
27
|
+
object.extend EnumerableDeepSearch
|
28
|
+
object.search 'something'
|
29
|
+
```
|
30
|
+
|
31
|
+
It will return a facimile of the path to the matching object, for hash-like objects it will return a Hash containg the match as well as the opposite pair (either the key or the value), for array-like objects it will return a two element Array where the first element is the index of the match and second element the match itself.
|
32
|
+
|
33
|
+
example:
|
34
|
+
|
35
|
+
```ruby
|
36
|
+
object.search 'something'
|
37
|
+
#=> {:foo => [1, 'something']}
|
38
|
+
```
|
39
|
+
|
40
|
+
## Contributing
|
41
|
+
|
42
|
+
1. Fork it
|
43
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
44
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
45
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
46
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
@@ -0,0 +1,25 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'enumerable_deep_search/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |gem|
|
7
|
+
gem.name = "enumerable_deep_search"
|
8
|
+
gem.version = EnumerableDeepSearch::VERSION
|
9
|
+
gem.authors = ["Anthony Cook"]
|
10
|
+
gem.email = ["anthonymichaelcook@gmail.com"]
|
11
|
+
gem.description = %q{Recursively searches enumerable objects and their nested objects for a given object or string.}
|
12
|
+
gem.summary = %q{Recursively searches through enumerable objects.}
|
13
|
+
gem.homepage = "https://github.com/acook/enumerable_deep_search"
|
14
|
+
|
15
|
+
gem.files = `git ls-files`.split($/)
|
16
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
17
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
18
|
+
gem.require_paths = ["lib"]
|
19
|
+
|
20
|
+
%w{
|
21
|
+
rspec guard-rspec pry pry-nav pry-doc pry-theme pry-coolline pry-highlight pry-buffers
|
22
|
+
}.each do |g|
|
23
|
+
gem.add_development_dependency g
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,100 @@
|
|
1
|
+
require_relative 'enumerable_deep_search/version'
|
2
|
+
|
3
|
+
module EnumerableDeepSearch
|
4
|
+
def search item
|
5
|
+
research self, item
|
6
|
+
end
|
7
|
+
|
8
|
+
def out *args
|
9
|
+
puts "-- #{args.join(' ')}" if $VERBOSE || $DEBUG
|
10
|
+
end
|
11
|
+
|
12
|
+
def research object, item
|
13
|
+
if simple_research object, item then
|
14
|
+
out '# simple objects match'
|
15
|
+
object
|
16
|
+
elsif object.respond_to? :keys then
|
17
|
+
out 'keys'
|
18
|
+
hash_research object, item
|
19
|
+
elsif object.respond_to? :each then
|
20
|
+
out 'each'
|
21
|
+
array_research object, item
|
22
|
+
elsif object.respond_to? :to_hash then
|
23
|
+
out 'convert to hash'
|
24
|
+
research object.to_hash, item
|
25
|
+
elsif object.respond_to? :to_a then
|
26
|
+
out 'convert to array'
|
27
|
+
research object.to_a, item
|
28
|
+
else
|
29
|
+
out 'other:', object.class
|
30
|
+
other_research object, item
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
def hash_research object, item
|
35
|
+
out 'hash'
|
36
|
+
|
37
|
+
match = nil
|
38
|
+
object.find do |key, value|
|
39
|
+
match = research key, item
|
40
|
+
if match then
|
41
|
+
out '# match found (hash key):', key
|
42
|
+
match = {match => value}
|
43
|
+
else
|
44
|
+
out 'no match (hash key):', key
|
45
|
+
match = research value, item
|
46
|
+
match = {key => match} if match
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
if match then
|
51
|
+
out '# match:', match
|
52
|
+
else
|
53
|
+
out 'no match'
|
54
|
+
end
|
55
|
+
match
|
56
|
+
end
|
57
|
+
|
58
|
+
def array_research object, item
|
59
|
+
out 'array'
|
60
|
+
match = nil
|
61
|
+
object.each_with_index do |element, index|
|
62
|
+
match = research(element, item)
|
63
|
+
if match then
|
64
|
+
match = [index, match]
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
if match then
|
69
|
+
out '# match found:', match
|
70
|
+
else
|
71
|
+
out 'no match'
|
72
|
+
end
|
73
|
+
match
|
74
|
+
end
|
75
|
+
|
76
|
+
def simple_research object, item
|
77
|
+
if object == item then
|
78
|
+
out '# objects match'
|
79
|
+
object
|
80
|
+
elsif object.to_s == item.to_s then
|
81
|
+
out '# object strings match'
|
82
|
+
object
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
def other_research object, item
|
87
|
+
if simple_research object, item then
|
88
|
+
out '# simple match found'
|
89
|
+
object
|
90
|
+
elsif item.is_a?(Regexp) && object.to_s =~ item then
|
91
|
+
out '# regex match found'
|
92
|
+
object
|
93
|
+
elsif object.to_s =~ /#{item.to_s}/ then
|
94
|
+
out '# substring match found'
|
95
|
+
object
|
96
|
+
else
|
97
|
+
out 'no match:', object.class
|
98
|
+
end
|
99
|
+
end
|
100
|
+
end
|
@@ -0,0 +1,92 @@
|
|
1
|
+
require_relative 'spec_helper'
|
2
|
+
require 'enumerable_deep_search'
|
3
|
+
|
4
|
+
describe EnumerableDeepSearch do
|
5
|
+
let(:eds){Object.new.extend EnumerableDeepSearch}
|
6
|
+
|
7
|
+
it 'should exist' do
|
8
|
+
subject.should be
|
9
|
+
end
|
10
|
+
|
11
|
+
it 'should define methods on objects it extends' do
|
12
|
+
class DummyClass; end
|
13
|
+
dummy = DummyClass.new
|
14
|
+
dummy.extend EnumerableDeepSearch
|
15
|
+
dummy.should respond_to(:search)
|
16
|
+
end
|
17
|
+
|
18
|
+
describe '#search' do
|
19
|
+
it 'should proxy to research' do
|
20
|
+
eds.should_receive(:research)
|
21
|
+
eds.search :foo
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
describe '#research' do
|
26
|
+
context 'simple object matching' do
|
27
|
+
it 'should match identical objects' do
|
28
|
+
object = Object.new
|
29
|
+
result = eds.research object, object
|
30
|
+
result.should == object
|
31
|
+
end
|
32
|
+
|
33
|
+
it 'should match keys in a hash' do
|
34
|
+
hash = {'match' => 'bar'}
|
35
|
+
result = eds.research hash, 'match'
|
36
|
+
result.should == hash
|
37
|
+
end
|
38
|
+
|
39
|
+
it 'should match values in a hash' do
|
40
|
+
hash = {foo: 'match'}
|
41
|
+
result = eds.research hash, 'match'
|
42
|
+
result.should == hash
|
43
|
+
end
|
44
|
+
|
45
|
+
it 'should match values in an array' do
|
46
|
+
array = ['match']
|
47
|
+
result = eds.research array, 'match'
|
48
|
+
result.should == [0, 'match']
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
context 'nested structures' do
|
53
|
+
it 'should match values in nested hashes' do
|
54
|
+
hash = {foo: {bar: 'match'}}
|
55
|
+
result = eds.research hash, 'match'
|
56
|
+
result.should == hash
|
57
|
+
end
|
58
|
+
|
59
|
+
it 'should match keys in nested hashes' do
|
60
|
+
hash = {foo: {'match' => 1}}
|
61
|
+
result = eds.research hash, 'match'
|
62
|
+
result.should == hash
|
63
|
+
end
|
64
|
+
|
65
|
+
it 'should match elements in nested array' do
|
66
|
+
array = [:foo, [:bar, 'match']]
|
67
|
+
result = eds.research array, 'match'
|
68
|
+
result.should == [1, [1, 'match']]
|
69
|
+
end
|
70
|
+
|
71
|
+
it 'should match objects in mixed hashes and arrays' do
|
72
|
+
struture = [:foo, {bar: [1,2,3,{baz: 'match'}]}]
|
73
|
+
result = eds.research struture, 'match'
|
74
|
+
result.should == [1, {:bar=>[3, {:baz=>"match"}]}]
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
context 'matching enumerable objects' do
|
79
|
+
it 'should match full arrays' do
|
80
|
+
array = [1]
|
81
|
+
result = eds.research array, array
|
82
|
+
result.should == array
|
83
|
+
end
|
84
|
+
|
85
|
+
it 'should match full hashes' do
|
86
|
+
hash = {a: 1}
|
87
|
+
result = eds.research hash, hash
|
88
|
+
result.should == hash
|
89
|
+
end
|
90
|
+
end
|
91
|
+
end
|
92
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
$: << File.dirname(__FILE__)
|
2
|
+
$: << File.join($:.last, '..', 'lib')
|
3
|
+
|
4
|
+
require 'bundler'
|
5
|
+
Bundler.require :test
|
6
|
+
require 'pry'
|
7
|
+
|
8
|
+
def run *args
|
9
|
+
output = Struct.new :pid, :stdout, :stderr
|
10
|
+
|
11
|
+
status = Open4.popen4(*args) do |pid, stdin, stdout, stderr|
|
12
|
+
output = output.new pid, stdout.read, stderr.read
|
13
|
+
end
|
14
|
+
|
15
|
+
[status, output]
|
16
|
+
end
|
metadata
ADDED
@@ -0,0 +1,204 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: enumerable_deep_search
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Anthony Cook
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-08-22 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rspec
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: guard-rspec
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
type: :development
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: pry
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ! '>='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
- !ruby/object:Gem::Dependency
|
63
|
+
name: pry-nav
|
64
|
+
requirement: !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
66
|
+
requirements:
|
67
|
+
- - ! '>='
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '0'
|
70
|
+
type: :development
|
71
|
+
prerelease: false
|
72
|
+
version_requirements: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ! '>='
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '0'
|
78
|
+
- !ruby/object:Gem::Dependency
|
79
|
+
name: pry-doc
|
80
|
+
requirement: !ruby/object:Gem::Requirement
|
81
|
+
none: false
|
82
|
+
requirements:
|
83
|
+
- - ! '>='
|
84
|
+
- !ruby/object:Gem::Version
|
85
|
+
version: '0'
|
86
|
+
type: :development
|
87
|
+
prerelease: false
|
88
|
+
version_requirements: !ruby/object:Gem::Requirement
|
89
|
+
none: false
|
90
|
+
requirements:
|
91
|
+
- - ! '>='
|
92
|
+
- !ruby/object:Gem::Version
|
93
|
+
version: '0'
|
94
|
+
- !ruby/object:Gem::Dependency
|
95
|
+
name: pry-theme
|
96
|
+
requirement: !ruby/object:Gem::Requirement
|
97
|
+
none: false
|
98
|
+
requirements:
|
99
|
+
- - ! '>='
|
100
|
+
- !ruby/object:Gem::Version
|
101
|
+
version: '0'
|
102
|
+
type: :development
|
103
|
+
prerelease: false
|
104
|
+
version_requirements: !ruby/object:Gem::Requirement
|
105
|
+
none: false
|
106
|
+
requirements:
|
107
|
+
- - ! '>='
|
108
|
+
- !ruby/object:Gem::Version
|
109
|
+
version: '0'
|
110
|
+
- !ruby/object:Gem::Dependency
|
111
|
+
name: pry-coolline
|
112
|
+
requirement: !ruby/object:Gem::Requirement
|
113
|
+
none: false
|
114
|
+
requirements:
|
115
|
+
- - ! '>='
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: '0'
|
118
|
+
type: :development
|
119
|
+
prerelease: false
|
120
|
+
version_requirements: !ruby/object:Gem::Requirement
|
121
|
+
none: false
|
122
|
+
requirements:
|
123
|
+
- - ! '>='
|
124
|
+
- !ruby/object:Gem::Version
|
125
|
+
version: '0'
|
126
|
+
- !ruby/object:Gem::Dependency
|
127
|
+
name: pry-highlight
|
128
|
+
requirement: !ruby/object:Gem::Requirement
|
129
|
+
none: false
|
130
|
+
requirements:
|
131
|
+
- - ! '>='
|
132
|
+
- !ruby/object:Gem::Version
|
133
|
+
version: '0'
|
134
|
+
type: :development
|
135
|
+
prerelease: false
|
136
|
+
version_requirements: !ruby/object:Gem::Requirement
|
137
|
+
none: false
|
138
|
+
requirements:
|
139
|
+
- - ! '>='
|
140
|
+
- !ruby/object:Gem::Version
|
141
|
+
version: '0'
|
142
|
+
- !ruby/object:Gem::Dependency
|
143
|
+
name: pry-buffers
|
144
|
+
requirement: !ruby/object:Gem::Requirement
|
145
|
+
none: false
|
146
|
+
requirements:
|
147
|
+
- - ! '>='
|
148
|
+
- !ruby/object:Gem::Version
|
149
|
+
version: '0'
|
150
|
+
type: :development
|
151
|
+
prerelease: false
|
152
|
+
version_requirements: !ruby/object:Gem::Requirement
|
153
|
+
none: false
|
154
|
+
requirements:
|
155
|
+
- - ! '>='
|
156
|
+
- !ruby/object:Gem::Version
|
157
|
+
version: '0'
|
158
|
+
description: Recursively searches enumerable objects and their nested objects for
|
159
|
+
a given object or string.
|
160
|
+
email:
|
161
|
+
- anthonymichaelcook@gmail.com
|
162
|
+
executables: []
|
163
|
+
extensions: []
|
164
|
+
extra_rdoc_files: []
|
165
|
+
files:
|
166
|
+
- .gitignore
|
167
|
+
- Gemfile
|
168
|
+
- Guardfile
|
169
|
+
- LICENSE.txt
|
170
|
+
- README.md
|
171
|
+
- Rakefile
|
172
|
+
- enumerable_deep_search.gemspec
|
173
|
+
- lib/enumerable_deep_search.rb
|
174
|
+
- lib/enumerable_deep_search/version.rb
|
175
|
+
- spec/enumerable_deep_search_spec.rb
|
176
|
+
- spec/spec_helper.rb
|
177
|
+
homepage: https://github.com/acook/enumerable_deep_search
|
178
|
+
licenses: []
|
179
|
+
post_install_message:
|
180
|
+
rdoc_options: []
|
181
|
+
require_paths:
|
182
|
+
- lib
|
183
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
184
|
+
none: false
|
185
|
+
requirements:
|
186
|
+
- - ! '>='
|
187
|
+
- !ruby/object:Gem::Version
|
188
|
+
version: '0'
|
189
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
190
|
+
none: false
|
191
|
+
requirements:
|
192
|
+
- - ! '>='
|
193
|
+
- !ruby/object:Gem::Version
|
194
|
+
version: '0'
|
195
|
+
requirements: []
|
196
|
+
rubyforge_project:
|
197
|
+
rubygems_version: 1.8.24
|
198
|
+
signing_key:
|
199
|
+
specification_version: 3
|
200
|
+
summary: Recursively searches through enumerable objects.
|
201
|
+
test_files:
|
202
|
+
- spec/enumerable_deep_search_spec.rb
|
203
|
+
- spec/spec_helper.rb
|
204
|
+
has_rdoc:
|