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
@@ -23,7 +23,6 @@ module Dao
23
23
  end
24
24
 
25
25
  %w( to_dao as_dao ).each do |method|
26
-
27
26
  module_eval <<-__, __FILE__, __LINE__ - 1
28
27
 
29
28
  def #{ method }(object, *args, &block)
@@ -47,10 +46,9 @@ module Dao
47
46
  __
48
47
  end
49
48
 
50
-
51
49
  def unindent!(s)
52
50
  margin = nil
53
- s.each do |line|
51
+ s.each_line do |line|
54
52
  next if line =~ %r/^\s*$/
55
53
  margin = line[%r/^\s*/] and break
56
54
  end
@@ -67,4 +65,8 @@ module Dao
67
65
  def name_for(path, *keys)
68
66
  Form.name_for(path, *keys)
69
67
  end
68
+
69
+ def parse(*args, &block)
70
+ Params.process(*args, &block)
71
+ end
70
72
  end
@@ -1,5 +1,7 @@
1
1
  module Dao
2
2
  class Validations < ::Map
3
+ # supporting classes
4
+ #
3
5
  class Callback < ::Proc
4
6
  attr :options
5
7
 
@@ -27,62 +29,38 @@ module Dao
27
29
  end
28
30
  end
29
31
 
30
-
31
32
  # class methods
32
33
  #
33
34
  class << Validations
34
35
  def for(*args, &block)
35
36
  new(*args, &block)
36
37
  end
37
-
38
- def cast(*args)
39
- if args.size == 1
40
- value = args.first
41
- value.is_a?(self) ? value : self.for(value)
42
- else
43
- self.for(*args)
44
- end
45
- end
46
38
  end
47
39
 
48
40
  # instance methods
49
41
  #
50
- attr_accessor :result
42
+ attr_accessor :map
43
+ attr_accessor :errors
44
+ attr_accessor :status
51
45
  attr_accessor :ran
52
46
 
53
47
  def initialize(*args, &block)
54
- @result = args.shift if args.first.is_a?(Result)
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
55
  @ran = false
56
56
  super
57
57
  end
58
58
  alias_method('ran?', 'ran')
59
59
 
60
- def params
61
- result.params
62
- end
63
-
64
- def data
65
- result.data
66
- end
67
-
68
60
  def errors
69
- result.errors
61
+ @map.errors
70
62
  end
71
63
 
72
- def each(&block)
73
- depth_first_each(&block)
74
- end
75
-
76
- def size
77
- size = 0
78
- depth_first_each{ size += 1 }
79
- size
80
- end
81
- alias_method('count', 'size')
82
- alias_method('length', 'size')
83
-
84
- Cleared = 'Cleared'.freeze unless defined?(Cleared)
85
-
86
64
  def run
87
65
  previous_errors = []
88
66
  new_errors = []
@@ -97,14 +75,11 @@ module Dao
97
75
  next unless callback and callback.respond_to?(:to_proc)
98
76
 
99
77
  number_of_errors = errors.size
100
- value = data.get(keys)
78
+ value = @map.get(keys)
101
79
  returned =
102
80
  catch(:valid) do
103
- if result
104
- result.instance_exec(value, &callback)
105
- else
106
- callback.call(value)
107
- end
81
+ args = [value, map].slice(0, callback.arity)
82
+ @map.instance_exec(*args, &callback)
108
83
  end
109
84
 
110
85
  case returned
@@ -147,428 +122,57 @@ module Dao
147
122
  @ran = true
148
123
  return self
149
124
  end
125
+ Cleared = 'Cleared'.freeze unless defined?(Cleared)
150
126
 
151
127
  def run!
152
128
  errors.clear!
153
129
  run
154
130
  end
155
131
 
156
- NotNil = lambda{|value| !value.nil?}
132
+ def each(&block)
133
+ depth_first_each(&block)
134
+ end
135
+
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')
157
143
 
158
144
  def add(*args, &block)
159
145
  options = Dao.map_for(args.last.is_a?(Hash) ? args.pop : {})
160
146
  block = args.pop if args.last.respond_to?(:call)
161
- block ||= NotNil
147
+ block ||= NotBlank
162
148
  callback = Callback.new(options, &block)
163
149
  set(args => Callback::Chain.new) unless has?(args)
164
150
  get(args).add(callback)
165
151
  callback
166
152
  end
153
+ NotBlank = lambda{|value| !value.to_s.strip.empty?} unless defined?(NotBlank)
167
154
  end
168
155
 
