select_where 1.0.2 → 1.1.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.
- checksums.yaml +4 -4
- data/lib/select_where/core_ext/array.rb +6 -0
- data/lib/select_where/helpers.rb +34 -0
- data/lib/select_where.rb +4 -30
- data/readme.md +16 -6
- data/select_where.gemspec +1 -1
- data/spec/unit/core_ext/array_spec.rb +25 -1
- data/spec/unit/helpers_spec.rb +53 -0
- data/spec/unit/select_where_spec.rb +13 -42
- metadata +5 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2eff9b6daf15e41df7aff3ce465df8655b46eab8
|
4
|
+
data.tar.gz: 66bc2d5652fb0cd5620943ba06dfe03379110962
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 92de75975ea411970bc113189fae346250fde874e50fa396b8d55c2b98a2288752dfba3e1b91ff4abfa3a9c74b5503f304f4cceadde401b2966c450e7f53a8dc
|
7
|
+
data.tar.gz: 603752f4d0c11754fc5758431bb5ccad05edd44235043a9721cd03f8a0f71c89c1e738cc847f72f4c6676d7d2b06f736ca4de1c863481fd68e6e433f0d7a3650
|
@@ -11,3 +11,9 @@ if !Array.method_defined?(:select_where)
|
|
11
11
|
define_method :select_where do |match_hash| ::SelectWhere.select(self, match_hash) end
|
12
12
|
end
|
13
13
|
end
|
14
|
+
|
15
|
+
if !Array.method_defined?(:fetch_map)
|
16
|
+
class Array
|
17
|
+
define_method :fetch_map do |*args, &block| ::SelectWhere.fetch_map(self, *args, &block) end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
|
2
|
+
module SelectWhere
|
3
|
+
module Helpers
|
4
|
+
|
5
|
+
def self.item_matches_target?(item, input_target, value)
|
6
|
+
target = input_target.to_sym
|
7
|
+
|
8
|
+
return false if !item.is_a?(Object)
|
9
|
+
return false if item.is_a?(Proc)
|
10
|
+
|
11
|
+
if item.class.method_defined?(target)
|
12
|
+
if item.class.instance_method(target).parameters.length == 0
|
13
|
+
return true if value_matches_target?(item.send(target), value)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
if item.class.method_defined?(:[])
|
18
|
+
return true if value_matches_target?(item[target], value)
|
19
|
+
return true if value_matches_target?(item[target.to_s], value)
|
20
|
+
end
|
21
|
+
|
22
|
+
return false
|
23
|
+
end
|
24
|
+
|
25
|
+
def self.value_matches_target?(target, value)
|
26
|
+
if value.respond_to?(:call)
|
27
|
+
value.call(target)
|
28
|
+
else
|
29
|
+
target == value
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
end
|
34
|
+
end
|
data/lib/select_where.rb
CHANGED
@@ -1,3 +1,4 @@
|
|
1
|
+
require 'select_where/helpers'
|
1
2
|
|
2
3
|
module SelectWhere
|
3
4
|
|
@@ -9,35 +10,8 @@ module SelectWhere
|
|
9
10
|
array.select { |i| match_hash.each.all? { |k,v| ::SelectWhere::Helpers.item_matches_target?(i, k, v) }}
|
10
11
|
end
|
11
12
|
|
12
|
-
|
13
|
-
|
14
|
-
def self.item_matches_target?(item, input_target, value)
|
15
|
-
target = input_target.to_sym
|
16
|
-
|
17
|
-
return false if !item.is_a?(Object)
|
18
|
-
return false if item.is_a?(Proc)
|
19
|
-
|
20
|
-
if item.class.method_defined?(target)
|
21
|
-
if item.class.instance_method(target).parameters.length == 0
|
22
|
-
return true if value_matches_target?(item.send(target), value)
|
23
|
-
end
|
24
|
-
end
|
25
|
-
|
26
|
-
if item.class.method_defined?(:[])
|
27
|
-
return true if value_matches_target?(item[target], value)
|
28
|
-
return true if value_matches_target?(item[target.to_s], value)
|
29
|
-
end
|
30
|
-
|
31
|
-
return false
|
32
|
-
end
|
33
|
-
|
34
|
-
def self.value_matches_target?(target, value)
|
35
|
-
if value.respond_to?(:call)
|
36
|
-
value.call(target)
|
37
|
-
else
|
38
|
-
target == value
|
39
|
-
end
|
40
|
-
end
|
41
|
-
|
13
|
+
def self.fetch_map(array, *args, &block)
|
14
|
+
array.map { |i| i.fetch(*args, &block) }
|
42
15
|
end
|
16
|
+
|
43
17
|
end
|
data/readme.md
CHANGED
@@ -10,7 +10,10 @@ Add the following line to your Gemfile:
|
|
10
10
|
gem 'select_where'
|
11
11
|
```
|
12
12
|
|
13
|
-
|
13
|
+
|
14
|
+
##### Methods
|
15
|
+
|
16
|
+
###### select_where / detect_where
|
14
17
|
|
15
18
|
```ruby
|
16
19
|
require 'select_where'
|
@@ -19,15 +22,22 @@ arr = [{ a: 1 }, { a: 2 }]
|
|
19
22
|
SelectWhere.select(arr, { a: 2 }) # => [{ a: 2}]
|
20
23
|
SelectWhere.detect(arr, { a: 2 }) # => { a: 2}
|
21
24
|
SelectWhere.select(arr, { a: ->(v) { v > 0 } }) # => [{ a: 1 }, { a: 2 }]
|
22
|
-
```
|
23
|
-
|
24
|
-
##### As an extension
|
25
25
|
|
26
|
-
```ruby
|
27
26
|
require 'select_where/core_ext/array'
|
28
|
-
|
29
27
|
arr = [{ a: 1 }, { a: 2 }]
|
30
28
|
arr.select_where({ a: 2 }) # => [{ a: 2}]
|
31
29
|
arr.detect_where({ a: 2 }) # => { a: 2}
|
32
30
|
arr.select_where({ a: ->(v) { v > 0 } }) # => [{ a: 1 }, { a: 2 }]
|
33
31
|
```
|
32
|
+
|
33
|
+
###### fetch_map
|
34
|
+
|
35
|
+
```ruby
|
36
|
+
require 'select_where'
|
37
|
+
|
38
|
+
arr = [{ a: 1 }, { a: 2 }, { b: 3 }]
|
39
|
+
SelectWhere.fetch_map(arr, :a, nil) # => [1, 2, nil]
|
40
|
+
|
41
|
+
require 'select_where/core_ext/array'
|
42
|
+
arr.fetch_map(:a, nil) # => [1, 2, nil]
|
43
|
+
```
|
data/select_where.gemspec
CHANGED
@@ -4,7 +4,7 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
|
4
4
|
|
5
5
|
Gem::Specification.new do |spec|
|
6
6
|
spec.name = "select_where"
|
7
|
-
spec.version = "1.0
|
7
|
+
spec.version = "1.1.0"
|
8
8
|
spec.authors = ["Trunkclub"]
|
9
9
|
spec.email = ["nickm@trunkclub.com"]
|
10
10
|
spec.description = "extends ruby array with select helpers"
|
@@ -46,7 +46,6 @@ describe Array do
|
|
46
46
|
|
47
47
|
end
|
48
48
|
|
49
|
-
|
50
49
|
describe Array.instance_method(:detect_where) do
|
51
50
|
|
52
51
|
it 'should find an item in an array of hashes' do
|
@@ -90,4 +89,29 @@ describe Array do
|
|
90
89
|
end
|
91
90
|
|
92
91
|
end
|
92
|
+
|
93
|
+
describe Array.instance_method(:fetch_map) do
|
94
|
+
|
95
|
+
it 'should implement fetch_map (key)' do
|
96
|
+
arr = [{ 'a' => 1 }, { 'a' => 2 }, { 'b' => 3}]
|
97
|
+
|
98
|
+
expect {
|
99
|
+
arr.fetch_map('a')
|
100
|
+
}.to raise_error(KeyError)
|
101
|
+
end
|
102
|
+
|
103
|
+
it 'should implement fetch_map (key, value)' do
|
104
|
+
arr = [{ 'a' => 1 }, { 'a' => 2 }, { 'b' => 3}]
|
105
|
+
expect(arr.fetch_map('a', 'default')).to eq([1, 2, 'default'])
|
106
|
+
end
|
107
|
+
|
108
|
+
it 'should implement fetch_map (key, block)' do
|
109
|
+
arr = [{ 'a' => 1 }, { 'a' => 2 }, { 'b' => 3}]
|
110
|
+
expect(arr.fetch_map('a') { 'default' }).to eq([1, 2, 'default'])
|
111
|
+
end
|
112
|
+
|
113
|
+
end
|
114
|
+
|
115
|
+
|
116
|
+
|
93
117
|
end
|
@@ -0,0 +1,53 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'select_where'
|
3
|
+
|
4
|
+
RSpec.describe SelectWhere::Helpers do
|
5
|
+
|
6
|
+
describe '::item_matches_target?' do
|
7
|
+
|
8
|
+
it 'should work with a symbol keyed hash' do
|
9
|
+
expect(SelectWhere::Helpers.item_matches_target?({ a: 10 }, 'a', 10)).to eq(true)
|
10
|
+
expect(SelectWhere::Helpers.item_matches_target?({ a: 10 }, :a, 10)).to eq(true)
|
11
|
+
end
|
12
|
+
|
13
|
+
it 'should work with a string keyed hash' do
|
14
|
+
expect(SelectWhere::Helpers.item_matches_target?({ 'a' => 10 }, 'a', 10)).to eq(true)
|
15
|
+
expect(SelectWhere::Helpers.item_matches_target?({ 'a' => 10 }, :a, 10)).to eq(true)
|
16
|
+
end
|
17
|
+
|
18
|
+
it 'should return false for a non-object or proc' do
|
19
|
+
expect(SelectWhere::Helpers.item_matches_target?(->() { 10 }, 'a', 10)).to eq(false)
|
20
|
+
expect(SelectWhere::Helpers.item_matches_target?(Proc.new { 10 }, 'a', 10)).to eq(false)
|
21
|
+
end
|
22
|
+
|
23
|
+
context 'value is a Proc' do
|
24
|
+
|
25
|
+
it 'call the proc with the found value' do
|
26
|
+
test_proc = ->(a) { a == 10 }
|
27
|
+
expect(SelectWhere::Helpers.item_matches_target?({ a: 10 }, 'a', test_proc)).to eq(true)
|
28
|
+
expect(SelectWhere::Helpers.item_matches_target?({ 'a' => 10 }, 'a', test_proc)).to eq(true)
|
29
|
+
end
|
30
|
+
|
31
|
+
end
|
32
|
+
|
33
|
+
end
|
34
|
+
|
35
|
+
describe '::value_matches_target?' do
|
36
|
+
|
37
|
+
it 'should work with simple equality' do
|
38
|
+
expect(SelectWhere::Helpers.value_matches_target?('a', 'a')).to eq(true)
|
39
|
+
end
|
40
|
+
|
41
|
+
it 'should work with proc values' do
|
42
|
+
value = -> (v) { v == 'a' }
|
43
|
+
expect(SelectWhere::Helpers.value_matches_target?('a', value)).to eq(true)
|
44
|
+
end
|
45
|
+
|
46
|
+
it 'should raise an exception if ' do
|
47
|
+
value = -> (v) { v == 'a' }
|
48
|
+
expect(SelectWhere::Helpers.value_matches_target?('a', value)).to eq(true)
|
49
|
+
end
|
50
|
+
|
51
|
+
end
|
52
|
+
|
53
|
+
end
|
@@ -1,53 +1,24 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
require 'select_where'
|
3
3
|
|
4
|
-
RSpec.describe SelectWhere
|
4
|
+
RSpec.describe SelectWhere do
|
5
5
|
|
6
|
-
|
7
|
-
|
8
|
-
it 'should work with a symbol keyed hash' do
|
9
|
-
expect(SelectWhere::Helpers.item_matches_target?({ a: 10 }, 'a', 10)).to eq(true)
|
10
|
-
expect(SelectWhere::Helpers.item_matches_target?({ a: 10 }, :a, 10)).to eq(true)
|
11
|
-
end
|
12
|
-
|
13
|
-
it 'should work with a string keyed hash' do
|
14
|
-
expect(SelectWhere::Helpers.item_matches_target?({ 'a' => 10 }, 'a', 10)).to eq(true)
|
15
|
-
expect(SelectWhere::Helpers.item_matches_target?({ 'a' => 10 }, :a, 10)).to eq(true)
|
16
|
-
end
|
17
|
-
|
18
|
-
it 'should return false for a non-object or proc' do
|
19
|
-
expect(SelectWhere::Helpers.item_matches_target?(->() { 10 }, 'a', 10)).to eq(false)
|
20
|
-
expect(SelectWhere::Helpers.item_matches_target?(Proc.new { 10 }, 'a', 10)).to eq(false)
|
21
|
-
end
|
22
|
-
|
23
|
-
context 'value is a Proc' do
|
24
|
-
|
25
|
-
it 'call the proc with the found value' do
|
26
|
-
test_proc = ->(a) { a == 10 }
|
27
|
-
expect(SelectWhere::Helpers.item_matches_target?({ a: 10 }, 'a', test_proc)).to eq(true)
|
28
|
-
expect(SelectWhere::Helpers.item_matches_target?({ 'a' => 10 }, 'a', test_proc)).to eq(true)
|
29
|
-
end
|
30
|
-
|
31
|
-
end
|
6
|
+
it 'should implement fetch_map (key)' do
|
7
|
+
arr = [{ 'a' => 1 }, { 'a' => 2 }, { 'b' => 3}]
|
32
8
|
|
9
|
+
expect {
|
10
|
+
SelectWhere.fetch_map(arr, 'a')
|
11
|
+
}.to raise_error(KeyError)
|
33
12
|
end
|
34
13
|
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
end
|
40
|
-
|
41
|
-
it 'should work with proc values' do
|
42
|
-
value = -> (v) { v == 'a' }
|
43
|
-
expect(SelectWhere::Helpers.value_matches_target?('a', value)).to eq(true)
|
44
|
-
end
|
45
|
-
|
46
|
-
it 'should raise an exception if ' do
|
47
|
-
value = -> (v) { v == 'a' }
|
48
|
-
expect(SelectWhere::Helpers.value_matches_target?('a', value)).to eq(true)
|
49
|
-
end
|
14
|
+
it 'should implement fetch_map (key, value)' do
|
15
|
+
arr = [{ 'a' => 1 }, { 'a' => 2 }, { 'b' => 3}]
|
16
|
+
expect(SelectWhere.fetch_map(arr, 'a', 'default')).to eq([1, 2, 'default'])
|
17
|
+
end
|
50
18
|
|
19
|
+
it 'should implement fetch_map (key, block)' do
|
20
|
+
arr = [{ 'a' => 1 }, { 'a' => 2 }, { 'b' => 3}]
|
21
|
+
expect(SelectWhere.fetch_map(arr, 'a') { 'default' }).to eq([1, 2, 'default'])
|
51
22
|
end
|
52
23
|
|
53
24
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: select_where
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0
|
4
|
+
version: 1.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Trunkclub
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-09-
|
11
|
+
date: 2016-09-12 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rake
|
@@ -80,10 +80,12 @@ files:
|
|
80
80
|
- LICENSE
|
81
81
|
- lib/select_where.rb
|
82
82
|
- lib/select_where/core_ext/array.rb
|
83
|
+
- lib/select_where/helpers.rb
|
83
84
|
- readme.md
|
84
85
|
- select_where.gemspec
|
85
86
|
- spec/spec_helper.rb
|
86
87
|
- spec/unit/core_ext/array_spec.rb
|
88
|
+
- spec/unit/helpers_spec.rb
|
87
89
|
- spec/unit/select_where_spec.rb
|
88
90
|
homepage: https://github.com/trunkclub/select_where
|
89
91
|
licenses:
|
@@ -112,4 +114,5 @@ summary: extends ruby array with select helpers
|
|
112
114
|
test_files:
|
113
115
|
- spec/spec_helper.rb
|
114
116
|
- spec/unit/core_ext/array_spec.rb
|
117
|
+
- spec/unit/helpers_spec.rb
|
115
118
|
- spec/unit/select_where_spec.rb
|