ratatouille 1.3.4 → 1.3.6

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/CHANGELOG.md CHANGED
@@ -1,3 +1,11 @@
1
+ ## 1.3.6
2
+
3
+ ### Updates
4
+
5
+ * New Array Method: **ratify\_each** iterates over each item in an array to perform validation logic.
6
+ * Tweaks to Ratatouille::Ratifier.errors\_array to provide additional functionality required for **ratify\_each**.
7
+ * Refactored logic in Ratatouille::Ratifier into new method (**namespace\_errors\_array**).
8
+
1
9
  ## 1.3.4
2
10
 
3
11
  ### Updates
@@ -2,6 +2,28 @@ module Ratatouille
2
2
 
3
3
  # Module used to provide Array-specific validation methods
4
4
  module ArrayMethods
5
+
6
+ # Iterator method to encapsulate validation
7
+ #
8
+ # @note Method will NOT work without a block
9
+ # @param [Hash] options
10
+ # @option options [Hash] :each
11
+ # options to pass to ratifier for each item in array
12
+ # @option options [Integer] :min_length
13
+ # @option options [Integer] :max_length
14
+ # @return [void]
15
+ def ratify_each(options={}, &block)
16
+ if block_given?
17
+ item_name = options[:name] || "array_item"
18
+ @ratifiable_object.each_with_index do |obj, i|
19
+ options[:name] = "#{item_name}"
20
+ child_object = Ratatouille::Ratifier.new(obj, options, &block)
21
+ @errors["/"] << child_object.errors unless child_object.valid?
22
+ end
23
+ end
24
+ end#ratify_each
25
+
26
+
5
27
  # Define Minimum Length of Array
6
28
  #
7
29
  # @param [Integer] min_size
@@ -92,38 +92,35 @@ module Ratatouille
92
92
 
93
93
  # @param [Hash] hsh Hash to act upon.
94
94
  # @return [Array]
95
- def errors_array(hsh = @errors)
96
- return [] unless Hash === hsh
95
+ def errors_array(item = @errors)
97
96
  all_errs = []
98
97
 
99
- hsh.each_pair do |k,v|
100
- pair_errs = case v
101
- when Hash then errors_array(v)
102
- when Array then v
103
- else []
104
- end
105
-
106
- nsed_errs = pair_errs.collect do |e|
107
- split_err = e.split("|")
108
-
109
- ctxt, err = "", e
110
- ctxt, err = split_err if split_err.size == 2
111
-
112
- case k
113
- when String
114
- ctxt = "#{k}#{ctxt}" unless k == '/'
115
- when Symbol
116
- ctxt = ":#{k}#{ctxt}"
98
+ case item
99
+ when Array
100
+ item.each_with_index do |e,i|
101
+ item_errs = case e
102
+ when Hash, Array then errors_array(e)
103
+ when String then e
104
+ else []
117
105
  end
118
106
 
119
- "/#{ctxt}|#{err}"
107
+ all_errs << namespace_error_array(item_errs, "[#{i}]")
108
+ all_errs.flatten!
120
109
  end
110
+ when Hash
111
+ item.each_pair do |k,v|
112
+ pair_errs = case v
113
+ when Hash, Array then errors_array(v)
114
+ when String then v
115
+ else []
116
+ end
121
117
 
122
- all_errs << nsed_errs
123
- all_errs.flatten!
118
+ all_errs << namespace_error_array(pair_errs, k)
119
+ all_errs.flatten!
120
+ end
124
121
  end
125
122
 
126
- return all_errs
123
+ return Array(all_errs)
127
124
  end#errors_array
128
125
 
129
126
 
@@ -131,6 +128,7 @@ module Ratatouille
131
128
  # Will not validate without class.
132
129
  #
133
130
  # @param [Class] klass
131
+ # @return [void]
134
132
  def is_a?(klass=nil, &block)
135
133
  if klass.nil?
136
134
  validation_error("must provide a Class for is_a?")
@@ -152,12 +150,14 @@ module Ratatouille
152
150
  # validation methods defined in various places.
153
151
  #
