dao 3.3.0 → 4.2.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (60) hide show
  1. data/README +7 -0
  2. data/Rakefile +36 -17
  3. data/b.rb +38 -0
  4. data/dao.gemspec +41 -13
  5. data/lib/dao.rb +44 -13
  6. data/lib/dao/api.rb +1 -1
  7. data/lib/dao/api/context.rb +35 -45
  8. data/lib/dao/api/endpoints.rb +225 -91
  9. data/lib/dao/conducer.rb +437 -0
  10. data/lib/dao/conducer/attributes.rb +21 -0
  11. data/lib/dao/conducer/crud.rb +70 -0
  12. data/lib/dao/current.rb +66 -0
  13. data/lib/dao/db.rb +44 -5
  14. data/lib/dao/endpoint.rb +13 -1
  15. data/lib/dao/errors.rb +74 -59
  16. data/lib/dao/exceptions.rb +1 -2
  17. data/lib/dao/extractor.rb +68 -0
  18. data/lib/dao/form.rb +139 -46
  19. data/lib/dao/image_cache.rb +193 -0
  20. data/lib/dao/instance_exec.rb +1 -1
  21. data/lib/dao/name.rb +7 -0
  22. data/lib/dao/params.rb +16 -66
  23. data/lib/dao/rack.rb +3 -0
  24. data/lib/dao/rack/middleware.rb +5 -0
  25. data/lib/dao/rack/middleware/params_parser.rb +24 -0
  26. data/lib/dao/rails.rb +22 -5
  27. data/lib/dao/rails/lib/generators/dao/USAGE +2 -6
  28. data/lib/dao/rails/lib/generators/dao/dao_generator.rb +52 -7
  29. data/lib/dao/rails/lib/generators/dao/templates/api.rb +23 -7
  30. data/lib/dao/rails/lib/generators/dao/templates/api_controller.rb +24 -7
  31. data/lib/dao/rails/lib/generators/dao/templates/conducer.rb +64 -0
  32. data/lib/dao/rails/lib/generators/dao/templates/conducer_controller.rb +79 -0
  33. data/lib/dao/rails/lib/generators/dao/templates/dao.js +13 -6
  34. data/lib/dao/rails/lib/generators/dao/templates/dao_helper.rb +75 -11
  35. data/lib/dao/result.rb +1 -26
  36. data/lib/dao/slug.rb +37 -8
  37. data/lib/dao/status.rb +4 -0
  38. data/lib/dao/support.rb +155 -0
  39. data/lib/dao/validations.rb +48 -157
  40. data/lib/dao/validations/callback.rb +30 -0
  41. data/lib/dao/validations/common.rb +322 -320
  42. data/lib/dao/validations/validator.rb +219 -0
  43. data/test/active_model_conducer_lint_test.rb +19 -0
  44. data/test/api_test.rb +261 -0
  45. data/test/conducer_test.rb +205 -0
  46. data/test/db.yml +9 -0
  47. data/test/form_test.rb +42 -0
  48. data/test/support_test.rb +52 -0
  49. data/test/testing.rb +145 -24
  50. data/test/validations_test.rb +156 -0
  51. metadata +138 -21
  52. data/TODO +0 -33
  53. data/a.rb +0 -80
  54. data/db/dao.yml +0 -5
  55. data/lib/dao/api/interfaces.rb +0 -306
  56. data/lib/dao/interface.rb +0 -28
  57. data/lib/dao/presenter.rb +0 -129
  58. data/lib/dao/rails/lib/generators/dao/api_generator.rb +0 -3
  59. data/lib/dao/validations/base.rb +0 -68
  60. data/test/dao_test.rb +0 -506
@@ -1,178 +1,69 @@
1
1
  module Dao
2
- class Validations < ::Map
3
- # supporting classes
4
- #
5
- class Callback < ::Proc
6
- attr :options
2
+ module Validations
3
+ class Error < Dao::Error; end
7
4
 
8
- def initialize(options = {}, &block)
9
- @options = Dao.map_for(options || {})
10
- super(&block)
11
- end
5
+ Dao.load('validations/validator.rb')
6
+ Dao.load('validations/callback.rb')
7
+ Dao.load('validations/common.rb')
12
8
 
