hash_selectors 0.0.1 → 0.0.2

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 6f465f6bc5e31b99b2176ff434d1f9cff6a1f7a6
4
- data.tar.gz: 148130fcfc88301609302815eb212e9648010c83
3
+ metadata.gz: c54e88d54866fd6a2b4f084d3e07cd8bf79cad9a
4
+ data.tar.gz: 64124d0bc34c1beca634a2a5791b441f64bba35a
5
5
  SHA512:
6
- metadata.gz: 7c70c25c510e5f0dde0cfee605a23f5cd733d8438d91235d9d9bbf14f31a631d9819be0cd0068113e7f694218feba63ff9cf462fd57562e55858a4970e7cf5d7
7
- data.tar.gz: a35bcaf8270fe29a219322e88144b391681488ceee3927dc21343fa4a14af0e34d1642e19002626ac8fcc051818ef8d5e04732bc1022beda0b73ad8a3ea4e816
6
+ metadata.gz: d2ab23a176b61a530f96e8d36d62e45084504a8e14402d8c7e96a22f3cb4275823503a04c540aff93c3a0a56f27d47bf4a276305c6fcd9dc395f39a46f7ab443
7
+ data.tar.gz: 8ff3671d090084ae55b0f3151632de1703a99f0fb89a4fc996504b7af1c06cd874b38110cbc7e31c241067581de750060247b21c75607a0248845a0a905ed14b
data/README.md CHANGED
@@ -2,3 +2,18 @@ hash-selectors
2
2
  ==============
3
3
 
4
4
  A small set of select methods for Ruby Hashes
5
+
6
+ # select_by...
7
+ {a: 1, b: 2, c: 3}.select_by_keys :a, :b # returns {a: 1, b: 2}
8
+
9
+ {a: 1, b: 2, c: 3}.select_by_values 1, 3 # returns {a: 1, c: 3}
10
+
11
+ # reject_by...
12
+ {a: 1, b: 2, c: 3}.reject_by_keys :c # returns {a: 1, b: 2}
13
+
14
+ {a: 1, b: 2, c: 3}.reject_by_values 2 # returns {a: 1, c: 3}
15
+
16
+ # partition_by...
17
+ {a: 1, b: 2, c: 3}.partition_by_keys :a, :b # returns [{a: 1, b: 2}, {c: 3}]
18
+
19
+ {a: 1, b: 2, c: 3}.partition_by_values 1, 3 # returns [{a: 1, c: 3}, {b: 2}]
@@ -2,6 +2,46 @@
2
2
  # Automatically mixed-in to Hash on load.
3
3
  module HashSelectors
4
4
 
5
+ # @example
6
+ # {a: 1, b: 2, c: 3}.partition_by_keys :a, :b # returns [{a: 1, b: 2}, {c: 3}]
7
+ #
8
+ # @param [Glob of any type] ks
9
+ # @return [Hash] Partitioned results based on whether keys are included within the *ks* argument.
10
+ def partition_by_keys(*ks)
11
+ ### OPTIMIZE: Simple partition with the block results in nested Arrays. Investigate.
12
+ blk = lambda { |k,v| ks.include?(k) }
13
+ [select(&blk), reject(&blk)]
14
+ end
15
+
16
+ # @example
17
+ # {a: 1, b: 2, c: 3}.partition_by_values 2, 3 # returns [{b: 2, c: 3}, {a: 1}]
18
+ #
19
+ # @param [Glob of any type] vs
20
+ # @return [Hash] Partitioned results based on whether values are included within the *vs* argument.
21
+ def partition_by_values(*vs)
22
+ ### OPTIMIZE: Simple partition with the block results in nested Arrays. Investigate.
23
+ blk = lambda { |k,v| vs.include?(v) }
24
+ [select(&blk), reject(&blk)]
25
+ end
26
+
27
+ # @example
28
+ # {a: 1, b: 2, c: 3}.reject_by_keys :a, :b # returns {c: 3}
29
+ #
30
+ # @param [Glob of any type] ks
31
+ # @return [Hash] That subset of the Hash whose keys are not found within the *ks* argument.
32
+ def reject_by_keys(*ks)
33
+ reject { |k,v| ks.include?(k) }
34
+ end
35
+
36
+ # @example
37
+ # {a: 1, b: 2, c: 3}.reject_by_values 2, 3 # returns {a: 1}
38
+ #
39
+ # @param [Glob of any type] vs
40
+ # @return [Hash] That subset of the Hash whose values are not found within the *vs* argument.
41
+ def reject_by_values(*vs)
42
+ reject { |k,v| vs.include?(v) }
43
+ end
44
+
5
45
  # @example
