hash_plus 1.1 → 1.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 4b8089cb1816245c13ded8dcdac34a849fc238c2
4
- data.tar.gz: 5fd853a1998fae2acbe01159b984537d2ba97349
3
+ metadata.gz: 4a9ee112603cf5e53624d742b8aa0766f8cffe66
4
+ data.tar.gz: 62ec2046c0189ed12b5de9ec1c62d7e260d2e355
5
5
  SHA512:
6
- metadata.gz: 2503486bfd1dab699afb6cbf80b5e21c7d127a3150a737d19579b178b2e67ab41393d5167fe0f50575623656e0b103a49ba2d4cadf8a2d4859fb60c7a4bce83e
7
- data.tar.gz: 6d81568afeafd111cd10725defb07084c2cc31c973da1c8e72070f28dae2da05d1e84a4f5189d871f1264a0e0646685d76ba95a2010d59a4552a5034bf68f37e
6
+ metadata.gz: f3760630a4355e71a459f38682e5c36726da97b37bfb429248890f25eacde4b42400e1b094aeec02c4557ce6660ab9abbca223bc24aef781076a503a7ff5f606
7
+ data.tar.gz: deba6420e34df63658d30176ff9eaab86f0239397e863bafeee7bdb7350d8467b0594b45322b6076ead04375f773b83681c901f4fe04db62b91aca98ebbbfda4
data/hash_plus.gemspec CHANGED
@@ -4,7 +4,7 @@ require "hash_plus"
4
4
 
5
5
  Gem::Specification.new do |gem|
6
6
  gem.name = "hash_plus"
7
- gem.version = '1.1'
7
+ gem.version = '1.2'
8
8
  gem.authors = ["Glenn Nagel"]
9
9
  gem.email = ["glenn@mercury-wireless.com"]
10
10
  gem.homepage = "https://github.com/gnagel/hash_plus"
data/lib/hash_plus.rb CHANGED
@@ -1,18 +1,26 @@
1
1
  class Hash
2
- # Short hand methods
3
- def requires(*cols); requires_fields(*cols); end
4
- def missing(*cols); missing_fields(*cols); end
5
-
6
- # Raise an ArgumentError if there are missing columns
7
- def requires_fields(*cols)
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
- # Return the missing columns
15
- def missing_fields(*cols)
16
- cols.select { |required_key| self[required_key].nil? }
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
@@ -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
- context "empty hash" do
11
- subject(:hash) { Hash.new }
12
- it { hash.missing_fields(:a).first.should === :a }
13
- it { hash.missing_fields(:a, :b, :c).should =~ [:a, :b, :c] }
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 { expect { hash.requires_fields(:a) }.to raise_error ArgumentError, "Missing value for keys=a in opts={}" }
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
- context "hash containing values" do
19
- subject(:hash) { {:a => 'b', :c => 'd'} }
20
- it { hash.missing_fields(:a).should be_empty }
21
- it { hash.missing_fields(:a, :c).should be_empty }
22
- it { hash.missing_fields(:a, :b, :c).should =~ [:b] }
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 { expect { hash.requires_fields(:b) }.to raise_error ArgumentError, "Missing value for keys=b in opts=#{hash.inspect}" }
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
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hash_plus
3
3
  version: !ruby/object:Gem::Version
4
- version: '1.1'
4
+ version: '1.2'
5
5
  platform: ruby
6
6
  authors:
7
7
  - Glenn Nagel