13
- def block
14
- self
9
+ ClassMethods = proc do
10
+ def validator
11
+ @validator ||= Validator.new(self)
15
12
  end
16
13
 
17
- class Chain
18
- def initialize
19
- @chain = []
20
- end
21
-
22
- def add(callback)
23
- @chain.push(callback)
24
- end
25
-
26
- def each(&block)
27
- @chain.each(&block)
28
- end
14
+ def validator=(validator)
15
+ @validator = validator
29
16
  end
30
- end
31
17
 
32
- # class methods
33
- #
34
- class << Validations
35
- def for(*args, &block)
36
- new(*args, &block)
18
+ %w(
19
+ validations
20
+ validates
21
+ ).each do |method|
22
+ module_eval <<-__, __FILE__, __LINE__
23
+ def self.#{ method }(*args, &block)
24
+ validator.#{ method }(*args, &block)
25
+ end
26
+ __
37
27
  end
38
28
  end
39
29
 
40
- # instance methods
41
- #
42
- attr_accessor :map
43
- attr_accessor :errors
44
- attr_accessor :status
45
- attr_accessor :ran
46
-
47
- def initialize(*args, &block)
48
- @map = args.first.is_a?(Map) ? args.shift : Map.new
49
- options = Map.options_for!(args)
50
-
51
- @errors = options[:errors] || Errors.new
52
- @status = options[:status] || Status.default
53
-
54
- @map.extend(InstanceExec) unless @map.respond_to?(:instance_exec)
55
- @ran = false
56
- super
57
- end
58
- alias_method('ran?', 'ran')
59
-
60
- def errors
61
- @map.errors
62
- end
63
-
64
- def run
65
- previous_errors = []
66
- new_errors = []
67
-
68
- errors.each_message do |keys, message|
69
- previous_errors.push([keys, message])
30
+ InstanceMethods = proc do
31
+ def validator
32
+ @validator ||= Validator.new(self)
70
33
  end
71
- errors.clear!
72
-
73
- depth_first_each do |keys, chain|
74
- chain.each do |callback|
75
- next unless callback and callback.respond_to?(:to_proc)
76
-
77
- number_of_errors = errors.size
78
- value = @map.get(keys)
79
- returned =
80
- catch(:valid) do
81
- args = [value, map].slice(0, callback.arity)
82
- @map.instance_exec(*args, &callback)
83
- end
84
-
85
- case returned
86
- when Hash
87
- map = Dao.map(returned)
88
- valid = map[:valid]
89
- message = map[:message]
90
-
91
- when TrueClass, FalseClass
92
- valid = returned
93
- message = nil
94
-
95
- else
96
- any_errors_added = errors.size > number_of_errors
97
- valid = !any_errors_added
98
- message = nil
99
- end
100
34
 
101
- message ||= callback.options[:message]
102
- message ||= (value.to_s.strip.empty? ? 'is blank' : 'is invalid')
103
-
104
- unless valid
105
- new_errors.push([keys, message])
106
- else
107
- new_errors.push([keys, Cleared])
108
- end
109
- end
35
+ def validator=(validator)
36
+ @validator = validator
110
37
  end
111
38
 
112
- previous_errors.each do |keys, message|
113
- errors.add(keys, message) unless new_errors.assoc(keys)
114
- end
115
-
116
- new_errors.each do |keys, value|
117
- next if value == Cleared
118
- message = value
119
- errors.add(keys, message)
39
+ %w(
40
+ validations
41
+ validates
42
+ validated?
43
+ validated!
44
+ validate
45
+ validate!
46
+ run_validations!
47
+ valid!
48
+ forcing_validity?
49
+ valid?
50
+ errors
51
+ status
52
+ ).each do |method|
53
+ module_eval <<-__, __FILE__, __LINE__
54
+ def #{ method }(*args, &block)
55
+ validator.#{ method }(*args, &block)
56
+ end
57
+ __
120
58
  end
121
-
122
- @ran = true
123
- return self
124
- end
125
- Cleared = 'Cleared'.freeze unless defined?(Cleared)
126
-
127
- def run!
128
- errors.clear!
129
- run
130
- end
131
-
132
- def each(&block)
133
- depth_first_each(&block)
134
59
  end
135
60
 
