ratatouille 0.9.2 → 1.0.0
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.
- data/README.md +223 -3
- data/lib/ratatouille.rb +2 -0
- data/lib/ratatouille/array.rb +53 -35
- data/lib/ratatouille/hash.rb +27 -5
- data/lib/ratatouille/nilclass.rb +4 -5
- data/lib/ratatouille/ratifier.rb +9 -9
- data/lib/ratatouille/string.rb +6 -0
- data/lib/ratatouille/version.rb +2 -1
- data/spec/lib/ratatouille/array_spec.rb +111 -0
- data/spec/lib/ratatouille/hash_spec.rb +51 -0
- data/spec/lib/ratatouille/nilclass_spec.rb +9 -0
- data/spec/lib/{ratatouille_ratifier_spec.rb → ratatouille/ratifier_spec.rb} +6 -2
- data/spec/lib/ratatouille_spec.rb +8 -55
- data/spec/spec_helper.rb +3 -0
- metadata +14 -9
- data/spec/ratifier_spec.rb +0 -10
data/README.md
CHANGED
@@ -1,6 +1,6 @@
|
|
1
|
-
# Ratatouille
|
1
|
+
# Ratatouille [](http://travis-ci.org/CITguy/ratatouille)
|
2
2
|
|
3
|
-
|
3
|
+
DSL for validation of complex Hashes
|
4
4
|
|
5
5
|
## Installation
|
6
6
|
|
@@ -16,9 +16,229 @@ Or install it yourself as:
|
|
16
16
|
|
17
17
|
$ gem install ratatouille
|
18
18
|
|
19
|
+
|
20
|
+
|
21
|
+
## Blocks
|
22
|
+
|
23
|
+
All of the given methods accept a block for validation and will not progress into the block if the core method logic does not validate.
|
24
|
+
However, some methods may be used without a block and validation will progress whether or not the method logic validates.
|
25
|
+
|
26
|
+
|
27
|
+
### ratifiable\_object
|
28
|
+
|
29
|
+
Within a block, the ratifiable\_object method provides the object that is to be validated against.
|
30
|
+
This will change when using *given\_key*.
|
31
|
+
|
32
|
+
|
33
|
+
|
19
34
|
## Usage
|
20
35
|
|
21
|
-
|
36
|
+
All of the following methods perform validation on the *ratifiable\_object* defined in scope of the block.
|
37
|
+
|
38
|
+
|
39
|
+
### given\_key
|
40
|
+
|
41
|
+
This method is used to scope its given block to the key value. Useful to reduce the need to explicitly namespace nested keys in *ratifiable\_object*.
|
42
|
+
|
43
|
+
* **This method doesn't perform any validation and must be used with a block to get any use out of it.**
|
44
|
+
* Changes *ratifiable\_object* to *key value* (original scope will return on block exit)
|
45
|
+
|
46
|
+
|
47
|
+
#### Syntax
|
48
|
+
|
49
|
+
```ruby
|
50
|
+
given_key(:foo) do
|
51
|
+
# validation for ratifiable_object[:foo]
|
52
|
+
end
|
53
|
+
```
|
54
|
+
|
55
|
+
#### Example
|
56
|
+
|
57
|
+
See **choice\_of** for an example.
|
58
|
+
|
59
|
+
|
60
|
+
### choice\_of
|
61
|
+
|
62
|
+
Meant to be used to validate that X number of given key choices exist in the Hash.
|
63
|
+
|
64
|
+
* Number of Choices must be less than size of key choice array
|
65
|
+
* Can be used without a block
|
66
|
+
* Works well with **given\_key()**.
|
67
|
+
|
68
|
+
|
69
|
+
#### Syntax
|
70
|
+
|
71
|
+
```ruby
|
72
|
+
choice_of(number_of_choices, key_choice_array) do
|
73
|
+
# Validation provided number of choices is satisfied
|
74
|
+
end
|
75
|
+
```
|
76
|
+
|
77
|
+
|
78
|
+
#### Example
|
79
|
+
|
80
|
+
```ruby
|
81
|
+
r = ratify({:foo => "bar", :bar => "biz"}) do
|
82
|
+
choice_of(1, :foo, :bar) do
|
83
|
+
# Validation, provided :foo OR :bar exists
|
84
|
+
end
|
85
|
+
end
|
86
|
+
r.valid? #=> false (:foo OR :bar must be defined, NOT BOTH)
|
87
|
+
|
88
|
+
r = ratify({:foo => "bar", :bar => "biz"}) do
|
89
|
+
choice_of(2, :foo, :bar, :biz) do
|
90
|
+
# Validation, provided 2 of the following exist: :foo, :bar, :biz
|
91
|
+
|
92
|
+
given_key(:foo) do
|
93
|
+
# in context of ratifiable_object[:foo]
|
94
|
+
end
|
95
|
+
|
96
|
+
given_key(:bar) do
|
97
|
+
# in context of ratifiable_object[:bar]
|
98
|
+
end
|
99
|
+
end
|
100
|
+
end
|
101
|
+
r.valid? #=> true (a choice of 2 items from [:foo, :bar, :biz] is satisfied)
|
102
|
+
|
103
|
+
r = ratify({:foo => "bar", :bar => "biz"}) do
|
104
|
+
choice_of(2, :foo, :bar) do
|
105
|
+
# Validation ...
|
106
|
+
end
|
107
|
+
end
|
108
|
+
r.valid? #=> false (you might as well use required_keys)
|
109
|
+
```
|
110
|
+
|
111
|
+
|
112
|
+
### required\_keys
|
113
|
+
|
114
|
+
Used to ensure that the list of keys exist in the Hash.
|
115
|
+
|
116
|
+
* Block is optional
|
117
|
+
|
118
|
+
|
119
|
+
#### Syntax
|
120
|
+
|
121
|
+
```ruby
|
122
|
+
# Validate that the keys exist and perform validation if they do
|
123
|
+
required_keys(:foo, :bar) do
|
124
|
+
# Validation provided that :foo and :bar exist
|
125
|
+
end
|
126
|
+
|
127
|
+
# Validate that the keys exist
|
128
|
+
required_keys(:foo, :bar)
|
129
|
+
```
|
130
|
+
|
131
|
+
|
132
|
+
#### Example
|
133
|
+
|
134
|
+
```ruby
|
135
|
+
r = ratify({:foo => "bar", :bar => "biz"}) do
|
136
|
+
required_keys(:foo, :bar)
|
137
|
+
end
|
138
|
+
r.valid? #=> true
|
139
|
+
|
140
|
+
r = ratify({:foo => "bar"}) do
|
141
|
+
required_keys(:foo, :bar)
|
142
|
+
end
|
143
|
+
r.valid? #=> false
|
144
|
+
```
|
145
|
+
|
146
|
+
|
147
|
+
|
148
|
+
### is\_empty
|
149
|
+
|
150
|
+
* Self-explanatory
|
151
|
+
* Block is optional
|
152
|
+
|
153
|
+
```ruby
|
154
|
+
r = ratify({:foo => "bar"}) do
|
155
|
+
is_empty # validation continues
|
156
|
+
is_empty do
|
157
|
+
# validation in block is never performed
|
158
|
+
end
|
159
|
+
end
|
160
|
+
r.valid? #=> false
|
161
|
+
|
162
|
+
r = ratify({}) do
|
163
|
+
is_empty # validation continues even if not empty
|
164
|
+
is_empty do
|
165
|
+
# validation continues only if ratifiable_object is empty
|
166
|
+
end
|
167
|
+
end
|
168
|
+
r.valid? #=> true
|
169
|
+
```
|
170
|
+
|
171
|
+
|
172
|
+
### is\_not\_empty
|
173
|
+
|
174
|
+
* Self-explanatory
|
175
|
+
* Block is optional
|
176
|
+
|
177
|
+
```ruby
|
178
|
+
r = ratify({:foo => "bar"}) do
|
179
|
+
is_not_empty # validation continues even if empty
|
180
|
+
is_not_empty do
|
181
|
+
# validation continues unless ratifiable_object is empty
|
182
|
+
end
|
183
|
+
end
|
184
|
+
r.valid? #=> true
|
185
|
+
|
186
|
+
r = ratify({}) do
|
187
|
+
is_not_empty # validation continues
|
188
|
+
is_not_empty do
|
189
|
+
# validation in block is never performed
|
190
|
+
end
|
191
|
+
end
|
192
|
+
r.valid? #=> false
|
193
|
+
```
|
194
|
+
|
195
|
+
|
196
|
+
|
197
|
+
## Custom Validation
|
198
|
+
|
199
|
+
**Return to this section after reading the remaining sections.**
|
200
|
+
|
201
|
+
Custom validation can take place using the following methods to generate custom validation logic that cannot be satisfied with the existing methods.
|
202
|
+
|
203
|
+
You should use the **validation\_error** method to add your own errors to the Ratifier object.
|
204
|
+
|
205
|
+
|
206
|
+
### validation\_error
|
207
|
+
|
208
|
+
Used to insert validation error message into the Ratifier object.
|
209
|
+
|
210
|
+
|
211
|
+
#### Syntax
|
212
|
+
|
213
|
+
It is also possible to set the context of an error by passing in a second argument. However, it defaults to the root of the current ratifiable\_object ('/').
|
214
|
+
|
215
|
+
```ruby
|
216
|
+
validation_error("This is an error")
|
217
|
+
validation_error("This is an error", "current_context")
|
218
|
+
```
|
219
|
+
|
220
|
+
|
221
|
+
|
222
|
+
## Advanced Example
|
223
|
+
|
224
|
+
```ruby
|
225
|
+
include Ratatouille
|
226
|
+
r = ratify({:foo => {:bar => {:biz => "bang"}}}) do
|
227
|
+
is_not_empty
|
228
|
+
given_key(:foo) do
|
229
|
+
validation_error(":foo error")
|
230
|
+
given_key(:bar) do
|
231
|
+
validation_error(":bar error")
|
232
|
+
given_key(:biz) do
|
233
|
+
if ratifiable_object == "bang"
|
234
|
+
validation_error("should be 'shoot'")
|
235
|
+
end
|
236
|
+
end
|
237
|
+
end
|
238
|
+
end
|
239
|
+
end
|
240
|
+
r.valid? # => false
|
241
|
+
```
|
22
242
|
|
23
243
|
## Contributing
|
24
244
|
|
data/lib/ratatouille.rb
CHANGED
@@ -4,7 +4,9 @@ require "ratatouille/ratifier"
|
|
4
4
|
require "ratatouille/nilclass"
|
5
5
|
require "ratatouille/hash"
|
6
6
|
require "ratatouille/array"
|
7
|
+
require "ratatouille/string"
|
7
8
|
|
9
|
+
# Module to provide DSL for validation of complex Hashes
|
8
10
|
module Ratatouille
|
9
11
|
|
10
12
|
# @param [Hash, Array] obj Object to validate
|
data/lib/ratatouille/array.rb
CHANGED
@@ -1,5 +1,6 @@
|
|
1
1
|
module Ratatouille
|
2
2
|
|
3
|
+
# Module used to provide Array-specific validation methods
|
3
4
|
module ArrayMethods
|
4
5
|
# @return [void]
|
5
6
|
def is_empty(&block)
|
@@ -28,17 +29,11 @@ module Ratatouille
|
|
28
29
|
# @param [Integer] min_size
|
29
30
|
# @return [void]
|
30
31
|
def min_length(min_size=0, &block)
|
31
|
-
unless min_size
|
32
|
-
validation_error("min_length argument must be an Integer")
|
33
|
-
return
|
34
|
-
end
|
35
|
-
|
36
|
-
unless @ratifiable_object.size >= min_size
|
37
|
-
validation_error("Array length must be #{size} or more")
|
38
|
-
return
|
39
|
-
end
|
32
|
+
return unless valid_min_length?(min_size)
|
40
33
|
|
41
34
|
instance_eval(&block) if block_given?
|
35
|
+
rescue Exception => e
|
36
|
+
validation_error("#{e.message}")
|
42
37
|
end#min_length
|
43
38
|
|
44
39
|
|
@@ -47,17 +42,11 @@ module Ratatouille
|
|
47
42
|
# @param [Integer] min_size
|
48
43
|
# @return [void]
|
49
44
|
def max_length(max_size=0, &block)
|
50
|
-
unless max_size
|
51
|
-
validation_error("max_length argument must be an Integer")
|
52
|
-
return
|
53
|
-
end
|
54
|
-
|
55
|
-
if @ratifiable_object.size > max_size
|
56
|
-
validation_error("Array length must be less than #{size}")
|
57
|
-
return
|
58
|
-
end
|
45
|
+
return unless valid_max_length?(max_size)
|
59
46
|
|
60
47
|
instance_eval(&block) if block_given?
|
48
|
+
rescue Exception => e
|
49
|
+
validation_error("#{e.message}")
|
61
50
|
end#max_length
|
62
51
|
|
63
52
|
|
@@ -67,36 +56,65 @@ module Ratatouille
|
|
67
56
|
# @param [Integer] max_size
|
68
57
|
# @return [void]
|
69
58
|
def length_between(min_size=0, max_size=nil, &block)
|
70
|
-
unless min_size
|
71
|
-
validation_error("min_size must be an integer")
|
72
|
-
return
|
73
|
-
end
|
59
|
+
return unless valid_min_length?(min_size)
|
74
60
|
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
return
|
79
|
-
end
|
80
|
-
|
81
|
-
unless max_size.nil?
|
82
|
-
unless max_size =~ /\d+/
|
83
|
-
validation_error("max_size must be an integer")
|
61
|
+
if max_size.nil?
|
62
|
+
if min_size == 1
|
63
|
+
validation_error("Consider using is_not_empty")
|
84
64
|
return
|
85
65
|
end
|
66
|
+
else
|
67
|
+
return unless valid_max_length?(max_size)
|
86
68
|
|
87
|
-
|
88
|
-
validation_error("
|
69
|
+
if max_size == 0 && min_size == 0
|
70
|
+
validation_error("Consider using is_empty")
|
89
71
|
return
|
90
72
|
end
|
91
73
|
|
92
|
-
unless
|
93
|
-
validation_error("
|
74
|
+
unless max_size > min_size
|
75
|
+
validation_error("max_size must be greater than min_size")
|
94
76
|
return
|
95
77
|
end
|
96
78
|
end
|
97
79
|
|
98
80
|
instance_eval(&block) if block_given?
|
99
81
|
end#length_between
|
82
|
+
private
|
83
|
+
|
84
|
+
# @return [Boolean]
|
85
|
+
def valid_min_length?(min_size)
|
86
|
+
unless min_size.to_i >= 0
|
87
|
+
validation_error("min_length must be a number greater than or equal to 0")
|
88
|
+
return false
|
89
|
+
end
|
90
|
+
|
91
|
+
unless @ratifiable_object.size >= min_size.to_i
|
92
|
+
validation_error("Array length must be #{min_size} or more")
|
93
|
+
return false
|
94
|
+
end
|
95
|
+
return true
|
96
|
+
rescue Exception => e
|
97
|
+
validation_error("#{e.message}")
|
98
|
+
return false
|
99
|
+
end
|
100
|
+
|
101
|
+
# @return [Boolean]
|
102
|
+
def valid_max_length?(max_size)
|
103
|
+
unless max_size.to_i >= 0
|
104
|
+
validation_error("max_size must be a number greater than or equal to 0")
|
105
|
+
return false
|
106
|
+
end
|
107
|
+
|
108
|
+
if @ratifiable_object.size > max_size.to_i
|
109
|
+
validation_error("Array length must be less than #{max_size.to_i}")
|
110
|
+
return false
|
111
|
+
end
|
112
|
+
|
113
|
+
return true
|
114
|
+
rescue Exception => e
|
115
|
+
validation_error("#{e.message}")
|
116
|
+
return false
|
117
|
+
end
|
100
118
|
end#ArrayMethods
|
101
119
|
|
102
120
|
end
|
data/lib/ratatouille/hash.rb
CHANGED
@@ -1,11 +1,12 @@
|
|
1
1
|
module Ratatouille
|
2
2
|
|
3
|
+
# Module used to provide Hash-specific validation methods
|
3
4
|
module HashMethods
|
4
5
|
# Runs validation in block against object for the given key.
|
5
6
|
#
|
6
7
|
# @param [String, Symbol] key
|
7
8
|
def given_key(key, &block)
|
8
|
-
if @ratifiable_object.has_key?(key)
|
9
|
+
if @ratifiable_object.has_key?(key) && block_given?
|
9
10
|
child_object = Ratatouille::Ratifier.new(@ratifiable_object[key], &block)
|
10
11
|
@errors[key] = child_object.errors unless child_object.valid?
|
11
12
|
end
|
@@ -37,17 +38,28 @@ module Ratatouille
|
|
37
38
|
# Provide a list of keys that must be present in the Hash to validate. Otherwise,
|
38
39
|
# an error will be added.
|
39
40
|
#
|
40
|
-
# @param [Array] args Array
|
41
|
+
# @param [Array] args Array required keys
|
41
42
|
# @return [void]
|
42
|
-
def required_keys(*
|
43
|
-
req_keys = args.collect{|a| String === a || Symbol === a}
|
43
|
+
def required_keys(*req_keys, &block)
|
44
44
|
common_keys = (@ratifiable_object.keys & req_keys)
|
45
45
|
|
46
|
+
if @ratifiable_object.empty?
|
47
|
+
validation_error("Cannot find required keys in empty hash.")
|
48
|
+
return
|
49
|
+
end
|
50
|
+
|
51
|
+
if req_keys.nil? || req_keys.empty?
|
52
|
+
validation_error("No required keys given to compare Hash against.")
|
53
|
+
return
|
54
|
+
end
|
55
|
+
|
46
56
|
unless common_keys.size == req_keys.size
|
47
57
|
(req_keys - common_keys).each do |missed|
|
48
58
|
case missed
|
49
59
|
when Symbol then validation_error("Missing :#{missed}")
|
50
60
|
when String then validation_error("Missing #{missed}")
|
61
|
+
when respond_to?(:to_s)
|
62
|
+
validation_error("Missing #{missed.to_s}")
|
51
63
|
end
|
52
64
|
end
|
53
65
|
return
|
@@ -67,8 +79,18 @@ module Ratatouille
|
|
67
79
|
# All other values are ignored.
|
68
80
|
# @return [void]
|
69
81
|
def choice_of(choice_size=1, *key_list, &block)
|
82
|
+
unless key_list.nil? || key_list.empty?
|
83
|
+
validation_error("choice_of requires a key list to choose from")
|
84
|
+
return
|
85
|
+
end
|
86
|
+
|
70
87
|
unless choice_size =~ /\d+/ && choice_size > 0
|
71
|
-
validation_error("choice_of requires a positive integer
|
88
|
+
validation_error("choice_of requires a positive integer for choice size")
|
89
|
+
return
|
90
|
+
end
|
91
|
+
|
92
|
+
unless key_list.size > choice_size
|
93
|
+
validation_error("Key list size for 'choice_of' should be larger than choice size. Consider using required_keys instead.")
|
72
94
|
return
|
73
95
|
end
|
74
96
|
|
data/lib/ratatouille/nilclass.rb
CHANGED
data/lib/ratatouille/ratifier.rb
CHANGED
@@ -16,7 +16,7 @@ module Ratatouille
|
|
16
16
|
when Array then extend Ratatouille::ArrayMethods
|
17
17
|
end
|
18
18
|
|
19
|
-
instance_eval(&block) if block_given?
|
19
|
+
instance_eval( &block ) if block_given?
|
20
20
|
|
21
21
|
cleanup_errors
|
22
22
|
|
@@ -28,11 +28,13 @@ module Ratatouille
|
|
28
28
|
# @param [String] str
|
29
29
|
# @param [String] context
|
30
30
|
# @return [void]
|
31
|
-
def validation_error(
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
31
|
+
def validation_error(err_in, context="/")
|
32
|
+
case err_in
|
33
|
+
when String
|
34
|
+
return if err_in.blank?
|
35
|
+
@errors[context] = [] unless @errors[context]
|
36
|
+
@errors[context] << err_in
|
37
|
+
end
|
36
38
|
rescue Exception => e
|
37
39
|
@errors["/"] << e.message
|
38
40
|
end#validation_error
|
@@ -50,9 +52,7 @@ module Ratatouille
|
|
50
52
|
#
|
51
53
|
# @return [void]
|
52
54
|
def cleanup_errors
|
53
|
-
if errors_array.empty?
|
54
|
-
@errors = {}
|
55
|
-
end
|
55
|
+
@errors = {} if errors_array.empty?
|
56
56
|
rescue Exception => e
|
57
57
|
@errors["/"] << e.message
|
58
58
|
end#cleanup_errors
|
data/lib/ratatouille/version.rb
CHANGED
@@ -0,0 +1,111 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe "Ratatouille::ArrayMethods" do
|
4
|
+
[ :is_empty,
|
5
|
+
:is_not_empty,
|
6
|
+
:min_length,
|
7
|
+
:max_length,
|
8
|
+
:length_between
|
9
|
+
].each do |m|
|
10
|
+
it "block context should respond to #{m}" do
|
11
|
+
r = nil
|
12
|
+
RatifierTest.new([]) { r = self }
|
13
|
+
r.should respond_to m
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
describe "is_empty" do
|
18
|
+
it "should not be valid for non-empty array" do
|
19
|
+
RatifierTest.new(['bar']){ is_empty }.should_not be_valid
|
20
|
+
end
|
21
|
+
|
22
|
+
it "should be valid for empty array" do
|
23
|
+
RatifierTest.new([]){ is_empty }.should be_valid
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
describe "is_not_empty" do
|
28
|
+
it "should be valid for non-empty array" do
|
29
|
+
RatifierTest.new(['bar']){ is_not_empty }.should be_valid
|
30
|
+
end
|
31
|
+
|
32
|
+
it "should not be valid for empty array" do
|
33
|
+
RatifierTest.new([]){ is_not_empty }.should_not be_valid
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
describe "length_between" do
|
38
|
+
describe "for empty array" do
|
39
|
+
before(:each) do
|
40
|
+
@arr = []
|
41
|
+
end
|
42
|
+
|
43
|
+
it "should be valid with 0 min length and any positive, non-zero max_length" do
|
44
|
+
RatifierTest.new(@arr) { length_between(0) }.should be_valid
|
45
|
+
RatifierTest.new(@arr) { length_between(0, 0) }.should_not be_valid
|
46
|
+
RatifierTest.new(@arr) { length_between(0, 1) }.should be_valid
|
47
|
+
end
|
48
|
+
|
49
|
+
it "should be invalid with 1 min_length and any max_length" do
|
50
|
+
RatifierTest.new(@arr) { length_between(1) }.should_not be_valid
|
51
|
+
RatifierTest.new(@arr) { length_between(1,1) }.should_not be_valid
|
52
|
+
RatifierTest.new(@arr) { length_between(1,2) }.should_not be_valid
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
describe "for Array with 2 elements" do
|
57
|
+
before(:each) do
|
58
|
+
@arr = ['foo', 'bar']
|
59
|
+
end
|
60
|
+
|
61
|
+
it "should be valid with 1 min_length and any max_length above 1" do
|
62
|
+
RatifierTest.new(@arr) { length_between(1,1) }.should_not be_valid
|
63
|
+
RatifierTest.new(@arr) { length_between(1,2) }.should be_valid
|
64
|
+
end
|
65
|
+
|
66
|
+
it "should be invalid with 0 min_length and any max_length less than 2" do
|
67
|
+
RatifierTest.new(@arr) { length_between(0,0) }.should_not be_valid
|
68
|
+
RatifierTest.new(@arr) { length_between(0,1) }.should_not be_valid
|
69
|
+
RatifierTest.new(@arr) { length_between(0,2) }.should be_valid
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
describe "max_length" do
|
75
|
+
it "should be valid for proper length array with integer argument" do
|
76
|
+
RatifierTest.new([]) { max_length(1) }.should be_valid
|
77
|
+
RatifierTest.new(['foo']) { max_length(1) }.should be_valid
|
78
|
+
|
79
|
+
RatifierTest.new(['foo']) { max_length(0) }.should_not be_valid
|
80
|
+
end
|
81
|
+
|
82
|
+
it "should be invalid for non-numeric length" do
|
83
|
+
RatifierTest.new([]) { max_length({}) }.should_not be_valid
|
84
|
+
end
|
85
|
+
|
86
|
+
it "should be valid for properly transformed float values" do
|
87
|
+
RatifierTest.new(['foo']) { max_length(0.3) }.should_not be_valid
|
88
|
+
RatifierTest.new(['foo']) { max_length(1.3) }.should be_valid
|
89
|
+
RatifierTest.new(['foo', 'bar']) { max_length(1.3) }.should_not be_valid
|
90
|
+
end
|
91
|
+
end
|
92
|
+
|
93
|
+
describe "min_length" do
|
94
|
+
it "should be valid for proper length array with integer argument" do
|
95
|
+
RatifierTest.new([]) { min_length(0) }.should be_valid
|
96
|
+
RatifierTest.new([]) { min_length(1) }.should_not be_valid
|
97
|
+
|
98
|
+
RatifierTest.new(['foo']) { min_length(0) }.should be_valid
|
99
|
+
RatifierTest.new(['foo']) { min_length(1) }.should be_valid
|
100
|
+
end
|
101
|
+
|
102
|
+
it "should be invalid for non-numeric length" do
|
103
|
+
RatifierTest.new([]) { min_length({}) }.should_not be_valid
|
104
|
+
end
|
105
|
+
|
106
|
+
it "should be valid for properly transformed float values" do
|
107
|
+
RatifierTest.new([]) { min_length(0.3) }.should be_valid
|
108
|
+
RatifierTest.new([]) { min_length(1.3) }.should_not be_valid
|
109
|
+
end
|
110
|
+
end
|
111
|
+
end
|
@@ -0,0 +1,51 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe "Ratatouille::HashMethods" do
|
4
|
+
describe "is_empty" do
|
5
|
+
it "should be invalid for non-empty Hash" do
|
6
|
+
RatifierTest.new({:bar => 'biz'}){ is_empty }.should_not be_valid
|
7
|
+
end
|
8
|
+
|
9
|
+
it "should be valid for empty Hash" do
|
10
|
+
RatifierTest.new({}){ is_empty }.should be_valid
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
describe "is_not_empty" do
|
15
|
+
it "should be valid for non-empty hash" do
|
16
|
+
RatifierTest.new({:bar => "biz"}){ is_not_empty }.should be_valid
|
17
|
+
end
|
18
|
+
|
19
|
+
it "should not be valid for empty hash" do
|
20
|
+
RatifierTest.new({}){ is_not_empty }.should_not be_valid
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
describe "choice_of" do
|
25
|
+
it "should be invalid if key list is empty" do
|
26
|
+
RatifierTest.new({}) { choice_of(1, []) }.should_not be_valid
|
27
|
+
end
|
28
|
+
|
29
|
+
it "should be invalid if choice size less than 1" do
|
30
|
+
RatifierTest.new({}) { choice_of(0, [:foo]) }.should_not be_valid
|
31
|
+
end
|
32
|
+
|
33
|
+
it "should be invalid if choice list is not 1 more than choice size" do
|
34
|
+
RatifierTest.new({}) { choice_of(1, [:foo]) }.should_not be_valid
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
describe "required_keys" do
|
39
|
+
it "should be valid if Has contains all required keys" do
|
40
|
+
RatifierTest.new({:foo => "foo"}) { required_keys(:foo, :bar) }.should_not be_valid
|
41
|
+
end
|
42
|
+
|
43
|
+
it "should be invalid if hash is empty and key list is not" do
|
44
|
+
RatifierTest.new({}) { required_keys(:foo) }.should_not be_valid
|
45
|
+
end
|
46
|
+
|
47
|
+
it "should be invalid if Hash does not contain ALL keys in key list" do
|
48
|
+
RatifierTest.new({:foo => "foo"}) { required_keys(:foo, :bar) }.should_not be_valid
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
@@ -1,8 +1,12 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
-
class RatifierTest < Ratatouille::Ratifier; end
|
4
|
-
|
5
3
|
describe Ratatouille::Ratifier do
|
4
|
+
|
5
|
+
it "should be valid on instantiation of new object" do
|
6
|
+
e = RatifierTest.new({})
|
7
|
+
e.should be_valid
|
8
|
+
end
|
9
|
+
|
6
10
|
it "errors should contain one key within block of new instance" do
|
7
11
|
x = {}
|
8
12
|
e = RatifierTest.new({}){ x = @errors }
|
@@ -1,65 +1,18 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
-
|
3
|
+
include Ratatouille
|
4
4
|
|
5
5
|
describe Ratatouille do
|
6
6
|
|
7
|
-
describe "
|
8
|
-
|
9
|
-
|
10
|
-
e = RatifierTest.new({:bar => 'biz'}){ is_empty }
|
11
|
-
#puts "RatifierTest:"
|
12
|
-
#puts "@ratifiable_object.nil? #{e.ratifiable_object.nil?}"
|
13
|
-
#puts e.ratifiable_object.inspect
|
14
|
-
#puts "Errors.nil? #{e.errors.nil?}"
|
15
|
-
#puts e.errors.inspect
|
16
|
-
#puts "Errors.empty? #{e.errors.empty?}"
|
17
|
-
#puts "Valid?: #{e.valid?}"
|
18
|
-
e.valid?.should be_false
|
19
|
-
end
|
20
|
-
|
21
|
-
it "should be valid for empty Hash" do
|
22
|
-
e = RatifierTest.new({}){ is_empty }
|
23
|
-
e.valid?.should be_true
|
24
|
-
end
|
25
|
-
end
|
26
|
-
|
27
|
-
describe "is_not_empty" do
|
28
|
-
it "should be valid for non-empty hash" do
|
29
|
-
e = RatifierTest.new({:bar => "biz"}){ is_not_empty }
|
30
|
-
e.valid?.should be_true
|
31
|
-
end
|
32
|
-
|
33
|
-
it "should not be valid for empty hash" do
|
34
|
-
e = RatifierTest.new({}){ is_not_empty }
|
35
|
-
e.valid?.should be_false
|
36
|
-
end
|
37
|
-
end
|
38
|
-
end
|
39
|
-
|
40
|
-
describe "with Array" do
|
41
|
-
describe "is_empty" do
|
42
|
-
it "should not be valid for non-empty array" do
|
43
|
-
e = RatifierTest.new(['bar']){ is_empty }
|
44
|
-
e.valid?.should be_false
|
45
|
-
end
|
46
|
-
|
47
|
-
it "should be valid for empty array" do
|
48
|
-
e = RatifierTest.new([]){ is_empty }
|
49
|
-
e.valid?.should be_true
|
50
|
-
end
|
7
|
+
describe "ratify" do
|
8
|
+
it "should return a Ratatouille::Ratifier object" do
|
9
|
+
ratify({}).should be_a Ratatouille::Ratifier
|
51
10
|
end
|
52
|
-
|
53
|
-
describe "is_not_empty" do
|
54
|
-
it "should be valid for non-empty array" do
|
55
|
-
e = RatifierTest.new(['bar']){ is_not_empty }
|
56
|
-
e.valid?.should be_true
|
57
|
-
end
|
58
11
|
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
12
|
+
it "should evaluate block in context of a Ratatouille::Ratifier object" do
|
13
|
+
o = nil
|
14
|
+
ratify({}) { o = self }
|
15
|
+
o.should be_a Ratatouille::Ratifier
|
63
16
|
end
|
64
17
|
end
|
65
18
|
|
data/spec/spec_helper.rb
CHANGED
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
|
+
- 1
|
7
8
|
- 0
|
8
|
-
-
|
9
|
-
|
10
|
-
version: 0.9.2
|
9
|
+
- 0
|
10
|
+
version: 1.0.0
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Ryan Johnson
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2012-04-
|
18
|
+
date: 2012-04-14 00:00:00 Z
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|
21
21
|
name: rspec
|
@@ -70,11 +70,14 @@ files:
|
|
70
70
|
- lib/ratatouille/hash.rb
|
71
71
|
- lib/ratatouille/nilclass.rb
|
72
72
|
- lib/ratatouille/ratifier.rb
|
73
|
+
- lib/ratatouille/string.rb
|
73
74
|
- lib/ratatouille/version.rb
|
74
75
|
- ratatouille.gemspec
|
75
|
-
- spec/lib/
|
76
|
+
- spec/lib/ratatouille/array_spec.rb
|
77
|
+
- spec/lib/ratatouille/hash_spec.rb
|
78
|
+
- spec/lib/ratatouille/nilclass_spec.rb
|
79
|
+
- spec/lib/ratatouille/ratifier_spec.rb
|
76
80
|
- spec/lib/ratatouille_spec.rb
|
77
|
-
- spec/ratifier_spec.rb
|
78
81
|
- spec/spec_helper.rb
|
79
82
|
homepage: http://github.com/CITguy/ratatouille
|
80
83
|
licenses: []
|
@@ -110,8 +113,10 @@ signing_key:
|
|
110
113
|
specification_version: 3
|
111
114
|
summary: DSL for validating complex hashes
|
112
115
|
test_files:
|
113
|
-
- spec/lib/
|
116
|
+
- spec/lib/ratatouille/array_spec.rb
|
117
|
+
- spec/lib/ratatouille/hash_spec.rb
|
118
|
+
- spec/lib/ratatouille/nilclass_spec.rb
|
119
|
+
- spec/lib/ratatouille/ratifier_spec.rb
|
114
120
|
- spec/lib/ratatouille_spec.rb
|
115
|
-
- spec/ratifier_spec.rb
|
116
121
|
- spec/spec_helper.rb
|
117
122
|
has_rdoc:
|
data/spec/ratifier_spec.rb
DELETED