ratatouille 1.2.0 → 1.2.2

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -33,12 +33,22 @@ Within a block, the ratifiable\_object method provides the object that is to be
33
33
  This will change when using *given\_key*.
34
34
 
35
35
 
36
+ ### name
37
+
38
+ Within a block, the name method provides the name of the scope. This can be used in your custom validation messages.
39
+
40
+
36
41
 
37
42
  ## Usage
38
43
 
39
44
  All of the following methods perform validation on the *ratifiable\_object* defined in scope of the block.
40
45
 
41
46
 
47
+ ### is\_a?
48
+
49
+ Method to check if ratifiable\_object matches given class. Will not validate without a given class.
50
+
51
+
42
52
  ### given\_key
43
53
 
44
54
  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*.
@@ -210,6 +220,8 @@ You should use the **validation\_error** method to add your own errors to the Ra
210
220
 
211
221
  Used to insert validation error message into the Ratifier object.
212
222
 
223
+ * Scope name prepended to every validation error
224
+
213
225
 
214
226
  #### Syntax
215
227
 
@@ -116,6 +116,26 @@ module Ratatouille
116
116
 
117
117
  return all_errs
118
118
  end#errors_array
119
+
120
+
121
+ # Validate against ratifiable_object class
122
+ #
123
+ # @param [Class] klass
124
+ def is_a?(klass=nil, &block)
125
+ if klass.nil?
126
+ validation_error("must provide a Class for is_a?")
127
+ return
128
+ end
129
+
130
+ unless klass === @ratifiable_object
131
+ validation_error("object not of type #{klass}")
132
+ return
133
+ end
134
+
135
+ instance_eval(&block) if block_given?
136
+ rescue Exception => e
137
+ validation_error("#{e.message}", "/")
138
+ end#is_a?
119
139
  end#Ratifier
120
140
 
121
141
  end
@@ -1,4 +1,4 @@
1
1
  module Ratatouille
2
2
  # Gem Version
3
- VERSION = "1.2.0"
3
+ VERSION = "1.2.2"
4
4
  end
@@ -1,5 +1,7 @@
1
1
  require 'spec_helper'
2
2
 
3
+ class Something; end
4
+
3
5
  describe Ratatouille::Ratifier do
4
6
 
5
7
  it "should be valid on instantiation of new object" do
@@ -150,4 +152,30 @@ describe Ratatouille::Ratifier do
150
152
  r.name.should == 'Hash'
151
153
  end
152
154
  end
155
+
156
+ describe "is_a?" do
157
+ it "should be invalid if no class given" do
158
+ RatifierTest.new({}) { is_a? }.should_not be_valid
159
+ end
160
+
161
+ it "should not progress into block if invalid" do
162
+ f = false
163
+ RatifierTest.new({}) do
164
+ is_a? { f = true }
165
+ end
166
+ f.should be_false
167
+ end
168
+
169
+ [
170
+ [['foo'], Array],
171
+ [{:foo => "foo"}, Hash],
172
+ [Object.new, Object],
173
+ [nil, NilClass]
174
+ ].each do |obj, klass|
175
+ it "#{obj.inspect} should be valid if matches #{klass}" do
176
+ RatifierTest.new(obj) { is_a?(klass) }.should be_valid
177
+ RatifierTest.new(obj) { is_a?(Something) }.should_not be_valid
178
+ end
179
+ end
180
+ end
153
181
  end
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: 31
4
+ hash: 27
5
5
  prerelease:
6
6
  segments:
7
7
  - 1
8
8
  - 2
9
- - 0
10
- version: 1.2.0
9
+ - 2
10
+ version: 1.2.2
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-14 00:00:00 Z
18
+ date: 2012-04-15 00:00:00 Z
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
21
21
  name: rspec