136
- def size
137
- size = 0
138
- depth_first_each{ size += 1 }
139
- size
140
- end
141
- alias_method('count', 'size')
142
- alias_method('length', 'size')
143
-
144
- def add(*args, &block)
145
- options = Dao.map_for(args.last.is_a?(Hash) ? args.pop : {})
146
- block = args.pop if args.last.respond_to?(:call)
147
- block ||= NotBlank
148
- callback = Callback.new(options, &block)
149
- set(args => Callback::Chain.new) unless has?(args)
150
- get(args).add(callback)
151
- callback
152
- end
153
- NotBlank = lambda{|value| !value.to_s.strip.empty?} unless defined?(NotBlank)
154
- end
155
-
156
- Dao.load('validations/base.rb')
157
- Dao.load('validations/common.rb')
158
-
159
- module Validations::Mixin
160
- def self.included(other)
161
- other.module_eval do
162
- include Validations::Base
163
- include Validations::Common
164
- end
61
+ def Validations.included(other)
62
+ other.send(:instance_eval, &ClassMethods)
63
+ other.send(:class_eval, &InstanceMethods)
64
+ other.send(:include, Common)
65
+ other.send(:extend, Common)
165
66
  super
166
67
  end
167
-
168
- def self.list
169
- @list ||= (
170
- c = Class.new
171
- a = c.instance_methods
172
- c.class_eval{ include Validations::Mixin }
173
- b = c.instance_methods
174
- b - a
175
- )
176
- end
177
68
  end
178
69
  end
@@ -0,0 +1,30 @@
1
+ module Dao
2
+ module Validations
3
+ class Callback < ::Proc
4
+ attr :options
5
+
6
+ def initialize(options = {}, &block)
7
+ @options = Map.for(options || {})
8
+ super(&block)
9
+ end
10
+
11
+ def block
12
+ self
13
+ end
14
+
15
+ class Chain
16
+ def initialize
17
+ @chain = []
18
+ end
19
+
20
+ def add(callback)
21
+ @chain.push(callback)
22
+ end
23
+
24
+ def each(&block)
25
+ @chain.each(&block)
26
+ end
27
+ end
28
+ end
29
+ end
30
+ end
@@ -1,457 +1,459 @@
1
1
  module Dao
2
- module Validations::Common
3
- def validates_length_of(*args)
4
- options = Dao.options_for!(args)
2
+ module Validations
3
+ module Common
4
+ def validates_length_of(*args)
5
+ options = Map.options_for!(args)
5
6
 
6
- message = options[:message]
7
+ message = options[:message]
7
8
 
8
- if options[:in].is_a?(Range)
9
- options[:minimum] = options[:in].begin
10
- options[:maximum] = options[:in].end
11
- end
12
- minimum = options[:minimum] || 1
13
- maximum = options[:maximum]
9
+ if options[:in].is_a?(Range)
10
+ options[:minimum] = options[:in].begin
11
+ options[:maximum] = options[:in].end
12
+ end
13
+ minimum = options[:minimum] || 1
14
+ maximum = options[:maximum]
14
15
 
15
- too_short = options[:too_short] || message || 'is too short'
16
- too_long = options[:too_long] || message || 'is too long'
16
+ too_short = options[:too_short] || message || 'is too short'
17
+ too_long = options[:too_long] || message || 'is too long'
17
18
 
18
- allow_nil = options[:allow_nil]
19
- allow_blank = options[:allow_blank]
19
+ allow_nil = options[:allow_nil]
20
+ allow_blank = options[:allow_blank]
20
21
 
21
- minimum = Float(minimum)
22
- maximum = Float(maximum) if maximum
22
+ minimum = Float(minimum)
23
+ maximum = Float(maximum) if maximum
23
24
 
24
- block =
25
- lambda do |value|
26
- m = Dao.map(:valid => true)
25
+ block =
26
+ lambda do |value|
27
+ m = Map(:valid => true)
27
28
 
28
- if value.nil? and allow_nil
29
- m[:valid] = true
30
- throw(:valid, m)
31
- end
29
+ if value.nil? and allow_nil
30
+ m[:valid] = true
31
+ throw(:validation, m)
32
+ end
32
33
 
33
- value = value.to_s.strip
34
+ value = value.to_s.strip
34
35
 
