hash_control 0.1.2 → 0.1.4
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/.travis.yml +6 -1
- data/README.md +9 -1
- data/hash_control.gemspec +1 -1
- data/lib/hash_control/model.rb +3 -3
- data/lib/hash_control/version.rb +1 -1
- data/spec/lib/hash_control/model_spec.rb +42 -5
- data/spec/lib/hash_control/validator_spec.rb +14 -14
- metadata +4 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 91db8077b43e392011ad2d957a0b1994fc504170
|
4
|
+
data.tar.gz: a05d93cb0a18727dd4ab9645dfad99d2aeb100bf
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ef8617b5b1ddd826b5560cbf7f579234dbd712097986c9cd0ad58ed845a17d6cbf63ee38abcfcc85d5eda3064ef54795d6255af8ccfe4480534aa3901b09c718
|
7
|
+
data.tar.gz: 9fe6d8c6302fbe60a1e8d2a500d479ff2a71579ae5406eaa665d159727d583465daf412f3cc94ec39d0e57ba38b5a3726352b997e88b327a1a8db3a7b96e9f81
|
data/.travis.yml
CHANGED
data/README.md
CHANGED
@@ -1,4 +1,5 @@
|
|
1
|
-
|
1
|
+
HashControl
|
2
|
+
===========
|
2
3
|
|
3
4
|
[](https://travis-ci.org/szhu/hashcontrol)
|
4
5
|
[](https://codeclimate.com/github/szhu/hashcontrol)
|
@@ -89,3 +90,10 @@ end
|
|
89
90
|
CustomValidator.new(get_request).validate_get_request
|
90
91
|
CustomValidator.new(post_request).validate_post_request
|
91
92
|
```
|
93
|
+
|
94
|
+
## Credit
|
95
|
+
|
96
|
+
**HashControl** was originally designed and written by [@szhu] at [@IFTTT].
|
97
|
+
|
98
|
+
[@szhu]: https://github.com/szhu
|
99
|
+
[@IFTTT]: https://github.com/IFTTT
|
data/hash_control.gemspec
CHANGED
data/lib/hash_control/model.rb
CHANGED
@@ -17,7 +17,7 @@ module HashControl
|
|
17
17
|
end
|
18
18
|
|
19
19
|
def slice(*keys)
|
20
|
-
|
20
|
+
symbolized_hash.select { |key, _| keys.include? key.to_sym }
|
21
21
|
end
|
22
22
|
|
23
23
|
def [](name)
|
@@ -58,7 +58,7 @@ module HashControl
|
|
58
58
|
|
59
59
|
def key_accessor(name)
|
60
60
|
name = name.to_sym
|
61
|
-
return if self.
|
61
|
+
return if self.method_defined?(name)
|
62
62
|
class_eval do
|
63
63
|
define_method(name) { @hash[name] }
|
64
64
|
end
|
@@ -78,7 +78,7 @@ module HashControl
|
|
78
78
|
module ClassMethods
|
79
79
|
def key_accessor(name)
|
80
80
|
name = name.to_sym
|
81
|
-
return if self.
|
81
|
+
return if self.method_defined?(name)
|
82
82
|
class_eval do
|
83
83
|
define_method(name) { @hash[name] }
|
84
84
|
define_method("#{name}=") { |x| @hash[name] = x }
|
data/lib/hash_control/version.rb
CHANGED
@@ -13,6 +13,26 @@ describe HashControl::Model do
|
|
13
13
|
require_key :id
|
14
14
|
permit_all_keys
|
15
15
|
end
|
16
|
+
|
17
|
+
# This class is used to test for the bug from 0.1.2. #require_key and
|
18
|
+
# #permit_key are supposed to make a reader method iff the key does not
|
19
|
+
# conflict with an instance method, not a class method.
|
20
|
+
#
|
21
|
+
# Below, :name and :superclass are class-only methods (reader method
|
22
|
+
# should be made), while :symbolized_hash and :slice are instance-only
|
23
|
+
# methods (reader method should not be made).
|
24
|
+
class MethodConflictTest
|
25
|
+
include ::HashControl::Model
|
26
|
+
require_key :name, :symbolized_hash
|
27
|
+
permit_key :superclass, :slice
|
28
|
+
end
|
29
|
+
|
30
|
+
@method_conflict_test = MethodConflictTest.new(
|
31
|
+
name: 'value of :name',
|
32
|
+
symbolized_hash: 'value of :symbolized_hash',
|
33
|
+
superclass: 'value of :superclass',
|
34
|
+
slice: 'value of :slice'
|
35
|
+
)
|
16
36
|
end
|
17
37
|
|
18
38
|
describe "making an instance should error if it" do
|
@@ -47,7 +67,17 @@ describe HashControl::Model do
|
|
47
67
|
expect(@comment.image).to eq(nil)
|
48
68
|
end
|
49
69
|
it "not other keys" do
|
50
|
-
expect{@comment.nonexistent}.to raise_error(NoMethodError)
|
70
|
+
expect { @comment.nonexistent }.to raise_error(NoMethodError)
|
71
|
+
end
|
72
|
+
it "even keys that conflict with class methods" do
|
73
|
+
expect(@method_conflict_test.name).to eq('value of :name')
|
74
|
+
expect(@method_conflict_test.superclass).to eq('value of :superclass')
|
75
|
+
end
|
76
|
+
it "not keys that conflict with instance methods" do
|
77
|
+
# If these methods were unintentionally overwritten (as they were in
|
78
|
+
# 0.1.2), then they would return Strings.
|
79
|
+
expect(@method_conflict_test.symbolized_hash).to be_a Hash
|
80
|
+
expect(@method_conflict_test.slice).to be_a Hash
|
51
81
|
end
|
52
82
|
end
|
53
83
|
|
@@ -62,6 +92,9 @@ describe HashControl::Model do
|
|
62
92
|
end
|
63
93
|
end
|
64
94
|
|
95
|
+
it "#slice should work" do
|
96
|
+
expect(@comment.slice(:author, :body).keys.to_set).to eq([:author, :body].to_set)
|
97
|
+
end
|
65
98
|
end
|
66
99
|
|
67
100
|
describe "that permits all keys" do
|
@@ -74,12 +107,12 @@ describe HashControl::Model do
|
|
74
107
|
expect(@something.id).to eq(1)
|
75
108
|
end
|
76
109
|
it "not implicitly-permitted keys" do
|
77
|
-
expect{@something.body}.to raise_error(NoMethodError)
|
78
|
-
expect{@something.nonexistent}.to raise_error(NoMethodError)
|
110
|
+
expect { @something.body }.to raise_error(NoMethodError)
|
111
|
+
expect { @something.nonexistent }.to raise_error(NoMethodError)
|
79
112
|
end
|
80
113
|
it "not other keys" do
|
81
|
-
expect{@something.body}.to raise_error(NoMethodError)
|
82
|
-
expect{@something.nonexistent}.to raise_error(NoMethodError)
|
114
|
+
expect { @something.body }.to raise_error(NoMethodError)
|
115
|
+
expect { @something.nonexistent }.to raise_error(NoMethodError)
|
83
116
|
end
|
84
117
|
end
|
85
118
|
|
@@ -91,6 +124,10 @@ describe HashControl::Model do
|
|
91
124
|
expect(@something[:nonexistent]).to eq(nil)
|
92
125
|
end
|
93
126
|
end
|
127
|
+
|
128
|
+
it "#slice should work" do
|
129
|
+
expect(@something.slice(:body).keys.to_set).to eq([:body].to_set)
|
130
|
+
end
|
94
131
|
end
|
95
132
|
end
|
96
133
|
end
|
@@ -28,28 +28,28 @@ describe HashControl::Validator do
|
|
28
28
|
describe "when used as its own class," do
|
29
29
|
# `require` ensures certain keys are present
|
30
30
|
it "require should work properly" do
|
31
|
-
expect{ validate(@post_request).require(:post) }.not_to raise_error
|
32
|
-
expect{ validate(@post_request).require(:body) }.not_to raise_error
|
33
|
-
expect{ validate(@post_request).require(:post, :body) }.not_to raise_error
|
34
|
-
expect{ validate(@post_request).require(:nonexistent) }.to raise_error(ArgumentError)
|
31
|
+
expect { validate(@post_request).require(:post) }.not_to raise_error
|
32
|
+
expect { validate(@post_request).require(:body) }.not_to raise_error
|
33
|
+
expect { validate(@post_request).require(:post, :body) }.not_to raise_error
|
34
|
+
expect { validate(@post_request).require(:nonexistent) }.to raise_error(ArgumentError)
|
35
35
|
end
|
36
36
|
|
37
37
|
# `require_n_of` ensures at least n of certain keys are present
|
38
38
|
it "require_n_of should work properly" do
|
39
|
-
expect{ validate(@post_request).require_n_of(2, :post, :body) }.not_to raise_error
|
39
|
+
expect { validate(@post_request).require_n_of(2, :post, :body) }.not_to raise_error
|
40
40
|
end
|
41
41
|
|
42
42
|
# `permit` marks keys as allowed but doesn't do any verification
|
43
43
|
it "permit should work properly" do
|
44
|
-
expect{ validate(@post_request).permit(:body) }.not_to raise_error
|
45
|
-
expect{ validate(@post_request).permit(:nonexistent) }.not_to raise_error
|
44
|
+
expect { validate(@post_request).permit(:body) }.not_to raise_error
|
45
|
+
expect { validate(@post_request).permit(:nonexistent) }.not_to raise_error
|
46
46
|
end
|
47
47
|
|
48
48
|
# `only` ensures no other keys are present
|
49
49
|
it "only should work properly" do
|
50
|
-
expect{ validate(@post_request).only }.to raise_error(ArgumentError)
|
51
|
-
expect{ validate(@post_request).require(:post).only }.to raise_error(ArgumentError)
|
52
|
-
expect{ validate(@post_request).require(:post).permit(:body).only }.not_to raise_error
|
50
|
+
expect { validate(@post_request).only }.to raise_error(ArgumentError)
|
51
|
+
expect { validate(@post_request).require(:post).only }.to raise_error(ArgumentError)
|
52
|
+
expect { validate(@post_request).require(:post).permit(:body).only }.not_to raise_error
|
53
53
|
end
|
54
54
|
end
|
55
55
|
|
@@ -71,13 +71,13 @@ describe HashControl::Validator do
|
|
71
71
|
end
|
72
72
|
|
73
73
|
it "should work properly for allowed hashes" do
|
74
|
-
expect{ CustomValidator.new(@get_request).validate_get_request }.not_to raise_error
|
75
|
-
expect{ CustomValidator.new(@post_request).validate_post_request }.not_to raise_error
|
76
|
-
expect{ CustomValidator.new(@not_allowed_but_currently_ok).validate_post_request }.not_to raise_error
|
74
|
+
expect { CustomValidator.new(@get_request).validate_get_request }.not_to raise_error
|
75
|
+
expect { CustomValidator.new(@post_request).validate_post_request }.not_to raise_error
|
76
|
+
expect { CustomValidator.new(@not_allowed_but_currently_ok).validate_post_request }.not_to raise_error
|
77
77
|
end
|
78
78
|
|
79
79
|
it "should work properly for invalid hashes" do
|
80
|
-
expect{ CustomValidator.new(@empty).validate_get_request }.to raise_error(ArgumentError)
|
80
|
+
expect { CustomValidator.new(@empty).validate_get_request }.to raise_error(ArgumentError)
|
81
81
|
end
|
82
82
|
end
|
83
83
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: hash_control
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Sean Zhu
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2015-06-26 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -24,8 +24,8 @@ dependencies:
|
|
24
24
|
- - "~>"
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: '4.0'
|
27
|
-
description:
|
28
|
-
|
27
|
+
description: " Provides some conveniences for validating and manipulating hash-like
|
28
|
+
data.\n"
|
29
29
|
email: interestinglythere@gmail.com
|
30
30
|
executables: []
|
31
31
|
extensions: []
|