hash_selectors 0.0.3 → 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +3 -1
- data/lib/hash_selectors.rb +15 -2
- data/spec/hash_selectors_spec.rb +50 -21
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b54f7f8b25b1ed4b3916fc65153e3126d63813ea
|
4
|
+
data.tar.gz: 750f16927853a5e0e67bdaf9f201acecec13ae74
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 32ec80d8259e884168654271782a4abff1d62bd405812c1607b477d868936be712ba075d96f7a2d6b3be39ea8134d099b9ee60fef5e23327c279960d06a146a6
|
7
|
+
data.tar.gz: 44bb7d92ef2c2976c907ddd6d4fbb9b1c69e260f01ab0332d5b49f9eca61a40a6d174ea841ef5d91c676579797d6dd00c3574df3c8e15da40a6c0eb40b072cf5
|
data/README.md
CHANGED
@@ -3,9 +3,11 @@ hash-selectors
|
|
3
3
|
|
4
4
|
A small set of select methods for Ruby Hashes
|
5
5
|
|
6
|
-
# select_by...
|
6
|
+
# select_by... (or filter_by...)
|
7
|
+
{a: 1, b: 2, c: 3}.filter_by_keys :a, :b # returns {a: 1, b: 2}
|
7
8
|
{a: 1, b: 2, c: 3}.select_by_keys :a, :b # returns {a: 1, b: 2}
|
8
9
|
|
10
|
+
{a: 1, b: 2, c: 3}.filter_by_values 1, 3 # returns {a: 1, c: 3}
|
9
11
|
{a: 1, b: 2, c: 3}.select_by_values 1, 3 # returns {a: 1, c: 3}
|
10
12
|
|
11
13
|
# reject_by...
|
data/lib/hash_selectors.rb
CHANGED
@@ -2,11 +2,22 @@
|
|
2
2
|
# Automatically mixed-in to Hash on load.
|
3
3
|
module HashSelectors
|
4
4
|
|
5
|
+
# @example
|
6
|
+
# {a: {ak: :av}, b: 2, c: 3}.merge_into(:a, {new_k: :new_v}) # returns {a: {ak: :av, new_k: :new_v}, b: 2, c: 3}
|
7
|
+
#
|
8
|
+
# @param [Any] Key to merge at
|
9
|
+
# @param [Hash] Hash of new pairs to merge at the Key
|
10
|
+
# @return [Hash] Merged results
|
11
|
+
def merge_into(the_key, hash_to_merge)
|
12
|
+
sub_hash = self[the_key] || {}
|
13
|
+
merge(the_key => sub_hash.merge(hash_to_merge))
|
14
|
+
end
|
15
|
+
|
5
16
|
# @example
|
6
17
|
# {a: 1, b: 2, c: 3}.partition_by_keys :a, :b # returns [{a: 1, b: 2}, {c: 3}]
|
7
18
|
#
|
8
19
|
# @param [Glob of any type] ks
|
9
|
-
# @return [
|
20
|
+
# @return [Array of Hashes] Partitioned results based on whether keys are included within the *ks* argument.
|
10
21
|
def partition_by_keys(*ks)
|
11
22
|
### OPTIMIZE: Simple partition with the block results in nested Arrays. Investigate.
|
12
23
|
blk = lambda { |k,v| ks.include?(k) }
|
@@ -17,7 +28,7 @@ module HashSelectors
|
|
17
28
|
# {a: 1, b: 2, c: 3}.partition_by_values 2, 3 # returns [{b: 2, c: 3}, {a: 1}]
|
18
29
|
#
|
19
30
|
# @param [Glob of any type] vs
|
20
|
-
# @return [
|
31
|
+
# @return [Array of Hashes] Partitioned results based on whether values are included within the *vs* argument.
|
21
32
|
def partition_by_values(*vs)
|
22
33
|
### OPTIMIZE: Simple partition with the block results in nested Arrays. Investigate.
|
23
34
|
blk = lambda { |k,v| vs.include?(v) }
|
@@ -50,6 +61,7 @@ module HashSelectors
|
|
50
61
|
def select_by_keys(*ks)
|
51
62
|
select { |k,v| ks.include?(k) }
|
52
63
|
end
|
64
|
+
alias_method :filter_by_keys, :select_by_keys
|
53
65
|
|
54
66
|
# @example
|
55
67
|
# {a: 1, b: 2, c: 3}.select_by_values 2, 3 # returns {b: 2, c: 3}
|
@@ -59,6 +71,7 @@ module HashSelectors
|
|
59
71
|
def select_by_values(*vs)
|
60
72
|
select { |k,v| vs.include?(v) }
|
61
73
|
end
|
74
|
+
alias_method :filter_by_values, :select_by_values
|
62
75
|
|
63
76
|
# @example
|
64
77
|
# {a: 1, b: 2, c: 3}.values_for_keys :a, :b # returns [1, 2]
|
data/spec/hash_selectors_spec.rb
CHANGED
@@ -7,6 +7,30 @@ RSpec.describe HashSelectors do
|
|
7
7
|
|
8
8
|
### INSTANCE METHODS
|
9
9
|
|
10
|
+
describe "#merge_into" do
|
11
|
+
context "assuming a Hash: {a: {}, b: 2, c: 3}}" do
|
12
|
+
let(:the_hash) { {a: {}, b: 2, c: 3} }
|
13
|
+
context "and given :a, {a1: :a2}" do
|
14
|
+
subject { the_hash.merge_into(:a, {a1: :a2}) }
|
15
|
+
it { is_expected.to eq({a: {a1: :a2}, b: 2, c: 3}) }
|
16
|
+
end
|
17
|
+
end
|
18
|
+
context "assuming a Hash: {a: {1 => 2}, b: 2, c: 3}}" do
|
19
|
+
let(:the_hash) { {a: {1 => 2}, b: 2, c: 3} }
|
20
|
+
context "and given :a, {a1: :a2}" do
|
21
|
+
subject { the_hash.merge_into(:a, {a1: :a2}) }
|
22
|
+
it { is_expected.to eq({a: {:a1 => :a2, 1 => 2}, b: 2, c: 3}) }
|
23
|
+
end
|
24
|
+
end
|
25
|
+
context "assuming a Hash: {a: {1 => 2}, b: 2, c: 3}}" do
|
26
|
+
let(:the_hash) { {a: {1 => 2}, b: 2, c: 3} }
|
27
|
+
context "and given :d, {d1: :d2}" do
|
28
|
+
subject { the_hash.merge_into(:d, {d1: :d2}) }
|
29
|
+
it { is_expected.to eq({a: {1 => 2}, b: 2, c: 3, d: {d1: :d2}}) }
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
10
34
|
describe "#partition_by_keys" do
|
11
35
|
context "assuming a Hash: {a: 1, b: 2, c: {c2: [:d, :e]}}" do
|
12
36
|
let(:the_hash) { {a: 1, b: 2, c: {c2: [:d, :e]}} }
|
@@ -63,30 +87,35 @@ RSpec.describe HashSelectors do
|
|
63
87
|
end
|
64
88
|
end
|
65
89
|
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
context "
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
90
|
+
selection_prefixes = %w{ select filter }
|
91
|
+
selection_prefixes.each do |selection_prefix|
|
92
|
+
describe "##{selection_prefix}_by_keys" do
|
93
|
+
context "assuming a Hash: {a: 1, b: 2, c: {c2: [:d, :e]}}" do
|
94
|
+
let(:the_hash) { {a: 1, b: 2, c: {c2: [:d, :e]}} }
|
95
|
+
let(:the_meth) { "#{selection_prefix}_by_keys" }
|
96
|
+
context "and given :a, :d" do
|
97
|
+
subject { the_hash.send(the_meth, :a, :d) }
|
98
|
+
it { is_expected.to eq({a: 1}) }
|
99
|
+
end
|
100
|
+
context "and given :a, :b" do
|
101
|
+
subject { the_hash.send(the_meth, :a, :b) }
|
102
|
+
it { is_expected.to eq({a: 1, b: 2}) }
|
103
|
+
end
|
104
|
+
context "and given :b, :c" do
|
105
|
+
subject { the_hash.send(the_meth, :b, :c) }
|
106
|
+
it { is_expected.to eq({b: 2, c: {c2: [:d, :e]}}) }
|
107
|
+
end
|
80
108
|
end
|
81
109
|
end
|
82
|
-
end
|
83
110
|
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
111
|
+
describe "##{selection_prefix}_by_values" do
|
112
|
+
let(:the_meth) { "#{selection_prefix}_by_values" }
|
113
|
+
context "assuming a Hash: {a: 1, b: 2, c: {c2: [:d, :e]}}" do
|
114
|
+
let(:the_hash) { {a: 1, b: 2, c: {c2: [:d, :e]}} }
|
115
|
+
context "and given 1, 2" do
|
116
|
+
subject { the_hash.send(the_meth, 1, 2) }
|
117
|
+
it { is_expected.to eq({a: 1, b: 2}) }
|
118
|
+
end
|
90
119
|
end
|
91
120
|
end
|
92
121
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: hash_selectors
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Kevin C. Baird
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-08-
|
11
|
+
date: 2014-08-22 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rspec
|