35
- if value.empty? and allow_blank
36
- m[:valid] = true
37
- throw(:valid, m)
38
- end
36
+ if value.empty? and allow_blank
37
+ m[:valid] = true
38
+ throw(:validation, m)
39
+ end
39
40
 
40
- if value.size < minimum
41
- m[:message] = too_short
42
- m[:valid] = false
43
- throw(:valid, m)
44
- end
41
+ if value.size < minimum
42
+ m[:message] = too_short
43
+ m[:valid] = false
44
+ throw(:validation, m)
45
+ end
46
+
47
+ if(maximum and(value.size > maximum))
48
+ m[:message] = too_long
49
+ m[:valid] = false
50
+ throw(:validation, m)
51
+ end
45
52
 
46
- if(maximum and(value.size > maximum))
47
- m[:message] = too_long
48
- m[:valid] = false
49
- throw(:valid, m)
53
+ m
50
54
  end
51
55
 
52
- m
53
- end
56
+ validates(*args, &block)
57
+ end
54
58
 
55
- validates(*args, &block)
56
- end
59
+ def validates_word_count_of(*args)
60
+ options = Map.options_for!(args)
57
61
 
58
- def validates_word_count_of(*args)
59
- options = Dao.options_for!(args)
62
+ message = options[:message]
60
63
 
61
- message = options[:message]
64
+ if options[:in].is_a?(Range)
65
+ options[:minimum] = options[:in].begin
66
+ options[:maximum] = options[:in].end
67
+ end
68
+ minimum = options[:minimum] || 1
69
+ maximum = options[:maximum]
62
70
 
63
- if options[:in].is_a?(Range)
64
- options[:minimum] = options[:in].begin
65
- options[:maximum] = options[:in].end
66
- end
67
- minimum = options[:minimum] || 1
68
- maximum = options[:maximum]
71
+ too_short = options[:too_short] || message || 'is too short'
72
+ too_long = options[:too_long] || message || 'is too long'
69
73
 
70
- too_short = options[:too_short] || message || 'is too short'
71
- too_long = options[:too_long] || message || 'is too long'
74
+ allow_nil = options[:allow_nil]
75
+ allow_blank = options[:allow_blank]
72
76
 
73
- allow_nil = options[:allow_nil]
74
- allow_blank = options[:allow_blank]
77
+ minimum = Float(minimum)
78
+ maximum = Float(maximum) if maximum
75
79
 
76
- minimum = Float(minimum)
77
- maximum = Float(maximum) if maximum
80
+ block =
81
+ lambda do |value|
82
+ m = Map(:valid => true)
78
83
 
79
- block =
80
- lambda do |value|
81
- m = Dao.map(:valid => true)
84
+ if value.nil? and allow_nil
85
+ m[:valid] = true
86
+ throw(:validation, m)
87
+ end
82
88
 
83
- if value.nil? and allow_nil
84
- m[:valid] = true
85
- throw(:valid, m)
86
- end
89
+ value = value.to_s.strip
87
90
 
88
- value = value.to_s.strip
91
+ if value.empty? and allow_blank
92
+ m[:valid] = true
93
+ throw(:validation, m)
94
+ end
89
95
 
90
- if value.empty? and allow_blank
91
- m[:valid] = true
92
- throw(:valid, m)
93
- end
96
+ words = value.split(/\s+/)
94
97
 
95
- words = value.split(/\s+/)
98
+ if words.size < minimum
99
+ m[:message] = too_short
100
+ m[:valid] = false
101
+ throw(:validation, m)
102
+ end
96
103
 
97
- if words.size < minimum
98
- m[:message] = too_short
99
- m[:valid] = false
100
- throw(:valid, m)
101
- end
104
+ if(maximum and(words.size > maximum))
105
+ m[:message] = too_long
106
+ m[:valid] = false
107
+ throw(:validation, m)
108
+ end
102
109
 
103
- if(maximum and(words.size > maximum))
104
- m[:message] = too_long
105
- m[:valid] = false
106
- throw(:valid, m)
110
+ m
107
111
  end
108
112
 
109
- m
110
- end
113
+ validates(*args, &block)
114
+ end
111
115
 
112
- validates(*args, &block)
113
- end
116
+ def validates_as_email(*args)
117
+ options = Map.options_for!(args)
114
118
 
