ratatouille 1.3.6 → 1.3.8

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG.md CHANGED
@@ -1,3 +1,10 @@
1
+ ## 1.3.8
2
+
3
+ ### Updates
4
+
5
+ * New is\_boolean method to test if ratifiable\_object value is of a TrueClass or FalseClass.
6
+ * All other classes fail validation.
7
+
1
8
  ## 1.3.6
2
9
 
3
10
  ### Updates
@@ -146,6 +146,23 @@ module Ratatouille
146
146
  end#is_a?
147
147
 
148
148
 
149
+ # Check if ratifiable object is a TrueClass or FalseClass.
150
+ # Any other class will result in a failed validation.
151
+ #
152
+ # @return [Boolean]
153
+ def is_boolean(&block)
154
+ case @ratifiable_object
155
+ when TrueClass, FalseClass
156
+ instance_eval(&block) if block_given?
157
+ else
158
+ validation_error("object is not a boolean")
159
+ return
160
+ end
161
+ rescue Exception => e
162
+ validation_error("#{e.message}", "/")
163
+ end#is_boolean?
164
+
165
+
149
166
  # Parse out common options into instance_variables for use within the
150
167
  # validation methods defined in various places.
151
168
  #
@@ -1,4 +1,4 @@
1
1
  module Ratatouille
2
2
  # Gem Version
3
- VERSION = "1.3.6"
3
+ VERSION = "1.3.8"
4
4
  end
@@ -3,32 +3,58 @@ require 'spec_helper'
3
3
  class Something; end
4
4
 
5
5
  describe Ratatouille::Ratifier do
6
+ let(:valid_ratifier) { RatifierTest.new({}) }
7
+ let(:invalid_ratifier) { RatifierTest.new({}) { is_not_empty } }
6
8
 
7
- it "should be valid on instantiation of new object" do
8
- e = RatifierTest.new({})
9
- e.should be_valid
10
- end
9
+ context ".new" do
10
+ it "should be valid" do
11
+ valid_ratifier.should be_valid
12
+ end
13
+
14
+ context "errors_array" do
15
+ it "should be empty on new object" do
16
+ valid_ratifier.errors_array.should be_empty
17
+ end
18
+ end
11
19
 
12
- it "should not progress into block if :is_a validation fails" do
13
- f = false
14
- RatifierTest.new({}, :is_a => String) { f = true }
15
- f.should be_false
20
+ context "errors within validation block" do
21
+ before(:each) do
22
+ x = {}
23
+ RatifierTest.new({}){ x = @errors }
24
+ @errs = x
25
+ end
16
26
 
17
- g = false
18
- RatifierTest.new({}, :is_a => Hash) { g = true }
19
- g.should be_true
27
+ it "errors should contain one key" do
28
+ @errs.keys.size.should == 1
29
+ @errs.keys.should == ['/']
30
+ end
20
31
 
21
- h = false
22
- RatifierTest.new({}) { h = true }
23
- h.should be_true
24
- end
32
+ it "errors['/'] should be empty" do
33
+ @errs['/'].should be_empty
34
+ end
35
+ end
25
36
 
26
- it "errors should contain one key within block of new instance" do
27
- x = {}
28
- e = RatifierTest.new({}){ x = @errors }
29
- x.keys.size.should == 1
30
- x.keys.should == ['/']
31
- x['/'].should be_empty
37
+ context "with :is_a" do
38
+ it "shouldn't enter validation block for Hash if expecting a String" do
39
+ block_entered = false
40
+ RatifierTest.new({}, :is_a => String) { block_entered = true }
41
+ block_entered.should be_false
42
+ end
43
+
44
+ it "should enter validation block for Hash if expecting a Hash" do
45
+ block_entered = false
46
+ RatifierTest.new({}, :is_a => Hash) { block_entered = true }
47
+ block_entered.should be_true
48
+ end
49
+ end
50
+
51
+ context "without :is_a" do
52
+ it "should enter validation block" do
53
+ block_entered = false
54
+ RatifierTest.new({}) { block_entered = true }
55
+ block_entered.should be_true
56
+ end
57
+ end
32
58
  end
33
59
 
34
60
  describe "when attempting to call an undefined method" do
@@ -67,11 +93,6 @@ describe Ratatouille::Ratifier do
67
93
  end
68
94
 
69
95
  it "should add the error to the '/' context by default" do
70
- test = RatifierTest.new({}) do
71
- # NO ERRORS
72
- end
73
- test.errors['/'].should be_empty
74
-
75
96
  test = RatifierTest.new({}) do
76
97
  validation_error("foo")
77
98
  end
@@ -80,9 +101,7 @@ describe Ratatouille::Ratifier do
80
101
 
81
102
  it "should add an error to an explicit context (even if it doesn't exist)" do
82
103
  ctxt = "foo"
83
- test = RatifierTest.new({}) do
84
- # NO ERRORS
85
- end
104
+ test = valid_ratifier
86
105
  test.errors[ctxt].should be_nil
87
106
 
88
107
  test = RatifierTest.new({}) do
@@ -93,72 +112,56 @@ describe Ratatouille::Ratifier do
93
112
  end
94
113
 
95
114
  describe "valid?" do
96
- it "should be true if errors is empty?" do
97
- test = RatifierTest.new({}) do
98
- # No Validation = Valid Object
99
- end
100
- test.valid?.should be_true
101
- end
102
115
  end