154
152
  # @param [Hash] options
153
+ # @option options [Class] :is_a (nil)
154
+ # @option options [Boolean] :required (false)
155
155
  # @option options [Boolean] :unwrap_block (false)
156
156
  # Perform block validation only -- skip method validation logic.
157
157
  def parse_options(options={})
158
- @unwrap_block = options.fetch(:unwrap_block, false)
159
158
  @is_a = options.fetch(:is_a, nil)
160
159
  @required = options.fetch(:required, false)
160
+ @unwrap_block = options.fetch(:unwrap_block, false)
161
161
  end#parse_options
162
162
 
163
163
 
@@ -203,6 +203,36 @@ module Ratatouille
203
203
  instance_eval(&block) if block_given?
204
204
  return
205
205
  end#method_missing
206
+
207
+
208
+ # Properly prepend namespace definition to array of errors
209
+ #
210
+ # @param [Array] arr
211
+ # @param [String,Symbol] namespace
212
+ # @return [Array]
213
+ def namespace_error_array(arr=[], namespace="")
214
+ errs_out = arr.collect do |e|
215
+ split_err = e.split("|")
216
+
217
+ ctxt, err = "", e
218
+ ctxt, err = split_err if split_err.size == 2
219
+
220
+ case namespace
221
+ when String
222
+ ctxt = "#{namespace}#{ctxt}" unless namespace =~ /^\/.?/
223
+ when Symbol
224
+ ctxt = ":#{namespace}#{ctxt}"
225
+ end
226
+
227
+ if ctxt =~ /^\/.?/
228
+ "#{ctxt}|#{err}"
229
+ else
230
+ "/#{ctxt}|#{err}"
231
+ end
232
+ end
233
+
234
+ return Array(errs_out)
235
+ end#namespace_error_array
206
236
  end#Ratifier
207
237
 
208
238
  end
@@ -1,4 +1,4 @@
1
1
  module Ratatouille
2
2
  # Gem Version
3
- VERSION = "1.3.4"
3
+ VERSION = "1.3.6"
4
4
  end
@@ -1,7 +1,8 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe "Ratatouille::ArrayMethods" do
4
- [ :min_length,
4
+ [ :ratify_each,
5
+ :min_length,
5
6
  :max_length,
6
7
  :length_between
7
8
  ].each do |m|
@@ -12,6 +13,39 @@ describe "Ratatouille::ArrayMethods" do
12
13
  end
13
14
  end
14
15
 
16
+ describe "ratify_each" do
17
+ it "should set block name to name in options" do
18
+ n = ""
19
+ RatifierTest.new(['foo', 'bar']) do
20
+ ratify_each { n = name }
21
+ end
22
+ n.should match /^(array_item)/i
23
+
24
+ RatifierTest.new(['foo', 'bar']) do
25
+ ratify_each(:name => "foo") { n = name }
26
+ end
27
+ n.should match /^foo/i
28
+ end
29
+
30
+ it "should be invalid for given array" do
31
+ r = RatifierTest.new(['bar', 'biz']) do
32
+ ratify_each(:is_a => String) do
33
+ validation_error("#{ro} is not 'bang'") unless ro == 'bang'
34
+ end
35
+ end
36
+ r.errors_array.length.should == 2
37
+ end
38
+
39
+ it "should be valid for given array" do
40
+ r = RatifierTest.new(['bar', 'biz']) do
41
+ ratify_each(:is_a => String) do
42
+ validation_error("too short") unless ro.size > 2
43
+ end
44
+ end
45
+ r.should be_valid
46
+ end
47
+ end
48
+
15
49
  describe "is_empty" do
16
50
  it "should not be valid for non-empty array" do
17
51
  RatifierTest.new(['bar']){ is_empty }.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: 19
4
+ hash: 23
5
5
  prerelease:
6
6
  segments:
7
7
  - 1
8
8
  - 3
9
- - 4
10
- version: 1.3.4
9
+ - 6
10
+ version: 1.3.6
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-25 00:00:00 Z
18
+ date: 2012-04-27 00:00:00 Z
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
21
21
  name: rspec