115
- def validates_as_email(*args)
116
- options = Dao.options_for!(args)
119
+ message = options[:message] || "doesn't look like an email (username@domain.com)"
117
120
 
118
- message = options[:message] || "doesn't look like an email (username@domain.com)"
121
+ allow_nil = options[:allow_nil]
122
+ allow_blank = options[:allow_blank]
119
123
 
120
- allow_nil = options[:allow_nil]
121
- allow_blank = options[:allow_blank]
124
+ block =
125
+ lambda do |value|
126
+ m = Map(:valid => true)
122
127
 
123
- block =
124
- lambda do |value|
125
- m = Dao.map(:valid => true)
128
+ if value.nil? and allow_nil
129
+ m[:valid] = true
130
+ throw(:validation, m)
131
+ end
126
132
 
127
- if value.nil? and allow_nil
128
- m[:valid] = true
129
- throw(:valid, m)
130
- end
133
+ value = value.to_s.strip
131
134
 
132
- value = value.to_s.strip
135
+ if value.empty? and allow_blank
136
+ m[:valid] = true
137
+ throw(:validation, m)
138
+ end
133
139
 
134
- if value.empty? and allow_blank
135
- m[:valid] = true
136
- throw(:valid, m)
137
- end
140
+ parts = value.split(/@/)
138
141
 
139
- parts = value.split(/@/)
142
+ unless parts.size == 2
143
+ m[:valid] = false
144
+ throw(:validation, m)
145
+ end
140
146
 
141
- unless parts.size == 2
142
- m[:valid] = false
143
- throw(:valid, m)
147
+ m
144
148
  end
145
149
 
146
- m
147
- end
150
+ args.push(:message => message)
151
+ validates(*args, &block)
152
+ end
148
153
 
149
- args.push(:message => message)
150
- validates(*args, &block)
151
- end
154
+ def validates_as_url(*args)
155
+ options = Map.options_for!(args)
152
156
 
153
- def validates_as_url(*args)
154
- options = Dao.options_for!(args)
157
+ message = options[:message] || "doesn't look like a url (http://domain.com)"
155
158
 
156
- message = options[:message] || "doesn't look like a url (http://domain.com)"
159
+ allow_nil = options[:allow_nil]
160
+ allow_blank = options[:allow_blank]
157
161
 
158
- allow_nil = options[:allow_nil]
159
- allow_blank = options[:allow_blank]
162
+ block =
163
+ lambda do |value|
164
+ m = Map(:valid => true)
160
165
 
161
- block =
162
- lambda do |value|
163
- m = Dao.map(:valid => true)
166
+ if value.nil? and allow_nil
167
+ m[:valid] = true
168
+ throw(:validation, m)
169
+ end
164
170
 
165
- if value.nil? and allow_nil
166
- m[:valid] = true
167
- throw(:valid, m)
168
- end
171
+ value = value.to_s.strip
169
172
 
170
- value = value.to_s.strip
173
+ if value.empty? and allow_blank
174
+ m[:valid] = true
175
+ throw(:validation, m)
176
+ end
171
177
 
172
- if value.empty? and allow_blank
173
- m[:valid] = true
174
- throw(:valid, m)
175
- end
178
+ parts = value.split(%r|://|)
176
179
 
177
- parts = value.split(%r|://|)
180
+ unless parts.size >= 2
181
+ m[:valid] = false
182
+ throw(:validation, m)
183
+ end
178
184
 
179
- unless parts.size >= 2
180
- m[:valid] = false
181
- throw(:valid, m)
185
+ m
182
186
  end
183
187
 
184
- m
185
- end
188
+ args.push(:message => message)
189
+ validates(*args, &block)
190
+ end
186
191
 
187
- args.push(:message => message)
188
- validates(*args, &block)
189
- end
192
+ def validates_as_phone(*args)
193
+ options = Map.options_for!(args)
190
194
 
191
- def validates_as_phone(*args)
192
- options = Dao.options_for!(args)
195
+ message = options[:message] || "doesn't look like a phone number (012.345.6789)"
193
196
 
194
- message = options[:message] || "doesn't look like a phone number (012.345.6789)"
197
+ allow_nil = options[:allow_nil]
198
+ allow_blank = options[:allow_blank]
195
199
 
196
- allow_nil = options[:allow_nil]
197
- allow_blank = options[:allow_blank]
200
+ block =
201
+ lambda do |value|
202
+ m = Map(:valid => true)
198
203
 
199
- block =
200
- lambda do |value|
201
- m = Dao.map(:valid => true)
204
+ if value.nil? and allow_nil
205
+ m[:valid] = true
206
+ throw(:validation, m)
207
+ end
202
208
 
203
- if value.nil? and allow_nil
204
- m[:valid] = true
205
- throw(:valid, m)
206
- end
209
+ value = value.to_s.strip
207
210
 
208
- value = value.to_s.strip
211
+ if value.empty? and allow_blank
212
+ m[:valid] = true
213
+ throw(:validation, m)
214
+ end
209
215
 
210
- if value.empty? and allow_blank
211
- m[:valid] = true
212
- throw(:valid, m)
213
- end
216
+ parts = value.scan(/\d+/)
214
217
 
215
- parts = value.scan(/\d+/)
218
+ unless parts.size >= 1
219
+ m[:valid] = false
220
+ throw(:validation, m)
221
+ end
216
222
 
217
- unless parts.size >= 1
218
- m[:valid] = false
219
- throw(:valid, m)
223
+ m
220
224
  end
221
225
 
222
- m
223
- end
224
-
225
- args.push(:message => message)
226
- validates(*args, &block)
227
- end
226
+ args.push(:message => message)
227
+ validates(*args, &block)
228
+ end
228
229
 
229
- def validates_presence_of(*args)
230
- options = Dao.options_for!(args)
230
+ def validates_presence_of(*args)
231
+ options = Map.options_for!(args)
231
232
 
232
- message = options[:message] || 'is blank or missing'
233
+ message = options[:message] || 'is blank or missing'
233
234
 
234
- allow_nil = options[:allow_nil]
235
- allow_blank = options[:allow_blank]
235
+ allow_nil = options[:allow_nil]
236
+ allow_blank = options[:allow_blank]
236
237
 
237
- block =
238
- lambda do |value|
239
- m = Dao.map(:valid => true)
238
+ block =
239
+ lambda do |value|
240
+ m = Map(:valid => true)
240
241
 
241
- if value.nil?
242
- unless allow_nil
243
- m[:message] = message
244
- m[:valid] = false
245
- throw(:valid, m)
242
+ if value.nil?
243
+ unless allow_nil
244
+ m[:message] = message
245
+ m[:valid] = false
246
+ throw(:validation, m)
247
+ end
246
248
  end
247
- end
248
249
 
249
- value = value.to_s.strip
250
+ value = value.to_s.strip
250
251
 
251
- if value.empty?
252
- unless allow_blank
253
- m[:message] = message
254
- m[:valid] = false
255
- throw(:valid, m)
252
+ if value.empty?
253
+ unless allow_blank
254
+ m[:message] = message
255
+ m[:valid] = false
256
+ throw(:validation, m)
257
+ end
256
258
  end
257
- end
258
259
 
259
- m
260
- end
261
-
262
- validates(*args, &block)
263
- end
260
+ m
261
+ end
264
262
 
265
- def validates_any_of(*args)
266
- options = Dao.options_for!(args)
267
- list = args + Array(options.delete(:keys)) + Array(options.delete(:or))
263
+ validates(*args, &block)
264
+ end
268
265
 
269
- list.each do |args|
270
- candidates = list.dup
271
- candidates.delete(args)
266
+ def validates_any_of(*args)
267
+ options = Map.options_for!(args)
268
+ list = args + Array(options.delete(:keys)) + Array(options.delete(:or))
272
269
 
273
- message = options[:message] || "(or #{ candidates.map{|candidate| Array(candidate).join('.')}.join(', ') } ) is blank or missing"
274
- allow_nil = options[:allow_nil]
275
- allow_blank = options[:allow_blank]
270
+ list.each do |args|
271
+ candidates = list.dup
272
+ candidates.delete(args)
276
273
 
277
- result = self.result
274
+ message = options[:message] || "(or #{ candidates.map{|candidate| Array(candidate).join('.')}.join(', ') } ) is blank or missing"
275
+ allow_nil = options[:allow_nil]
276
+ allow_blank = options[:allow_blank]
278
277
 
279
- block =
280
- lambda do |value|
281
- m = Dao.map(:valid => true)
282
- values = list.map{|key| result.get(key)}
283
- valid = false
284
- values.each do |val|
285
- if val
286
- valid = true
287
- break
288
- end
278
+ result = self.result
289
279
 
290
- if val.nil?
291
- if allow_nil
280
+ block =
281
+ lambda do |value|
282
+ m = Map(:valid => true)
283
+ values = list.map{|key| result.get(key)}
284
+ valid = false
285
+ values.each do |val|
286
+ if val
292
287
  valid = true
293
288
  break
294
289
  end
295
- end
296
290
 
297
- val = val.to_s.strip
291
+ if val.nil?
292
+ if allow_nil
293
+ valid = true
294
+ break
295
+ end
296
+ end
298
297
 
299
- if val.empty?
300
- if allow_blank
301
- valid = true
302
- break
298
+ val = val.to_s.strip
299
+
300
+ if val.empty?
301
+ if allow_blank
302
+ valid = true
303
+ break
304
+ end
303
305
  end
304
306
  end
305
- end
306
307
 
307
- unless valid
308
- if value.nil?
309
- unless allow_nil
310
- m[:message] = message
311
- m[:valid] = false
312
- throw(:valid, m)
308
+ unless valid
309
+ if value.nil?
310
+ unless allow_nil
311
+ m[:message] = message
312
+ m[:valid] = false
313
+ throw(:validation, m)
314
+ end
313
315
  end
314
- end
315
316
 
316
- value = value.to_s.strip
317
+ value = value.to_s.strip
317
318
 
318
- if value.empty?
319
- unless allow_blank
320
- m[:message] = message
321
- m[:valid] = false
322
- throw(:valid, m)
319
+ if value.empty?
320
+ unless allow_blank
321
+ m[:message] = message
322
+ m[:valid] = false
323
+ throw(:validation, m)
324
+ end
323
325
  end
324
326
  end
325
- end
326
327
 
327
- m
328
- end
329
- validates(*args, &block)
328
+ m
329
+ end
330
+ validates(*args, &block)
331
+ end
330
332
  end
331
- end
332
333
 
333
- def validates_all_of(*args)
334
- options = Dao.options_for!(args)
335
- list = args + Array(options.delete(:keys)) + Array(options.delete(:or))
334
+ def validates_all_of(*args)
335
+ options = Map.options_for!(args)
336
+ list = args + Array(options.delete(:keys)) + Array(options.delete(:or))
336
337
 
337
- list.each do |args|
338
- candidates = list.dup
339
- candidates.delete(args)
340
-
341
- message = options[:message] || "(and #{ candidates.map{|candidate| Array(candidate).join('.')}.join(', ') } ) is blank or missing"
342
- allow_nil = options[:allow_nil]
343
- allow_blank = options[:allow_blank]
338
+ list.each do |args|
339
+ candidates = list.dup
340
+ candidates.delete(args)
344
341
 
345
- result = self.result
342
+ message = options[:message] || "(and #{ candidates.map{|candidate| Array(candidate).join('.')}.join(', ') } ) is blank or missing"
343
+ allow_nil = options[:allow_nil]
344
+ allow_blank = options[:allow_blank]
346
345
 
347
- block =
348
- lambda do |value|
349
- m = Dao.map(:valid => true)
346
+ result = self.result
350
347
 
351
- values = list.map{|key| result.get(key)}
352
- valid = true
353
- values.each do |val|
354
- if val
355
- break
356
- end
348
+ block =
349
+ lambda do |value|
350
+ m = Map(:valid => true)
357
351
 
358
- if val.nil?
359
- unless allow_nil
360
- valid = false
352
+ values = list.map{|key| result.get(key)}
353
+ valid = true
354
+ values.each do |val|
355
+ if val
361
356
  break
362
357
  end
363
- end
364
358
 
365
- val = val.to_s.strip
359
+ if val.nil?
360
+ unless allow_nil
361
+ valid = false
362
+ break
363
+ end
364
+ end
366
365
 
367
- if val.empty?
368
- unless allow_blank
369
- valid = false
370
- break
366
+ val = val.to_s.strip
367
+
368
+ if val.empty?
369
+ unless allow_blank
370
+ valid = false
371
+ break
372
+ end
371
373
  end
372
374
  end
373
- end
374
375
 
375
- unless valid
376
- if value.nil?
377
- unless allow_nil
378
- m[:message] = message
379
- m[:valid] = false
380
- throw(:valid, m)
376
+ unless valid
377
+ if value.nil?
378
+ unless allow_nil
379
+ m[:message] = message
380
+ m[:valid] = false
381
+ throw(:validation, m)
382
+ end
381
383
  end
382
- end
383
384
 
384
- value = value.to_s.strip
385
+ value = value.to_s.strip
385
386
 
386
- if value.empty?
387
- unless allow_blank
388
- m[:message] = message
389
- m[:valid] = false
390
- throw(:valid, m)
387
+ if value.empty?
388
+ unless allow_blank
389
+ m[:message] = message
390
+ m[:valid] = false
391
+ throw(:validation, m)
392
+ end
391
393
  end
392
394
  end
393
- end
394
395
 
395
- m
396
- end
397
- validates(*args, &block)
396
+ m
397
+ end
398
+ validates(*args, &block)
399
+ end
398
400
  end
399
- end
400
401
 
401
- def validates_confirmation_of(*args)
402
- options = Dao.options_for!(args)
402
+ def validates_confirmation_of(*args)
403
+ options = Map.options_for!(args)
403
404
 
404
405
 
405
- confirmation_key = args.map{|k| k.to_s}
406
- last = confirmation_key.pop
407
- last = "#{ last }_confirmation" unless last =~ /_confirmation$/
408
- confirmation_key.push(last)
406
+ confirmation_key = args.map{|k| k.to_s}
407
+ last = confirmation_key.pop
408
+ last = "#{ last }_confirmation" unless last =~ /_confirmation$/
409
+ confirmation_key.push(last)
409
410
 
410
- key = args.map{|k| k.to_s}
411
- last = key.pop
412
- last.sub!(/_confirmation$/, '')
413
- key.push(last)
411
+ key = args.map{|k| k.to_s}
412
+ last = key.pop
413
+ last.sub!(/_confirmation$/, '')
414
+ key.push(last)
414
415
 
415
- message = options[:message] || "does not match #{ key.join('.') }"
416
+ message = options[:message] || "does not match #{ key.join('.') }"
416
417
 
417
- allow_nil = options[:allow_nil]
418
- allow_blank = options[:allow_blank]
418
+ allow_nil = options[:allow_nil]
419
+ allow_blank = options[:allow_blank]
419
420
 
420
- block =
421
- lambda do |value|
422
- m = Dao.map(:valid => true)
421
+ block =
422
+ lambda do |value|
423
+ m = Map(:valid => true)
423
424
 
424
- if value.nil?
425
- unless allow_nil
426
- m[:message] = message
427
- m[:valid] = false
428
- throw(:valid, m)
425
+ if value.nil?
426
+ unless allow_nil
427
+ m[:message] = message
428
+ m[:valid] = false
429
+ throw(:validation, m)
430
+ end
429
431
  end
430
- end
431
432
 
432
- value = value.to_s.strip
433
+ value = value.to_s.strip
433
434
 
434
- if value.empty?
435
- unless allow_blank
435
+ if value.empty?
436
+ unless allow_blank
437
+ m[:message] = message
438
+ m[:valid] = false
439
+ throw(:validation, m)
440
+ end
441
+ end
442
+
443
+ target = get(key).to_s.strip
444
+ confirmed = target == value
445
+
446
+ unless confirmed
436
447
  m[:message] = message
437
448
  m[:valid] = false
438
- throw(:valid, m)
449
+ throw(:validation, m)
439
450
  end
440
- end
441
451
 
442
- target = get(key).to_s.strip
443
- confirmed = target == value
444
-
445
- unless confirmed
446
- m[:message] = message
447
- m[:valid] = false
448
- throw(:valid, m)
452
+ m
449
453
  end
450
454
 
451
- m
452
- end
453
-
454
- validates(confirmation_key, &block)
455
+ validates(confirmation_key, &block)
456
+ end
455
457
  end
456
458
  end
457
459