hash_plus 1.1 → 1.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 +4 -4
- data/hash_plus.gemspec +1 -1
- data/lib/hash_plus.rb +21 -13
- data/spec/hash_plus_spec.rb +47 -11
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4a9ee112603cf5e53624d742b8aa0766f8cffe66
|
4
|
+
data.tar.gz: 62ec2046c0189ed12b5de9ec1c62d7e260d2e355
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f3760630a4355e71a459f38682e5c36726da97b37bfb429248890f25eacde4b42400e1b094aeec02c4557ce6660ab9abbca223bc24aef781076a503a7ff5f606
|
7
|
+
data.tar.gz: deba6420e34df63658d30176ff9eaab86f0239397e863bafeee7bdb7350d8467b0594b45322b6076ead04375f773b83681c901f4fe04db62b91aca98ebbbfda4
|
data/hash_plus.gemspec
CHANGED
data/lib/hash_plus.rb
CHANGED
@@ -1,18 +1,26 @@
|
|
1
1
|
class Hash
|
2
|
-
#
|
3
|
-
|
4
|
-
def
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
missing = missing_fields(*cols)
|
9
|
-
unless missing.empty?
|
10
|
-
raise ArgumentError, "Missing value for keys=#{missing.join(',')} in opts=#{self.inspect}"
|
2
|
+
# Requires that the key simply be present
|
3
|
+
# The hash can contain any value for these keys
|
4
|
+
def requires_keys_are_present(*_keys)
|
5
|
+
invalid_keys = _keys - self.keys
|
6
|
+
unless invalid_keys.empty?
|
7
|
+
raise ArgumentError, "Missing keys=#{invalid_keys.join(',')} in opts=#{self.inspect}"
|
11
8
|
end
|
12
9
|
end
|
13
|
-
|
14
|
-
#
|
15
|
-
def
|
16
|
-
|
10
|
+
|
11
|
+
# Requires that the values for these keys be non-nil
|
12
|
+
def requires_keys_are_not_nil(*_keys)
|
13
|
+
invalid_keys = _keys.select { |k| self[k].nil? }
|
14
|
+
unless invalid_keys.empty?
|
15
|
+
raise ArgumentError, "Nil values for keys=#{invalid_keys.join(',')} in opts=#{self.inspect}"
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
# Requires that the values for these keys be nil
|
20
|
+
def requires_keys_are_nil(*_keys)
|
21
|
+
invalid_keys = _keys.reject { |k| self[k].nil? }
|
22
|
+
unless invalid_keys.empty?
|
23
|
+
raise ArgumentError, "Non-Nil values for keys=#{invalid_keys.join(',')} in opts=#{self.inspect}"
|
24
|
+
end
|
17
25
|
end
|
18
26
|
end
|
data/spec/hash_plus_spec.rb
CHANGED
@@ -7,21 +7,57 @@ $:.push File.expand_path("../lib", File.dirname(__FILE__))
|
|
7
7
|
require 'hash_plus'
|
8
8
|
|
9
9
|
describe Hash do
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
it
|
10
|
+
let(:sample_columns) { [:a, :b, :c] }
|
11
|
+
|
12
|
+
describe "requires_keys_are_nil" do
|
13
|
+
it "shouldn't raise error" do
|
14
|
+
hash = {}
|
15
|
+
hash.requires_keys_are_nil(*sample_columns)
|
16
|
+
end
|
17
|
+
|
18
|
+
it "should raise error for 1 column" do
|
19
|
+
hash = { :a => 123, :b => nil }
|
20
|
+
expect{ hash.requires_keys_are_nil(*sample_columns) }.to raise_error ArgumentError, "Non-Nil values for keys=a in opts=#{hash.inspect}"
|
21
|
+
end
|
14
22
|
|
15
|
-
it
|
23
|
+
it "should raise error for 2 columns" do
|
24
|
+
hash = { :a => 123, :b => 456 }
|
25
|
+
expect{ hash.requires_keys_are_nil(*sample_columns) }.to raise_error ArgumentError, "Non-Nil values for keys=a,b in opts=#{hash.inspect}"
|
26
|
+
end
|
16
27
|
end
|
17
28
|
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
29
|
+
describe "requires_keys_are_not_nil" do
|
30
|
+
it "shouldn't raise error" do
|
31
|
+
hash = { :a => 123, :b => 456, :c => 'hello world' }
|
32
|
+
hash.requires_keys_are_not_nil(*sample_columns)
|
33
|
+
end
|
34
|
+
|
35
|
+
it "should raise error for 1 column" do
|
36
|
+
hash = { :a => 123, :b => 456, :c => nil }
|
37
|
+
expect{ hash.requires_keys_are_not_nil(*sample_columns) }.to raise_error ArgumentError, "Nil values for keys=c in opts=#{hash.inspect}"
|
38
|
+
end
|
39
|
+
|
40
|
+
it "should raise error for 2 columns" do
|
41
|
+
hash = { :a => 123, :b => nil, :c => nil }
|
42
|
+
expect{ hash.requires_keys_are_not_nil(*sample_columns) }.to raise_error ArgumentError, "Nil values for keys=b,c in opts=#{hash.inspect}"
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
describe "requires_keys_are_present" do
|
47
|
+
it "shouldn't raise error" do
|
48
|
+
hash = { :a => 123, :b => 456, :c => nil }
|
49
|
+
hash.requires_keys_are_present(*sample_columns)
|
50
|
+
end
|
51
|
+
|
52
|
+
it "should raise error for 1 column" do
|
53
|
+
hash = { :a => 123, :b => nil }
|
54
|
+
expect{ hash.requires_keys_are_present(*sample_columns) }.to raise_error ArgumentError, "Missing keys=c in opts=#{hash.inspect}"
|
55
|
+
end
|
23
56
|
|
24
|
-
it
|
57
|
+
it "should raise error for 2 columns" do
|
58
|
+
hash = { :a => 123 }
|
59
|
+
expect{ hash.requires_keys_are_present(*sample_columns) }.to raise_error ArgumentError, "Missing keys=b,c in opts=#{hash.inspect}"
|
60
|
+
end
|
25
61
|
end
|
26
62
|
|
27
63
|
end
|