ratatouille 1.2.2 → 1.2.4
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +22 -0
- data/lib/ratatouille/hash.rb +27 -0
- data/lib/ratatouille/version.rb +1 -1
- data/spec/lib/ratatouille/hash_spec.rb +18 -0
- metadata +3 -3
data/README.md
CHANGED
@@ -158,6 +158,28 @@ Used to ensure that the list of keys exist in the Hash.
|
|
158
158
|
|
159
159
|
|
160
160
|
|
161
|
+
### required\_key
|
162
|
+
|
163
|
+
Used to ensure given key exists in the Hash.
|
164
|
+
|
165
|
+
* Eliminates the need to perform "given\_key" methods within a "required\_keys" block.
|
166
|
+
* Block is optional
|
167
|
+
|
168
|
+
|
169
|
+
#### Syntax
|
170
|
+
|
171
|
+
```ruby
|
172
|
+
# Validate that the keys exist and perform validation if they do
|
173
|
+
required_key(:foo) do
|
174
|
+
# Validation provided that :foo exists in Hash
|
175
|
+
end
|
176
|
+
|
177
|
+
# Validate that the keys exist
|
178
|
+
required_key(:foo)
|
179
|
+
```
|
180
|
+
|
181
|
+
|
182
|
+
|
161
183
|
### is\_empty
|
162
184
|
|
163
185
|
* Self-explanatory
|
data/lib/ratatouille/hash.rb
CHANGED
@@ -80,6 +80,33 @@ module Ratatouille
|
|
80
80
|
end#required_keys
|
81
81
|
|
82
82
|
|
83
|
+
# Perform validation on a key that must be present in the Hash to validate. Otherwise,
|
84
|
+
# an error will be added.
|
85
|
+
#
|
86
|
+
# @param [String,Symbol] req_key Required Key
|
87
|
+
# @return [void]
|
88
|
+
def required_key(req_key, &block)
|
89
|
+
if req_key.nil?
|
90
|
+
validation_error("required_key needs key argument")
|
91
|
+
return
|
92
|
+
end
|
93
|
+
|
94
|
+
unless @ratifiable_object.has_key?(req_key)
|
95
|
+
case req_key
|
96
|
+
when Symbol then validation_error("Missing key :#{req_key}")
|
97
|
+
when String then validation_error("Missing key #{req_key}")
|
98
|
+
when respond_to?(:to_s)
|
99
|
+
validation_error("Missing key #{req_key.to_s}")
|
100
|
+
end
|
101
|
+
return
|
102
|
+
end
|
103
|
+
|
104
|
+
instance_eval(&block) if block_given?
|
105
|
+
rescue Exception => e
|
106
|
+
validation_error("#{e.message}")
|
107
|
+
end
|
108
|
+
|
109
|
+
|
83
110
|
# Provide a list of keys to choose from and a choice size (default 1).
|
84
111
|
# When the Hash does not contain at least 'choice_size' keys of the key
|
85
112
|
# list provided, an error will be added.
|
data/lib/ratatouille/version.rb
CHANGED
@@ -63,6 +63,24 @@ describe "Ratatouille::HashMethods" do
|
|
63
63
|
end
|
64
64
|
end
|
65
65
|
|
66
|
+
describe "required_key" do
|
67
|
+
it "should be invalid when given a key for an empty hash" do
|
68
|
+
RatifierTest.new({}){ required_key(:foo) }.should_not be_valid
|
69
|
+
end
|
70
|
+
|
71
|
+
it "should be invalid when given a key that doesn't exist in the hash" do
|
72
|
+
RatifierTest.new({:foo => "foo"}){ required_key(:bar) }.should_not be_valid
|
73
|
+
end
|
74
|
+
|
75
|
+
it "should not progress into block if invalid" do
|
76
|
+
f = false
|
77
|
+
RatifierTest.new({}) do
|
78
|
+
required_key(:foo) { f = true }
|
79
|
+
end
|
80
|
+
f.should be_false
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
66
84
|
describe "given_key" do
|
67
85
|
it "should change the scope name to default to the key if no name passed as option" do
|
68
86
|
n = ""
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ratatouille
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 23
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 1
|
8
8
|
- 2
|
9
|
-
-
|
10
|
-
version: 1.2.
|
9
|
+
- 4
|
10
|
+
version: 1.2.4
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Ryan Johnson
|