103
116
 
104
117
  describe "instance variables" do
105
- before(:each) do
106
- @test = RatifierTest.new({})
107
- end
108
-
109
118
  describe "ratifiable_object" do
110
119
  it "should raise error if modification is attempted" do
111
- Proc.new { @test.ratifiable_object = {} }.should raise_error NoMethodError
120
+ Proc.new { valid_ratifier.ratifiable_object = {} }.should raise_error NoMethodError
112
121
  end
113
122
  end
114
123
 
115
124
  describe "errors" do
116
125
  it "should raise error if modification is attempted" do
117
- Proc.new { @test.errors = {} }.should raise_error NoMethodError
118
- Proc.new { @test.errors.delete("/") }.should raise_error TypeError
126
+ Proc.new { valid_ratifier.errors = {} }.should raise_error NoMethodError
127
+ Proc.new { valid_ratifier.errors.delete("/") }.should raise_error TypeError
119
128
  end
120
129
 
121
130
  it "should be empty on valid object" do
122
- ratifier = RatifierTest.new({:foo => "bar"}) do
123
- # No Validations = Valid Object
124
- end
125
- ratifier.errors.should be_empty
131
+ valid_ratifier.errors.should be_empty
126
132
  end
127
133
 
128
134
  it "should not be empty on invalid object" do
129
- ratifier = RatifierTest.new({:foo => "bar"}) { is_empty }
130
- ratifier.errors.should_not be_empty
135
+ invalid_ratifier.errors.should_not be_empty
131
136
  end
132
137
  end
133
138
 
134
139
  describe "errors_array" do
135
- it "should be empty on new Ratifier" do
136
- @test.errors_array.should be_empty
140
+ it "should have at least one String item for an invalid object" do
141
+ invalid_ratifier.errors_array.should have_at_least(1).String
137
142
  end
143
+ end
144
+ end
138
145
 
139
- it "should be empty on valid object" do
140
- ratifier = RatifierTest.new({}) do
141
- # No Validations = Valid Object
146
+ describe "is_boolean" do
147
+ [true, false].each do |b|
148
+ it "should enter block for #{b} value" do
149
+ block_entered = false
150
+ RatifierTest.new(b) do
151
+ is_boolean { block_entered = true }
142
152
  end
143
- ratifier.errors_array.should be_empty
144
- end
145
-
146
- it "should have at least one String item for an invalid object" do
147
- test = RatifierTest.new({:foo => "bar"}){ is_empty }
148
- test.errors_array.should_not be_empty
149
- test.errors_array.should have_at_least(1).String
153
+ block_entered.should be_true
150
154
  end
151
155
  end
152
156
  end
153
157
 
154
158
  describe "name" do
155
- it "should return the same value if called twice in a row" do
156
- r = RatifierTest.new({})
157
- r.name.should == r.name
159
+ it "should return same value if called twice in a row" do
160
+ valid_ratifier.name.should == valid_ratifier.name
158
161
  end
159
162
 
160
163
  it "should always return a String" do
161
- RatifierTest.new({}).name.should be_a String
164
+ valid_ratifier.name.should be_a String
162
165
  end
163
166
 
164
167
  it "should return the class of the object if :name isn't passed into options" do
@@ -175,7 +178,9 @@ describe Ratatouille::Ratifier do
175
178
  it "should not change the name if passed a non-string name" do
176
179
  r = RatifierTest.new({})
177
180
  r.name = NilClass
181
+ r.name.should == 'Hash'
178
182
  r.name = Object.new
183
+ r.name.should == 'Hash'
179
184
  r.name = nil
180
185
  r.name.should == 'Hash'
181
186
  end
@@ -202,22 +207,25 @@ describe Ratatouille::Ratifier do
202
207
  ].each do |obj, klass|
203
208
  it "#{obj.inspect} should be valid if matches #{klass}" do
204
209
  RatifierTest.new(obj) { is_a?(klass) }.should be_valid
210
+ end
211
+
212
+ it "#{obj.inspect} should NOT be valid if expecting Something object" do
205
213
  RatifierTest.new(obj) { is_a?(Something) }.should_not be_valid
206
214
  end
207
215
  end
208
216
  end
209
217
 
210
218
  describe "method_missing" do
211
- describe "for non-standard boolean methods" do
219
+ context "with non-standard boolean methods" do
220
+ let(:obj) { Object.new }
221
+
212
222
  it "should render object invalid for given method" do
213
- obj = Object.new
214
223
  obj.stub(:foo?).and_return(false)
215
224
  RatifierTest.new(obj) { is_foo }.should_not be_valid
216
225
  RatifierTest.new(obj) { is_not_foo }.should be_valid
217
226
  end
218
227
 
219
228
  it "should render object valid for given method" do
220
- obj = Object.new
221
229
  obj.stub(:bar?).and_return(true)
222
230
  RatifierTest.new(obj) { is_bar }.should be_valid
223
231
  RatifierTest.new(obj) { is_not_bar }.should_not be_valid
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: 23
4
+ hash: 11
5
5
  prerelease:
6
6
  segments:
7
7
  - 1
8
8
  - 3
9
- - 6
10
- version: 1.3.6
9
+ - 8
10
+ version: 1.3.8
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-27 00:00:00 Z
18
+ date: 2012-05-06 00:00:00 Z
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
21
21
  name: rspec