6
46
  # {a: 1, b: 2, c: 3}.select_by_keys :a, :b # returns {a: 1, b: 2}
7
47
  #
@@ -7,6 +7,62 @@ RSpec.describe HashSelectors do
7
7
 
8
8
  ### INSTANCE METHODS
9
9
 
10
+ describe "#partition_by_keys" do
11
+ context "assuming a Hash: {a: 1, b: 2, c: {c2: [:d, :e]}}" do
12
+ let(:the_hash) { {a: 1, b: 2, c: {c2: [:d, :e]}} }
13
+ context "and given :a, :d" do
14
+ subject { the_hash.partition_by_keys :a, :d }
15
+ it { is_expected.to eq([{a: 1}, {b: 2, c: {c2: [:d, :e]}}]) }
16
+ end
17
+ context "and given :a, :b" do
18
+ subject { the_hash.partition_by_keys :a, :b }
19
+ it { is_expected.to eq([{a: 1, b: 2}, {c: {c2: [:d, :e]}}]) }
20
+ end
21
+ context "and given :b, :c" do
22
+ subject { the_hash.partition_by_keys :b, :c }
23
+ it { is_expected.to eq([{b: 2, c: {c2: [:d, :e]}}, {a: 1}]) }
24
+ end
25
+ end
26
+ end
27
+
28
+ describe "#partition_by_values" do
29
+ context "assuming a Hash: {a: 1, b: 2, c: {c2: [:d, :e]}}" do
30
+ let(:the_hash) { {a: 1, b: 2, c: {c2: [:d, :e]}} }
31
+ context "and given 1, 2" do
32
+ subject { the_hash.partition_by_values 1, 2 }
33
+ it { is_expected.to eq([{a: 1, b: 2}, {c: {c2: [:d, :e]}}]) }
34
+ end
35
+ end
36
+ end
37
+
38
+ describe "#reject_by_keys" do
39
+ context "assuming a Hash: {a: 1, b: 2, c: {c2: [:d, :e]}}" do
40
+ let(:the_hash) { {a: 1, b: 2, c: {c2: [:d, :e]}} }
41
+ context "and given :a, :d" do
42
+ subject { the_hash.reject_by_keys :a, :d }
43
+ it { is_expected.to eq({b: 2, c: {c2: [:d, :e]}}) }
44
+ end
45
+ context "and given :a, :b" do
46
+ subject { the_hash.reject_by_keys :a, :b }
47
+ it { is_expected.to eq({c: {c2: [:d, :e]}}) }
48
+ end
49
+ context "and given :b, :c" do
50
+ subject { the_hash.reject_by_keys :b, :c }
51
+ it { is_expected.to eq({a: 1}) }
52
+ end
53
+ end
54
+ end
55
+
56
+ describe "#reject_by_values" do
57
+ context "assuming a Hash: {a: 1, b: 2, c: {c2: [:d, :e]}}" do
58
+ let(:the_hash) { {a: 1, b: 2, c: {c2: [:d, :e]}} }
59
+ context "and given 1, 2" do
60
+ subject { the_hash.reject_by_values 1, 2 }
61
+ it { is_expected.to eq({c: {c2: [:d, :e]}}) }
62
+ end
63
+ end
64
+ end
65
+
10
66
  describe "#select_by_keys" do
11
67
  context "assuming a Hash: {a: 1, b: 2, c: {c2: [:d, :e]}}" do
12
68
  let(:the_hash) { {a: 1, b: 2, c: {c2: [:d, :e]}} }
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.1
4
+ version: 0.0.2
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-06-11 00:00:00.000000000 Z
11
+ date: 2014-06-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec