dao 2.2.3 → 3.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (79) hide show
  1. data/Rakefile +68 -113
  2. data/TODO +3 -9
  3. data/a.rb +25 -0
  4. data/dao.gemspec +70 -14
  5. data/lib/dao.rb +8 -8
  6. data/lib/dao/api.rb +1 -0
  7. data/lib/dao/api/context.rb +48 -23
  8. data/lib/dao/api/dsl.rb +1 -1
  9. data/lib/dao/api/interfaces.rb +149 -117
  10. data/lib/dao/api/modes.rb +24 -23
  11. data/lib/dao/api/routes.rb +9 -0
  12. data/lib/dao/data.rb +9 -2
  13. data/lib/dao/errors.rb +15 -11
  14. data/lib/dao/form.rb +46 -37
  15. data/lib/dao/interface.rb +1 -1
  16. data/lib/dao/mode.rb +45 -20
  17. data/lib/dao/params.rb +35 -53
  18. data/lib/dao/path.rb +6 -9
  19. data/lib/dao/rails/lib/generators/dao/templates/api.rb +1 -1
  20. data/lib/dao/rails/lib/generators/dao/templates/dao_helper.rb +16 -0
  21. data/lib/dao/result.rb +26 -133
  22. data/lib/dao/route.rb +87 -0
  23. data/lib/dao/status.rb +96 -94
  24. data/lib/dao/support.rb +5 -3
  25. data/lib/dao/validations.rb +46 -442
  26. data/lib/dao/validations/base.rb +68 -0
  27. data/lib/dao/validations/common.rb +463 -0
  28. data/test/dao_test.rb +214 -33
  29. metadata +20 -112
  30. data/lib/dao/rails/app/api.rb +0 -55
  31. data/lib/dao/rails/app/controllers/api_controller.rb +0 -99
  32. data/sample/rails_app/Gemfile +0 -33
  33. data/sample/rails_app/Gemfile.lock +0 -88
  34. data/sample/rails_app/README +0 -1
  35. data/sample/rails_app/Rakefile +0 -7
  36. data/sample/rails_app/app/api.rb +0 -55
  37. data/sample/rails_app/app/controllers/api_controller.rb +0 -99
  38. data/sample/rails_app/app/controllers/application_controller.rb +0 -3
  39. data/sample/rails_app/app/helpers/application_helper.rb +0 -2
  40. data/sample/rails_app/app/views/layouts/application.html.erb +0 -14
  41. data/sample/rails_app/config.ru +0 -4
  42. data/sample/rails_app/config/application.rb +0 -51
  43. data/sample/rails_app/config/boot.rb +0 -13
  44. data/sample/rails_app/config/database.yml +0 -22
  45. data/sample/rails_app/config/environment.rb +0 -5
  46. data/sample/rails_app/config/environments/development.rb +0 -26
  47. data/sample/rails_app/config/environments/production.rb +0 -49
  48. data/sample/rails_app/config/environments/test.rb +0 -35
  49. data/sample/rails_app/config/initializers/backtrace_silencers.rb +0 -7
  50. data/sample/rails_app/config/initializers/inflections.rb +0 -10
  51. data/sample/rails_app/config/initializers/mime_types.rb +0 -5
  52. data/sample/rails_app/config/initializers/secret_token.rb +0 -7
  53. data/sample/rails_app/config/initializers/session_store.rb +0 -8
  54. data/sample/rails_app/config/locales/en.yml +0 -5
  55. data/sample/rails_app/config/routes.rb +0 -62
  56. data/sample/rails_app/db/development.sqlite3 +0 -0
  57. data/sample/rails_app/db/seeds.rb +0 -7
  58. data/sample/rails_app/doc/README_FOR_APP +0 -2
  59. data/sample/rails_app/log/development.log +0 -27
  60. data/sample/rails_app/log/production.log +0 -0
  61. data/sample/rails_app/log/server.log +0 -0
  62. data/sample/rails_app/log/test.log +0 -0
  63. data/sample/rails_app/pubic/javascripts/dao.js +0 -148
  64. data/sample/rails_app/public/404.html +0 -26
  65. data/sample/rails_app/public/422.html +0 -26
  66. data/sample/rails_app/public/500.html +0 -26
  67. data/sample/rails_app/public/favicon.ico +0 -0
  68. data/sample/rails_app/public/images/rails.png +0 -0
  69. data/sample/rails_app/public/index.html +0 -239
  70. data/sample/rails_app/public/javascripts/application.js +0 -2
  71. data/sample/rails_app/public/javascripts/controls.js +0 -965
  72. data/sample/rails_app/public/javascripts/dragdrop.js +0 -974
  73. data/sample/rails_app/public/javascripts/effects.js +0 -1123
  74. data/sample/rails_app/public/javascripts/prototype.js +0 -6001
  75. data/sample/rails_app/public/javascripts/rails.js +0 -175
  76. data/sample/rails_app/public/robots.txt +0 -5
  77. data/sample/rails_app/script/rails +0 -6
  78. data/sample/rails_app/test/performance/browsing_test.rb +0 -9
  79. data/sample/rails_app/test/test_helper.rb +0 -13
@@ -0,0 +1,68 @@
1
+ module Dao
2
+ # objects must have and errors object and a status object to use this mixin
3
+ #
4
+ module Validations::Base
5
+ def validations
6
+ @validations ||= Validations.for(self)
7
+ end
8
+
9
+ def is_valid=(boolean)
10
+ @is_valid = !!boolean
11
+ end
12
+
13
+ def is_valid(*bool)
14
+ @is_valid ||= nil
15
+ @is_valid = !!bool.first unless bool.empty?
16
+ @is_valid
17
+ end
18
+
19
+ def valid!
20
+ @forcing_validity = true
21
+ end
22
+
23
+ def forcing_validity?
24
+ defined?(@forcing_validity) and @forcing_validity
25
+ end
26
+
27
+ def valid?(*args)
28
+ if forcing_validity?
29
+ true
30
+ else
31
+ options = Dao.options_for!(args)
32
+ validate unless validations.ran?
33
+ validate if options[:validate]
34
+ errors.empty? and status.ok?
35
+ end
36
+ end
37
+
38
+ def validate(*args, &block)
39
+ if !args.empty?
40
+ validations.add(*args, &block)
41
+ else
42
+ validations.run
43
+ status.update(420) if(status.ok? and !errors.empty?)
44
+ errors.empty? and status.ok?
45
+ end
46
+ end
47
+
48
+ # TODO - consider how to factor out this throw...
49
+ #
50
+ def validate!(*args, &block)
51
+ if !args.empty?
52
+ validations.add(*args, &block)
53
+ end
54
+ @forcing_validity = false
55
+ validations.run!
56
+ status.update(420) if(status.ok? and !errors.empty?)
57
+ throw(:result, nil) unless(errors.empty? and status.ok?)
58
+ end
59
+
60
+ def validates(*args, &block)
61
+ validations.add(*args, &block)
62
+ end
63
+
64
+ def validations
65
+ @validations ||= Validations.for(self)
66
+ end
67
+ end
68
+ end
@@ -0,0 +1,463 @@
1
+ module Dao
2
+ module Validations::Common
3
+ def validates_length_of(*args)
4
+ options = Dao.options_for!(args)
5
+
6
+ message = options[:message]
7
+
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]
14
+
15
+ too_short = options[:too_short] || message || 'is too short'
16
+ too_long = options[:too_long] || message || 'is too long'
17
+
18
+ allow_nil = options[:allow_nil]
19
+ allow_blank = options[:allow_blank]
20
+
21
+ minimum = Float(minimum)
22
+ maximum = Float(maximum) if maximum
23
+
24
+ block =
25
+ lambda do |value|
26
+ m = Dao.map(:valid => true)
27
+
28
+ if value.nil? and allow_nil
29
+ m[:valid] = true
30
+ throw(:valid, m)
31
+ end
32
+
33
+ value = value.to_s.strip
34
+
35
+ if value.empty? and allow_blank
36
+ m[:valid] = true
37
+ throw(:valid, m)
38
+ end
39
+
40
+ if value.size < minimum
41
+ m[:message] = too_short
42
+ m[:valid] = false
43
+ throw(:valid, m)
44
+ end
45
+
46
+ if(maximum and(value.size > maximum))
47
+ m[:message] = too_long
48
+ m[:valid] = false
49
+ throw(:valid, m)
50
+ end
51
+
52
+ m
53
+ end
54
+
55
+ validates(*args, &block)
56
+ end
57
+
58
+ def validates_word_count_of(*args)
59
+ options = Dao.options_for!(args)
60
+
61
+ message = options[:message]
62
+
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]
69
+
70
+ too_short = options[:too_short] || message || 'is too short'
71
+ too_long = options[:too_long] || message || 'is too long'
72
+
73
+ allow_nil = options[:allow_nil]
74
+ allow_blank = options[:allow_blank]
75
+
76
+ minimum = Float(minimum)
77
+ maximum = Float(maximum) if maximum
78
+
79
+ block =
80
+ lambda do |value|
81
+ m = Dao.map(:valid => true)
82
+
83
+ if value.nil? and allow_nil
84
+ m[:valid] = true
85
+ throw(:valid, m)
86
+ end
87
+
88
+ value = value.to_s.strip
89
+
90
+ if value.empty? and allow_blank
91
+ m[:valid] = true
92
+ throw(:valid, m)
93
+ end
94
+
95
+ words = value.split(/\s+/)
96
+
97
+ if words.size < minimum
98
+ m[:message] = too_short
99
+ m[:valid] = false
100
+ throw(:valid, m)
101
+ end
102
+
103
+ if(maximum and(words.size > maximum))
104
+ m[:message] = too_long
105
+ m[:valid] = false
106
+ throw(:valid, m)
107
+ end
108
+
109
+ m
110
+ end
111
+
112
+ validates(*args, &block)
113
+ end
114
+
115
+ def validates_as_email(*args)
116
+ options = Dao.options_for!(args)
117
+
118
+ message = options[:message] || "doesn't look like an email (username@domain.com)"
119
+
120
+ allow_nil = options[:allow_nil]
121
+ allow_blank = options[:allow_blank]
122
+
123
+ block =
124
+ lambda do |value|
125
+ m = Dao.map(:valid => true)
126
+
127
+ if value.nil? and allow_nil
128
+ m[:valid] = true
129
+ throw(:valid, m)
130
+ end
131
+
132
+ value = value.to_s.strip
133
+
134
+ if value.empty? and allow_blank
135
+ m[:valid] = true
136
+ throw(:valid, m)
137
+ end
138
+
139
+ parts = value.split(/@/)
140
+
141
+ unless parts.size == 2
142
+ m[:valid] = false
143
+ throw(:valid, m)
144
+ end
145
+
146
+ m
147
+ end
148
+
149
+ args.push(:message => message)
150
+ validates(*args, &block)
151
+ end
152
+
153
+ def validates_as_url(*args)
154
+ options = Dao.options_for!(args)
155
+
156
+ message = options[:message] || "doesn't look like a url (http://domain.com)"
157
+
158
+ allow_nil = options[:allow_nil]
159
+ allow_blank = options[:allow_blank]
160
+
161
+ block =
162
+ lambda do |value|
163
+ m = Dao.map(:valid => true)
164
+
165
+ if value.nil? and allow_nil
166
+ m[:valid] = true
167
+ throw(:valid, m)
168
+ end
169
+
170
+ value = value.to_s.strip
171
+
172
+ if value.empty? and allow_blank
173
+ m[:valid] = true
174
+ throw(:valid, m)
175
+ end
176
+
177
+ parts = value.split(%r|://|)
178
+
179
+ unless parts.size >= 2
180
+ m[:valid] = false
181
+ throw(:valid, m)
182
+ end
183
+
184
+ m
185
+ end
186
+
187
+ args.push(:message => message)
188
+ validates(*args, &block)
189
+ end
190
+
191
+ def validates_as_phone(*args)
192
+ options = Dao.options_for!(args)
193
+
194
+ message = options[:message] || "doesn't look like a phone number (012.345.6789)"
195
+
196
+ allow_nil = options[:allow_nil]
197
+ allow_blank = options[:allow_blank]
198
+
199
+ block =
200
+ lambda do |value|
201
+ m = Dao.map(:valid => true)
202
+
203
+ if value.nil? and allow_nil
204
+ m[:valid] = true
205
+ throw(:valid, m)
206
+ end
207
+
208
+ value = value.to_s.strip
209
+
210
+ if value.empty? and allow_blank
211
+ m[:valid] = true
212
+ throw(:valid, m)
213
+ end
214
+
215
+ parts = value.scan(/\d+/)
216
+
217
+ unless parts.size >= 1
218
+ m[:valid] = false
219
+ throw(:valid, m)
220
+ end
221
+
222
+ m
223
+ end
224
+
225
+ args.push(:message => message)
226
+ validates(*args, &block)
227
+ end
228
+
229
+ def validates_presence_of(*args)
230
+ options = Dao.options_for!(args)
231
+
232
+ message = options[:message] || 'is blank or missing'
233
+
234
+ allow_nil = options[:allow_nil]
235
+ allow_blank = options[:allow_blank]
236
+
237
+ block =
238
+ lambda do |value|
239
+ m = Dao.map(:valid => true)
240
+
241
+ if value.nil?
242
+ unless allow_nil
243
+ m[:message] = message
244
+ m[:valid] = false
245
+ throw(:valid, m)
246
+ end
247
+ end
248
+
249
+ value = value.to_s.strip
250
+
251
+ if value.empty?
252
+ unless allow_blank
253
+ m[:message] = message
254
+ m[:valid] = false
255
+ throw(:valid, m)
256
+ end
257
+ end
258
+
259
+ m
260
+ end
261
+
262
+ validates(*args, &block)
263
+ end
264
+
265
+ def validates_any_of(*args)
266
+ options = Dao.options_for!(args)
267
+ list = args + Array(options.delete(:keys)) + Array(options.delete(:or))
268
+
269
+ list.each do |args|
270
+ candidates = list.dup
271
+ candidates.delete(args)
272
+
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]
276
+
277
+ result = self.result
278
+
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
289
+
290
+ if val.nil?
291
+ if allow_nil
292
+ valid = true
293
+ break
294
+ end
295
+ end
296
+
297
+ val = val.to_s.strip
298
+
299
+ if val.empty?
300
+ if allow_blank
301
+ valid = true
302
+ break
303
+ end
304
+ end
305
+ end
306
+
307
+ unless valid
308
+ if value.nil?
309
+ unless allow_nil
310
+ m[:message] = message
311
+ m[:valid] = false
312
+ throw(:valid, m)
313
+ end
314
+ end
315
+
316
+ value = value.to_s.strip
317
+
318
+ if value.empty?
319
+ unless allow_blank
320
+ m[:message] = message
321
+ m[:valid] = false
322
+ throw(:valid, m)
323
+ end
324
+ end
325
+ end
326
+
327
+ m
328
+ end
329
+ validates(*args, &block)
330
+ end
331
+ end
332
+
333
+ def validates_all_of(*args)
334
+ options = Dao.options_for!(args)
335
+ list = args + Array(options.delete(:keys)) + Array(options.delete(:or))
336
+
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]
344
+
345
+ result = self.result
346
+
347
+ block =
348
+ lambda do |value|
349
+ m = Dao.map(:valid => true)
350
+
351
+ values = list.map{|key| result.get(key)}
352
+ valid = true
353
+ values.each do |val|
354
+ if val
355
+ break
356
+ end
357
+
358
+ if val.nil?
359
+ unless allow_nil
360
+ valid = false
361
+ break
362
+ end
363
+ end
364
+
365
+ val = val.to_s.strip
366
+
367
+ if val.empty?
368
+ unless allow_blank
369
+ valid = false
370
+ break
371
+ end
372
+ end
373
+ end
374
+
375
+ unless valid
376
+ if value.nil?
377
+ unless allow_nil
378
+ m[:message] = message
379
+ m[:valid] = false
380
+ throw(:valid, m)
381
+ end
382
+ end
383
+
384
+ value = value.to_s.strip
385
+
386
+ if value.empty?
387
+ unless allow_blank
388
+ m[:message] = message
389
+ m[:valid] = false
390
+ throw(:valid, m)
391
+ end
392
+ end
393
+ end
394
+
395
+ m
396
+ end
397
+ validates(*args, &block)
398
+ end
399
+ end
400
+
401
+ def validates_confirmation_of(*args)
402
+ options = Dao.options_for!(args)
403
+
404
+
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)
409
+
410
+ key = args.map{|k| k.to_s}
411
+ last = key.pop
412
+ last.sub!(/_confirmation$/, '')
413
+ key.push(last)
414
+
415
+ message = options[:message] || "does not match #{ key.join('.') }"
416
+
417
+ allow_nil = options[:allow_nil]
418
+ allow_blank = options[:allow_blank]
419
+
420
+ block =
421
+ lambda do |value|
422
+ m = Dao.map(:valid => true)
423
+
424
+ if value.nil?
425
+ unless allow_nil
426
+ m[:message] = message
427
+ m[:valid] = false
428
+ throw(:valid, m)
429
+ end
430
+ end
431
+
432
+ value = value.to_s.strip
433
+
434
+ if value.empty?
435
+ unless allow_blank
436
+ m[:message] = message
437
+ m[:valid] = false
438
+ throw(:valid, m)
439
+ end
440
+ end
441
+
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)
449
+ end
450
+
451
+ m
452
+ end
453
+
454
+ validates(confirmation_key, &block)
455
+ end
456
+ end
457
+
458
+ def Validations.add(method_name, &block)
459
+ ::Dao::Validations::Common.module_eval do
460
+ define_method(method_name, &block)
461
+ end
462
+ end
463
+ end