169
- module Validations::Common
170
- def validates_length_of(*args)
171
- options = Dao.options_for!(args)
172
-
173
- message = options[:message]
174
-
175
- if options[:in].is_a?(Range)
176
- options[:minimum] = options[:in].begin
177
- options[:maximum] = options[:in].end
178
- end
179
- minimum = options[:minimum] || 1
180
- maximum = options[:maximum]
181
-
182
- too_short = options[:too_short] || message || 'is too short'
183
- too_long = options[:too_long] || message || 'is too long'
184
-
185
- allow_nil = options[:allow_nil]
186
- allow_blank = options[:allow_blank]
187
-
188
- minimum = Float(minimum)
189
- maximum = Float(maximum) if maximum
190
-
191
- block =
192
- lambda do |value|
193
- map = Dao.map(:valid => true)
194
-
195
- if value.nil? and allow_nil
196
- map[:valid] = true
197
- throw(:valid, map)
198
- end
199
-
200
- value = value.to_s.strip
201
-
202
- if value.empty? and allow_blank
203
- map[:valid] = true
204
- throw(:valid, map)
205
- end
206
-
207
- if value.size < minimum
208
- map[:message] = too_short
209
- map[:valid] = false
210
- throw(:valid, map)
211
- end
212
-
213
- if(maximum and(value.size > maximum))
214
- map[:message] = too_long
215
- map[:valid] = false
216
- throw(:valid, map)
217
- end
218
-
219
- map
220
- end
221
-
222
- validates(*args, &block)
223
- end
224
-
225
- def validates_word_count_of(*args)
226
- options = Dao.options_for!(args)
227
-
228
- message = options[:message]
229
-
230
- if options[:in].is_a?(Range)
231
- options[:minimum] = options[:in].begin
232
- options[:maximum] = options[:in].end
233
- end
234
- minimum = options[:minimum] || 1
235
- maximum = options[:maximum]
236
-
237
- too_short = options[:too_short] || message || 'is too short'
238
- too_long = options[:too_long] || message || 'is too long'
239
-
240
- allow_nil = options[:allow_nil]
241
- allow_blank = options[:allow_blank]
242
-
243
- minimum = Float(minimum)
244
- maximum = Float(maximum) if maximum
245
-
246
- block =
247
- lambda do |value|
248
- map = Dao.map(:valid => true)
249
-
250
- if value.nil? and allow_nil
251
- map[:valid] = true
252
- throw(:valid, map)
253
- end
254
-
255
- value = value.to_s.strip
256
-
257
- if value.empty? and allow_blank
258
- map[:valid] = true
259
- throw(:valid, map)
260
- end
261
-
262
- words = value.split(/\s+/)
263
-
264
- if words.size < minimum
265
- map[:message] = too_short
266
- map[:valid] = false
267
- throw(:valid, map)
268
- end
269
-
270
- if(maximum and(words.size > maximum))
271
- map[:message] = too_long
272
- map[:valid] = false
273
- throw(:valid, map)
274
- end
275
-
276
- map
277
- end
278
-
279
- validates(*args, &block)
280
- end
281
-
282
- def validates_as_email(*args)
283
- options = Dao.options_for!(args)
284
-
285
- message = options[:message] || "doesn't look like an email (username@domain.com)"
286
-
287
- allow_nil = options[:allow_nil]
288
- allow_blank = options[:allow_blank]
289
-
290
- block =
291
- lambda do |value|
292
- map = Dao.map(:valid => true)
293
-
294
- if value.nil? and allow_nil
295
- map[:valid] = true
296
- throw(:valid, map)
297
- end
298
-
299
- value = value.to_s.strip
300
-
301
- if value.empty? and allow_blank
302
- map[:valid] = true
303
- throw(:valid, map)
304
- end
305
-
306
- parts = value.split(/@/)
307
-
308
- unless parts.size == 2
309
- map[:valid] = false
310
- throw(:valid, map)
311
- end
312
-
313
- map
314
- end
315
-
316
- args.push(:message => message)
317
- validates(*args, &block)
318
- end
319
-
320
- def validates_as_url(*args)
321
- options = Dao.options_for!(args)
322
-
323
- message = options[:message] || "doesn't look like a url (http://domain.com)"
324
-
325
- allow_nil = options[:allow_nil]
326
- allow_blank = options[:allow_blank]
327
-
328
- block =
329
- lambda do |value|
330
- map = Dao.map(:valid => true)
331
-
332
- if value.nil? and allow_nil
333
- map[:valid] = true
334
- throw(:valid, map)
335
- end
336
-
337
- value = value.to_s.strip
338
-
339
- if value.empty? and allow_blank
340
- map[:valid] = true
341
- throw(:valid, map)
342
- end
343
-
344
- parts = value.split(%r|://|)
345
-
346
- unless parts.size >= 2
347
- map[:valid] = false
348
- throw(:valid, map)
349
- end
350
-
351
- map
352
- end
353
-
354
- args.push(:message => message)
355
- validates(*args, &block)
356
- end
357
-
358
- def validates_as_phone(*args)
359
- options = Dao.options_for!(args)
360
-
361
- message = options[:message] || "doesn't look like a phone number (012.345.6789)"
362
-
363
- allow_nil = options[:allow_nil]
364
- allow_blank = options[:allow_blank]
365
-
366
- block =
367
- lambda do |value|
368
- map = Dao.map(:valid => true)
369
-
370
- if value.nil? and allow_nil
371
- map[:valid] = true
372
- throw(:valid, map)
373
- end
374
-
375
- value = value.to_s.strip
376
-
377
- if value.empty? and allow_blank
378
- map[:valid] = true
379
- throw(:valid, map)
380
- end
381
-
382
- parts = value.scan(/\d+/)
383
-
384
- unless parts.size >= 1
385
- map[:valid] = false
386
- throw(:valid, map)
387
- end
388
-
389
- map
390
- end
391
-
392
- args.push(:message => message)
393
- validates(*args, &block)
394
- end
395
-
396
- def validates_presence_of(*args)
397
- options = Dao.options_for!(args)
398
-
399
- message = options[:message] || 'is blank or missing'
400
-
401
- allow_nil = options[:allow_nil]
402
- allow_blank = options[:allow_blank]
403
-
404
- block =
405
- lambda do |value|
406
- map = Dao.map(:valid => true)
407
-
408
- if value.nil?
409
- unless allow_nil
410
- map[:message] = message
411
- map[:valid] = false
412
- throw(:valid, map)
413
- end
414
- end
415
-
416
- value = value.to_s.strip
417
-
418
- if value.empty?
419
- unless allow_blank
420
- map[:message] = message
421
- map[:valid] = false
422
- throw(:valid, map)
423
- end
424
- end
425
-
426
- map
427
- end
428
-
429
- validates(*args, &block)
430
- end
431
-
432
- def validates_any_of(*args)
433
- options = Dao.options_for!(args)
434
- list = args
435
-
436
- list.each do |args|
437
- candidates = list.dup
438
- candidates.delete(args)
439
-
440
- message = options[:message] || "(or #{ candidates.map{|candidate| Array(candidate).join('.')}.join(', ') } ) is blank or missing"
441
- allow_nil = options[:allow_nil]
442
- allow_blank = options[:allow_blank]
443
-
444
- result = self.result
445
-
446
- block =
447
- lambda do |value|
448
- map = Dao.map(:valid => true)
449
- values = list.map{|key| result.get(key)}
450
- valid = false
451
- values.each do |val|
452
- if val
453
- valid = true
454
- break
455
- end
456
-
457
- if val.nil?
458
- if allow_nil
459
- valid = true
460
- break
461
- end
462
- end
463
-
464
- val = val.to_s.strip
465
-
466
- if val.empty?
467
- if allow_blank
468
- valid = true
469
- break
470
- end
471
- end
472
- end
473
-
474
- unless valid
475
- if value.nil?
476
- unless allow_nil
477
- map[:message] = message
478
- map[:valid] = false
479
- throw(:valid, map)
480
- end
481
- end
482
-
483
- value = value.to_s.strip
484
-
485
- if value.empty?
486
- unless allow_blank
487
- map[:message] = message
488
- map[:valid] = false
489
- throw(:valid, map)
490
- end
491
- end
492
- end
493
-
494
- map
495
- end
496
- validates(*args, &block)
497
- end
498
- end
499
-
500
- def validates_all_of(*args)
501
- options = Dao.options_for!(args)
502
- list = args
503
-
504
- list.each do |args|
505
- candidates = list.dup
506
- candidates.delete(args)
507
-
508
- message = options[:message] || "(and #{ candidates.map{|candidate| Array(candidate).join('.')}.join(', ') } ) is blank or missing"
509
- allow_nil = options[:allow_nil]
510
- allow_blank = options[:allow_blank]
511
-
512
- result = self.result
513
-
514
- block =
515
- lambda do |value|
516
- map = Dao.map(:valid => true)
517
-
518
- values = list.map{|key| result.get(key)}
519
- valid = true
520
- values.each do |val|
521
- if val
522
- break
523
- end
156
+ Dao.load('validations/base.rb')
157
+ Dao.load('validations/common.rb')
524
158
 
525
- if val.nil?
526
- unless allow_nil
527
- valid = false
528
- break
529
- end
530
- end
531
-
532
- val = val.to_s.strip
533
-
534
- if val.empty?
535
- unless allow_blank
536
- valid = false
537
- break
538
- end
539
- end
540
- end
541
-
542
- unless valid
543
- if value.nil?
544
- unless allow_nil
545
- map[:message] = message
546
- map[:valid] = false
547
- throw(:valid, map)
548
- end
549
- end
550
-
551
- value = value.to_s.strip
552
-
553
- if value.empty?
554
- unless allow_blank
555
- map[:message] = message
556
- map[:valid] = false
557
- throw(:valid, map)
558
- end
559
- end
560
- end
561
-
562
- map
563
- end
564
- validates(*args, &block)
159
+ module Validations::Mixin
160
+ def self.included(other)
161
+ other.module_eval do
162
+ include Validations::Base
163
+ include Validations::Common
565
164
  end
165
+ super
566
166
  end
567
- end
568
167
 
569
- def Validations.add(method_name, &block)
570
- ::Dao::Validations::Common.module_eval do
571
- define_method(method_name, &block)
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
+ )
572
176
  end
573
177
  end
574
178
  end