adaptation 0.0.4 → 0.0.5
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG +2 -0
- data/lib/adaptation/message.rb +7 -214
- data/lib/adaptation/test/test_help.rb +0 -9
- data/lib/adaptation/validateable.rb +61 -0
- data/lib/adaptation.rb +1 -0
- metadata +3 -2
data/CHANGELOG
CHANGED
data/lib/adaptation/message.rb
CHANGED
@@ -89,23 +89,18 @@ module Adaptation
|
|
89
89
|
# has_one :attribute, :type
|
90
90
|
# has_one :text, :owner
|
91
91
|
# validates_presence_of :owner, :type
|
92
|
-
#
|
92
|
+
# validates_value_of :type, "indexed"
|
93
93
|
# end
|
94
94
|
#
|
95
95
|
#
|
96
96
|
class Message
|
97
97
|
|
98
|
-
@@validations = {}
|
99
|
-
@@default_error_messages = {
|
100
|
-
:invalid_value => "Invalid value",
|
101
|
-
:must_be_present => "Must be present"
|
102
|
-
}
|
103
|
-
@@errors = []
|
104
98
|
@@classes_with_brothers = []
|
105
99
|
cattr_reader :classes_with_brothers
|
106
100
|
cattr_reader :validations
|
107
101
|
cattr_reader :objects
|
108
102
|
|
103
|
+
include Validateable
|
109
104
|
include ROXML
|
110
105
|
|
111
106
|
def to_hash
|
@@ -217,6 +212,7 @@ module Adaptation
|
|
217
212
|
klass = get_class_object(symbols[1].to_s.capitalize)
|
218
213
|
end
|
219
214
|
xml_object symbols[1], klass
|
215
|
+
validates_associated symbols[1]
|
220
216
|
end
|
221
217
|
when :attribute
|
222
218
|
@attributes = [] if @attributes.nil?
|
@@ -234,7 +230,7 @@ module Adaptation
|
|
234
230
|
@texts << symbols[1]
|
235
231
|
xml_text symbols[1]
|
236
232
|
end
|
237
|
-
end
|
233
|
+
end
|
238
234
|
end
|
239
235
|
|
240
236
|
def self.has_text
|
@@ -281,229 +277,26 @@ module Adaptation
|
|
281
277
|
|
282
278
|
if configuration[:in]
|
283
279
|
xml_object configuration[:in], klass, ROXML::TAG_ARRAY, configuration[:in].to_s
|
280
|
+
validates_associated configuration[:in]
|
284
281
|
else
|
285
282
|
xml_object options[0], klass, ROXML::TAG_ARRAY #, options[0].to_s
|
283
|
+
validates_associated options[0]
|
286
284
|
end
|
287
285
|
end
|
288
286
|
end
|
289
287
|
|
290
288
|
def self.get_class_object(searched_class)
|
291
|
-
#class_object = nil
|
292
|
-
#ObjectSpace.each_object(Class){|aClass|
|
293
|
-
# class_object = aClass if aClass.to_s == searched_class
|
294
|
-
#}
|
295
|
-
#if class_object.nil?
|
296
|
-
# raise Exception.new("Unknown class #{searched_class}.")
|
297
|
-
#else
|
298
|
-
# return class_object
|
299
|
-
#end
|
300
289
|
Object.const_get searched_class
|
301
290
|
end
|
302
291
|
|
303
|
-
def self.get_validations_array
|
304
|
-
validations_arrays = self.has_brothers?
|
305
|
-
if validations_arrays.nil?
|
306
|
-
validations_arrays = [self.to_s]
|
307
|
-
end
|
308
|
-
|
309
|
-
validations_arrays.each do |va|
|
310
|
-
@@validations[va] = [] if @@validations[va].nil?
|
311
|
-
end
|
312
|
-
|
313
|
-
validations_arrays
|
314
|
-
end
|
315
|
-
|
316
|
-
def self.validates_value(*options)
|
317
|
-
get_validations_array.each do |va|
|
318
|
-
|
319
|
-
validation = Validation.new {|message|
|
320
|
-
configuration = {:message => @@default_error_messages[:invalid_value] }
|
321
|
-
brothers_containers = self.has_brothers?
|
322
|
-
if !brothers_containers
|
323
|
-
unless (message.send(options[0]) == options[1].to_s)
|
324
|
-
@@errors << [options[0], message.send(options[0]), configuration[:message]]
|
325
|
-
end
|
326
|
-
else
|
327
|
-
brothers_containers.each do |bc|
|
328
|
-
elements = message.search_objects(bc.split(":")[1].to_sym)
|
329
|
-
bad_element = nil
|
330
|
-
elements.each do |e|
|
331
|
-
if e.send(options[0]) != options[1].to_s
|
332
|
-
bad_element = e
|
333
|
-
break
|
334
|
-
end
|
335
|
-
end
|
336
|
-
unless bad_element.nil?
|
337
|
-
@@errors << [options[0], bad_element.send(options[0]), configuration[:message]]
|
338
|
-
end
|
339
|
-
|
340
|
-
#container = nil
|
341
|
-
#begin
|
342
|
-
# container = message.send(bc.split(":")[1].to_sym)
|
343
|
-
#rescue Exception => e
|
344
|
-
# container = nil
|
345
|
-
# next
|
346
|
-
#end
|
347
|
-
#
|
348
|
-
#if container
|
349
|
-
# bad_son = nil
|
350
|
-
# container.each do |son|
|
351
|
-
# if son.send(options[0]) != options[1].to_s
|
352
|
-
# bad_son = son
|
353
|
-
# break
|
354
|
-
# end
|
355
|
-
# end
|
356
|
-
# unless bad_son.nil?
|
357
|
-
# @@errors << [options[0], bad_son.send(options[0]), configuration[:message]]
|
358
|
-
# end
|
359
|
-
#end
|
360
|
-
end
|
361
|
-
|
362
|
-
end
|
363
|
-
|
364
|
-
#if belongs_to.nil?
|
365
|
-
# unless (message.send(options[0]) == options[1].to_s)
|
366
|
-
# @@errors << [options[0], message.send(options[0]), configuration[:message]]
|
367
|
-
# end
|
368
|
-
#else
|
369
|
-
# found = false
|
370
|
-
# message.send(belongs_to).each do |son|
|
371
|
-
# if son.send(options[0]) == options[1].to_s
|
372
|
-
# found = true
|
373
|
-
# break
|
374
|
-
# end
|
375
|
-
# end
|
376
|
-
# unless found
|
377
|
-
# @@errors << [options[0], message.send(options[0]), configuration[:message]]
|
378
|
-
# end
|
379
|
-
#end
|
380
|
-
}
|
381
|
-
|
382
|
-
validation.vid = "#{self.to_s} validates_value #{options.map{|o| o.to_s + " "}}"
|
383
|
-
validation_already_present = false
|
384
|
-
@@validations[va].each do |v|
|
385
|
-
if v.vid == validation.vid
|
386
|
-
validation_already_present = true
|
387
|
-
break
|
388
|
-
end
|
389
|
-
end
|
390
|
-
if !validation_already_present
|
391
|
-
@@validations[va] << validation
|
392
|
-
end
|
393
|
-
|
394
|
-
end
|
395
|
-
end
|
396
|
-
|
397
|
-
def self.validates_presence_of *options
|
398
|
-
get_validations_array.each do |va|
|
399
|
-
|
400
|
-
validation = Validation.new {|message|
|
401
|
-
configuration = {:message => @@default_error_messages[:must_be_present]}
|
402
|
-
options.each do |o|
|
403
|
-
brothers_containers = self.has_brothers?
|
404
|
-
if !brothers_containers
|
405
|
-
if message.send(o).nil?
|
406
|
-
@@errors << [o, message.send(o), configuration[:message]]
|
407
|
-
end
|
408
|
-
else
|
409
|
-
brothers_containers.each do |bc|
|
410
|
-
elements = message.search_objects(bc.split(":")[1].to_sym)
|
411
|
-
elements.each do |e|
|
412
|
-
if e.send(o).nil?
|
413
|
-
@@errors << [o, e.send(o), configuration[:message]]
|
414
|
-
end
|
415
|
-
end
|
416
|
-
|
417
|
-
#container = nil
|
418
|
-
#begin
|
419
|
-
# container = message.send(bc.split(":")[1].to_sym)
|
420
|
-
#rescue Exception => e
|
421
|
-
# container = nil
|
422
|
-
# next
|
423
|
-
#end
|
424
|
-
#
|
425
|
-
#if container
|
426
|
-
# container.each do |son|
|
427
|
-
# if son.send(o).nil?
|
428
|
-
# @@errors << [o, son.send(o), configuration[:message]]
|
429
|
-
# break
|
430
|
-
# end
|
431
|
-
# end
|
432
|
-
#end
|
433
|
-
|
434
|
-
end
|
435
|
-
|
436
|
-
end
|
437
|
-
end
|
438
|
-
}
|
439
|
-
validation.vid = "#{self.to_s} validates_presence_of #{options.map{|o| o.to_s + " "}}"
|
440
|
-
validation_already_present = false
|
441
|
-
@@validations[va].each do |v|
|
442
|
-
if v.vid == validation.vid
|
443
|
-
validation_already_present = true
|
444
|
-
break
|
445
|
-
end
|
446
|
-
end
|
447
|
-
if !validation_already_present
|
448
|
-
@@validations[va] << validation
|
449
|
-
end
|
450
|
-
end
|
451
|
-
end
|
452
|
-
|
453
|
-
def self.validates_uniqueness_of(*options)
|
454
|
-
end
|
455
|
-
|
456
292
|
def self.to_object xml_message
|
457
293
|
parse xml_message
|
458
294
|
end
|
459
295
|
|
460
296
|
def check
|
461
|
-
|
462
|
-
|
463
|
-
@@validations.each_key do |k|
|
464
|
-
son = k.split(":")[1]
|
465
|
-
unless son.nil?
|
466
|
-
objects = self.search_objects(son.to_sym)
|
467
|
-
unless objects.empty?
|
468
|
-
#if self.has_son? son.to_sym
|
469
|
-
@@validations[k].each do |v|
|
470
|
-
v.call(self)
|
471
|
-
end
|
472
|
-
end
|
473
|
-
end
|
474
|
-
end
|
475
|
-
|
476
|
-
unless @@validations[self.class.to_s].nil?
|
477
|
-
@@validations[self.class.to_s].each do |v|
|
478
|
-
v.call(self)
|
479
|
-
end
|
480
|
-
end
|
481
|
-
|
482
|
-
unless @@errors.empty?
|
483
|
-
error = "Errors found in XML message:\n"
|
484
|
-
@@errors.each do |e|
|
485
|
-
error << " #{e[0]}: #{e[2]}\n"
|
486
|
-
error << " #{e[1]}\n"
|
487
|
-
end
|
488
|
-
puts "#{error}"
|
489
|
-
return false
|
490
|
-
end
|
491
|
-
|
492
|
-
return true
|
493
|
-
end
|
494
|
-
|
495
|
-
def get_errors
|
496
|
-
@@errors
|
497
|
-
end
|
498
|
-
|
499
|
-
def clear_errors
|
500
|
-
@@errors = []
|
297
|
+
valid?
|
501
298
|
end
|
502
299
|
|
503
300
|
end
|
504
301
|
|
505
|
-
class Validation < Proc
|
506
|
-
attr_accessor :vid
|
507
|
-
end
|
508
|
-
|
509
302
|
end
|
@@ -280,15 +280,6 @@ class Test::Unit::TestCase
|
|
280
280
|
|
281
281
|
# this method is repeated many times all over the code!
|
282
282
|
def get_class_object(searched_class) #:nodoc:
|
283
|
-
#class_object = nil
|
284
|
-
#ObjectSpace.each_object(Class){|aClass|
|
285
|
-
# class_object = aClass if aClass.to_s == searched_class
|
286
|
-
#}
|
287
|
-
#if class_object.nil?
|
288
|
-
# raise Exception.new("Unknown class #{searched_class}.")
|
289
|
-
#else
|
290
|
-
# return class_object
|
291
|
-
#end
|
292
283
|
Object.const_get searched_class
|
293
284
|
end
|
294
285
|
|
@@ -0,0 +1,61 @@
|
|
1
|
+
module Validateable
|
2
|
+
|
3
|
+
[:save, :save!, :update_attribute].each{|attr| define_method(attr){}}
|
4
|
+
|
5
|
+
def method_missing(symbol, *params)
|
6
|
+
if(symbol.to_s =~ /(.*)_before_type_cast$/)
|
7
|
+
send($1)
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
def self.append_features(base)
|
12
|
+
super
|
13
|
+
base.send(:include, ActiveRecord::Validations)
|
14
|
+
end
|
15
|
+
|
16
|
+
end
|
17
|
+
|
18
|
+
module ActiveRecord
|
19
|
+
module Validations
|
20
|
+
module ClassMethods
|
21
|
+
|
22
|
+
def validates_value_of(*attr_names)
|
23
|
+
configuration = {
|
24
|
+
:message => 'value doesn\'t exist'
|
25
|
+
}
|
26
|
+
configuration.update(attr_names.pop) if attr_names.last.is_a?(Hash)
|
27
|
+
|
28
|
+
if configuration[:in].nil?
|
29
|
+
validates_each(attr_names, configuration) do |record, attr_name, value|
|
30
|
+
if (attr_names[1].to_s != record.send(attr_names[0].to_sym))
|
31
|
+
record.errors.add(attr_name, configuration[:message])
|
32
|
+
end
|
33
|
+
end
|
34
|
+
else
|
35
|
+
validates_each(attr_names, configuration) do |record, attr_name, value|
|
36
|
+
found = false
|
37
|
+
record.send(configuration[:in]).each do |s|
|
38
|
+
if (attr_names[1].to_s == s.send(attr_names[0].to_sym))
|
39
|
+
found = true
|
40
|
+
break
|
41
|
+
end
|
42
|
+
end
|
43
|
+
record.errors.add(attr_name, configuration[:message]) unless found
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
def validates_as_email(*attr_names)
|
49
|
+
configuration = {
|
50
|
+
:message => 'is an invalid email',
|
51
|
+
:with => RFC822::EmailAddress,
|
52
|
+
:allow_nil => true }
|
53
|
+
configuration.update(attr_names.pop) if attr_names.last.is_a?(Hash)
|
54
|
+
|
55
|
+
validates_format_of attr_names, configuration
|
56
|
+
end
|
57
|
+
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
data/lib/adaptation.rb
CHANGED
metadata
CHANGED
@@ -3,8 +3,8 @@ rubygems_version: 0.9.4
|
|
3
3
|
specification_version: 1
|
4
4
|
name: adaptation
|
5
5
|
version: !ruby/object:Gem::Version
|
6
|
-
version: 0.0.
|
7
|
-
date: 2008-
|
6
|
+
version: 0.0.5
|
7
|
+
date: 2008-07-24 00:00:00 +02:00
|
8
8
|
summary: Framework to facilitate web-application interaction.
|
9
9
|
require_paths:
|
10
10
|
- lib
|
@@ -50,6 +50,7 @@ files:
|
|
50
50
|
- lib/adaptation/test
|
51
51
|
- lib/adaptation/test/fake_fixtures.rb
|
52
52
|
- lib/adaptation/test/test_help.rb
|
53
|
+
- lib/adaptation/validateable.rb
|
53
54
|
- lib/commands.rb
|
54
55
|
- lib/rails_generator
|
55
56
|
- lib/rails_generator/options.rb
|