hash_selectors 0.0.2 → 0.0.3

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: c54e88d54866fd6a2b4f084d3e07cd8bf79cad9a
4
- data.tar.gz: 64124d0bc34c1beca634a2a5791b441f64bba35a
3
+ metadata.gz: 530d9c99cacfe3b9c855797115355cad2d52a709
4
+ data.tar.gz: d04d9f82a1d9f289ee0af9aeb86cbff729289bf6
5
5
  SHA512:
6
- metadata.gz: d2ab23a176b61a530f96e8d36d62e45084504a8e14402d8c7e96a22f3cb4275823503a04c540aff93c3a0a56f27d47bf4a276305c6fcd9dc395f39a46f7ab443
7
- data.tar.gz: 8ff3671d090084ae55b0f3151632de1703a99f0fb89a4fc996504b7af1c06cd874b38110cbc7e31c241067581de750060247b21c75607a0248845a0a905ed14b
6
+ metadata.gz: a1682fc0cbb6edb9e3e454be01db2768adea2c16c8fe5db36a050caea97e437bf59ca9c6c2d7212042cc3684e77180946655ad4c5f3f6e517cc7247d528053b3
7
+ data.tar.gz: 42e0ca17a6f1dc52739db63cacde137f8ac7364ddb8915f86d566e4c49760b66113f1eec51d4293e7c4017b0cbf9a3b5f7cb6a26c1f551c243e69da8d50bb201
data/README.md CHANGED
@@ -17,3 +17,9 @@ A small set of select methods for Ruby Hashes
17
17
  {a: 1, b: 2, c: 3}.partition_by_keys :a, :b # returns [{a: 1, b: 2}, {c: 3}]
18
18
 
19
19
  {a: 1, b: 2, c: 3}.partition_by_values 1, 3 # returns [{a: 1, c: 3}, {b: 2}]
20
+
21
+ # values_for_keys
22
+ {a: 1, b: 2, c: nil}.values_for_keys :a, :c # returns [1, nil]
23
+
24
+ # unfiltered_values_for_keys
25
+ {a: 1, b: 2, c: nil}.unfiltered_values_for_keys :a, :c, :d # returns [1, nil, nil]
@@ -60,6 +60,24 @@ module HashSelectors
60
60
  select { |k,v| vs.include?(v) }
61
61
  end
62
62
 
63
+ # @example
64
+ # {a: 1, b: 2, c: 3}.values_for_keys :a, :b # returns [1, 2]
65
+ #
66
+ # @param [Glob of any type] ks
67
+ # @return [Array] The values for each respective key in the *ks* argument, non-matching keys ignored.
68
+ def values_for_keys(*ks)
69
+ ks.select { |k| key?(k) }.map { |k| self[k] }
70
+ end
71
+
72
+ # @example
73
+ # {a: 1, b: 2, c: 3}.unfiltered_values_for_keys :a, :b, :d # returns [1, 2, nil]
74
+ #
75
+ # @param [Glob of any type] ks
76
+ # @return [Array] The values for each respective key in the *ks* argument, non-matching keys returning nil.
77
+ def unfiltered_values_for_keys(*ks)
78
+ ks.map { |k| self[k] }
79
+ end
80
+
63
81
  Hash.include(self)
64
82
 
65
83
  end
@@ -91,4 +91,48 @@ RSpec.describe HashSelectors do
91
91
  end
92
92
  end
93
93
 
94
+ describe "#values_for_keys" do
95
+ context "assuming a Hash: {a: 1, b: 2, c: {c2: [:d, :e]}}" do
96
+ let(:the_hash) { {a: 1, b: 2, c: {c2: [:d, :e]}, has_nil_value: nil} }
97
+ context "and given :a, :d" do
98
+ subject { the_hash.values_for_keys :a, :d }
99
+ it { is_expected.to eq([1]) }
100
+ end
101
+ context "and given :a, :b" do
102
+ subject { the_hash.values_for_keys :a, :b }
103
+ it { is_expected.to eq([1, 2]) }
104
+ end
105
+ context "and given :b, :c" do
106
+ subject { the_hash.values_for_keys :b, :c }
107
+ it { is_expected.to eq([2, {c2: [:d, :e]}]) }
108
+ end
109
+ context "and given :a, :has_nil_value" do
110
+ subject { the_hash.values_for_keys :a, :has_nil_value }
111
+ it { is_expected.to eq([1, nil]) }
112
+ end
113
+ end
114
+ end
115
+
116
+ describe "#unfiltered_values_for_keys" do
117
+ context "assuming a Hash: {a: 1, b: 2, c: {c2: [:d, :e]}}" do
118
+ let(:the_hash) { {a: 1, b: 2, c: {c2: [:d, :e]}, has_nil_value: nil} }
119
+ context "and given :a, :d, :x" do
120
+ subject { the_hash.unfiltered_values_for_keys :a, :d, :x }
121
+ it { is_expected.to eq([1, nil, nil]) }
122
+ end
123
+ context "and given :a, :b" do
124
+ subject { the_hash.unfiltered_values_for_keys :a, :b }
125
+ it { is_expected.to eq([1, 2]) }
126
+ end
127
+ context "and given :b, :c" do
128
+ subject { the_hash.unfiltered_values_for_keys :b, :c }
129
+ it { is_expected.to eq([2, {c2: [:d, :e]}]) }
130
+ end
131
+ context "and given :a, :has_nil_value" do
132
+ subject { the_hash.unfiltered_values_for_keys :a, :has_nil_value }
133
+ it { is_expected.to eq([1, nil]) }
134
+ end
135
+ end
136
+ end
137
+
94
138
  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.2
4
+ version: 0.0.3
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-18 00:00:00.000000000 Z
11
+ date: 2014-08-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec