adaptation 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -37,7 +37,6 @@ module Adaptation
37
37
  # end
38
38
  #
39
39
  # class Contact < Adaptation::Message
40
- # belongs_to :contacts
41
40
  # has_one :text, :name
42
41
  # end
43
42
  #
@@ -45,9 +44,9 @@ module Adaptation
45
44
  #
46
45
  # <agenda type="...">
47
46
  # <owner>...</owner>
48
- # <contacts>
49
- # <contact>...</contact>
50
- # </contacts>
47
+ # <contact>...</contact>
48
+ # <contact>...</contact>
49
+ # ...
51
50
  # </agenda>
52
51
  #
53
52
  # while the _Contact_ class would map:
@@ -102,16 +101,12 @@ module Adaptation
102
101
  :must_be_present => "Must be present"
103
102
  }
104
103
  @@errors = []
105
-
104
+ @@classes_with_brothers = []
105
+ cattr_reader :classes_with_brothers
106
+ cattr_reader :validations
107
+ cattr_reader :objects
108
+
106
109
  include ROXML
107
-
108
- def self.belongs_to symbol
109
- @belongs_to = symbol
110
- end
111
-
112
- def self.parent
113
- @belongs_to
114
- end
115
110
 
116
111
  def to_hash
117
112
  s = self.to_yaml
@@ -140,18 +135,72 @@ module Adaptation
140
135
 
141
136
  found
142
137
  end
138
+
139
+ def search_paths(symbol)
140
+ get_paths_from_hash self.to_hash, symbol
141
+ end
142
+
143
+ def search_objects(symbol)
144
+ objects = []
145
+ paths = search_paths(symbol)
146
+ paths.each do |path|
147
+ objects << get_object(path)
148
+ end
149
+ objects.flatten
150
+ end
151
+
152
+ def get_paths_from_hash hash, symbol, root_path = ""
153
+ paths = []
154
+ hash.each_pair do |k,v|
155
+ if (k.to_sym == symbol)
156
+ paths << "#{root_path}.#{k}" unless paths.include?("#{root_path}.#{k}")
157
+ end
158
+ end
159
+
160
+ hash.each_pair do |k,v|
161
+ if v.is_a?(Hash)
162
+ son_paths = get_paths_from_hash(v, symbol, k)
163
+ son_paths.each do |sp|
164
+ paths << "#{root_path}.#{sp}" unless paths.include?("#{root_path}.#{sp}")
165
+ end
166
+ elsif v.is_a?(Array)
167
+ v.each do |h|
168
+ son_paths = get_paths_from_hash(h, symbol, k)
169
+ son_paths.each do |sp|
170
+ paths << "#{root_path}.#{sp}" unless paths.include?("#{root_path}.#{sp}")
171
+ end
172
+ end
173
+ end
174
+ end
143
175
 
144
- def self.highest_parent
145
- parent_class = self
146
- while parent_class != nil
147
- begin
148
- parent_class = get_class_object(parent_class.parent.to_s.capitalize)
149
- rescue Exception => e
150
- top_parent_class = parent_class.parent.to_s
151
- parent_class = nil
176
+ paths.map!{|p|
177
+ if p.first == "."
178
+ p[1..p.length]
179
+ else
180
+ p
152
181
  end
182
+ }
183
+
184
+ paths
185
+ end
186
+
187
+ def get_object path
188
+ subpaths = path.split(".")
189
+ objects = [self]
190
+ subpaths.each do |s|
191
+ array = []
192
+ objects.each do |o|
193
+ if o.is_a?(Array)
194
+ o.each do |os|
195
+ array << os.send(s.to_sym)
196
+ end
197
+ else
198
+ array << o.send(s.to_sym)
199
+ end
200
+ end
201
+ objects = array
153
202
  end
154
- top_parent_class
203
+ objects
155
204
  end
156
205
 
157
206
  def self.has_one *symbols
@@ -194,24 +243,46 @@ module Adaptation
194
243
  xml_text :text, nil, ROXML::TEXT_CONTENT
195
244
  end
196
245
  end
246
+
247
+ def self.has_brothers?
248
+ brothers_containers = []
249
+ @@classes_with_brothers.each do |cwb|
250
+ if cwb =~ /^#{self.to_s}:/
251
+ brothers_containers << cwb
252
+ end
253
+ end
254
+ if brothers_containers.empty?
255
+ return nil
256
+ else
257
+ return brothers_containers
258
+ end
259
+ end
197
260
 
198
261
  def self.has_many *options
199
262
  configuration = {}
200
263
  configuration.update(options.pop) if options.last.is_a?(Hash)
264
+
265
+ class_with_container = nil
266
+ if configuration[:in]
267
+ class_with_container = options[0].to_s.capitalize[0..-2] + ":#{configuration[:in].to_s}"
268
+ else
269
+ class_with_container = options[0].to_s.capitalize[0..-2] + ":#{options[0].to_s}"
270
+ end
271
+ unless @@classes_with_brothers.include?(class_with_container)
272
+ @@classes_with_brothers << class_with_container
273
+ end
274
+
201
275
  @has_many = [] if @has_many.nil?
202
276
  unless @has_many.include?(options[0])
203
277
  @has_many << options[0]
204
- begin
205
- klass = get_class_object(options[0].to_s.capitalize[0..-2]) # TODO: singularize seriously
206
- rescue Exception => e
207
- require "#{ADAPTOR_ROOT}/app/messages/_#{options[0].to_s[0..-2]}.rb"
208
- klass = get_class_object(options[0].to_s.capitalize[0..-2])
209
- end
210
-
278
+
279
+ load "#{ADAPTOR_ROOT}/app/messages/_#{options[0].to_s[0..-2]}.rb"
280
+ klass = get_class_object(options[0].to_s.capitalize[0..-2])
281
+
211
282
  if configuration[:in]
212
283
  xml_object configuration[:in], klass, ROXML::TAG_ARRAY, configuration[:in].to_s
213
284
  else
214
- xml_object options[0], klass, ROXML::TAG_ARRAY, options[0].to_s
285
+ xml_object options[0], klass, ROXML::TAG_ARRAY #, options[0].to_s
215
286
  end
216
287
  end
217
288
  end
@@ -227,59 +298,155 @@ module Adaptation
227
298
  return class_object
228
299
  end
229
300
  end
230
-
301
+
231
302
  def self.get_validations_array
232
- validations_array = nil
233
- if self.highest_parent.empty?
234
- validations_array = self.to_s.downcase.to_sym
235
- else
236
- validations_array = self.highest_parent.to_sym
303
+ validations_arrays = self.has_brothers?
304
+ if validations_arrays.nil?
305
+ validations_arrays = [self.to_s]
237
306
  end
238
-
239
- @@validations[validations_array] = [] if @@validations[validations_array].nil?
240
- @@validations[validations_array]
307
+
308
+ validations_arrays.each do |va|
309
+ @@validations[va] = [] if @@validations[va].nil?
310
+ end
311
+
312
+ validations_arrays
241
313
  end
242
314
 
243
315
  def self.validates_value(*options)
244
- belongs_to = @belongs_to
245
- get_validations_array << Proc.new {|message|
246
- configuration = {:message => @@default_error_messages[:invalid_value] }
247
- if belongs_to.nil?
248
- unless (message.send(options[0]) == options[1].to_s)
249
- @@errors << [options[0], message.send(options[0]), configuration[:message]]
250
- end
251
- else
252
- found = false
253
- message.send(belongs_to).each do |son|
254
- if son.send(options[0]) == options[1].to_s
255
- found = true
256
- break
316
+ get_validations_array.each do |va|
317
+
318
+ validation = Validation.new {|message|
319
+ configuration = {:message => @@default_error_messages[:invalid_value] }
320
+ brothers_containers = self.has_brothers?
321
+ if !brothers_containers
322
+ unless (message.send(options[0]) == options[1].to_s)
323
+ @@errors << [options[0], message.send(options[0]), configuration[:message]]
324
+ end
325
+ else
326
+ brothers_containers.each do |bc|
327
+ elements = message.search_objects(bc.split(":")[1].to_sym)
328
+ bad_element = nil
329
+ elements.each do |e|
330
+ if e.send(options[0]) != options[1].to_s
331
+ bad_element = e
332
+ break
333
+ end
334
+ end
335
+ unless bad_element.nil?
336
+ @@errors << [options[0], bad_element.send(options[0]), configuration[:message]]
337
+ end
338
+
339
+ #container = nil
340
+ #begin
341
+ # container = message.send(bc.split(":")[1].to_sym)
342
+ #rescue Exception => e
343
+ # container = nil
344
+ # next
345
+ #end
346
+ #
347
+ #if container
348
+ # bad_son = nil
349
+ # container.each do |son|
350
+ # if son.send(options[0]) != options[1].to_s
351
+ # bad_son = son
352
+ # break
353
+ # end
354
+ # end
355
+ # unless bad_son.nil?
356
+ # @@errors << [options[0], bad_son.send(options[0]), configuration[:message]]
357
+ # end
358
+ #end
257
359
  end
360
+
258
361
  end
259
- unless found
260
- @@errors << [options[0], message.send(options[0]), configuration[:message]]
261
- end
362
+
363
+ #if belongs_to.nil?
364
+ # unless (message.send(options[0]) == options[1].to_s)
365
+ # @@errors << [options[0], message.send(options[0]), configuration[:message]]
366
+ # end
367
+ #else
368
+ # found = false
369
+ # message.send(belongs_to).each do |son|
370
+ # if son.send(options[0]) == options[1].to_s
371
+ # found = true
372
+ # break
373
+ # end
374
+ # end
375
+ # unless found
376
+ # @@errors << [options[0], message.send(options[0]), configuration[:message]]
377
+ # end
378
+ #end
379
+ }
380
+
381
+ validation.vid = "#{self.to_s} validates_value #{options.map{|o| o.to_s + " "}}"
382
+ validation_already_present = false
383
+ @@validations[va].each do |v|
384
+ if v.vid == validation.vid
385
+ validation_already_present = true
386
+ break
387
+ end
262
388
  end
263
- }
389
+ if !validation_already_present
390
+ @@validations[va] << validation
391
+ end
392
+
393
+ end
264
394
  end
265
395
 
266
396
  def self.validates_presence_of *options
267
- belongs_to = @belongs_to
268
- get_validations_array << Proc.new {|message|
269
- configuration = {:message => @@default_error_messages[:must_be_present]}
270
- options.each do |o|
271
- if belongs_to.nil?
272
- if message.send(o).nil?
273
- @@errors << [o, message.send(o), configuration[:message]]
274
- end
275
- else
276
- parent = message.send(belongs_to)
277
- if parent.send(o).nil? # this throws a warning if o = :id (but it works)
278
- @@errors << [o, message.send(o), configuration[:message]]
397
+ get_validations_array.each do |va|
398
+
399
+ validation = Validation.new {|message|
400
+ configuration = {:message => @@default_error_messages[:must_be_present]}
401
+ options.each do |o|
402
+ brothers_containers = self.has_brothers?
403
+ if !brothers_containers
404
+ if message.send(o).nil?
405
+ @@errors << [o, message.send(o), configuration[:message]]
406
+ end
407
+ else
408
+ brothers_containers.each do |bc|
409
+ elements = message.search_objects(bc.split(":")[1].to_sym)
410
+ elements.each do |e|
411
+ if e.send(o).nil?
412
+ @@errors << [o, e.send(o), configuration[:message]]
413
+ end
414
+ end
415
+
416
+ #container = nil
417
+ #begin
418
+ # container = message.send(bc.split(":")[1].to_sym)
419
+ #rescue Exception => e
420
+ # container = nil
421
+ # next
422
+ #end
423
+ #
424
+ #if container
425
+ # container.each do |son|
426
+ # if son.send(o).nil?
427
+ # @@errors << [o, son.send(o), configuration[:message]]
428
+ # break
429
+ # end
430
+ # end
431
+ #end
432
+
433
+ end
434
+
279
435
  end
280
436
  end
437
+ }
438
+ validation.vid = "#{self.to_s} validates_presence_of #{options.map{|o| o.to_s + " "}}"
439
+ validation_already_present = false
440
+ @@validations[va].each do |v|
441
+ if v.vid == validation.vid
442
+ validation_already_present = true
443
+ break
444
+ end
281
445
  end
282
- }
446
+ if !validation_already_present
447
+ @@validations[va] << validation
448
+ end
449
+ end
283
450
  end
284
451
 
285
452
  def self.validates_uniqueness_of(*options)
@@ -291,16 +458,24 @@ module Adaptation
291
458
 
292
459
  def check
293
460
  clear_errors
294
-
461
+
295
462
  @@validations.each_key do |k|
296
- if self.has_son? k.to_sym
297
- @@validations[k.to_sym].each do |v|
298
- v.call(self)
463
+ son = k.split(":")[1]
464
+ unless son.nil?
465
+ objects = self.search_objects(son.to_sym)
466
+ unless objects.empty?
467
+ #if self.has_son? son.to_sym
468
+ @@validations[k].each do |v|
469
+ v.call(self)
470
+ end
299
471
  end
300
472
  end
301
473
  end
302
- @@validations[self.class.to_s.downcase.to_sym].each do |v|
303
- v.call(self)
474
+
475
+ unless @@validations[self.class.to_s].nil?
476
+ @@validations[self.class.to_s].each do |v|
477
+ v.call(self)
478
+ end
304
479
  end
305
480
 
306
481
  unless @@errors.empty?
@@ -309,6 +484,7 @@ module Adaptation
309
484
  error << " #{e[0]}: #{e[2]}\n"
310
485
  error << " #{e[1]}\n"
311
486
  end
487
+ puts "#{error}"
312
488
  return false
313
489
  end
314
490
 
@@ -324,5 +500,9 @@ module Adaptation
324
500
  end
325
501
 
326
502
  end
503
+
504
+ class Validation < Proc
505
+ attr_accessor :vid
506
+ end
327
507
 
328
508
  end
@@ -3,66 +3,70 @@ require 'yaml'
3
3
 
4
4
  module Adaptation
5
5
 
6
- class Mom
6
+ module Mom
7
7
 
8
- def initialize mom_uri
9
- @mom_uri = mom_uri
10
- end
8
+ class Mom
11
9
 
12
- def subscribe drb_uri
13
- unless get_subscribers.include?(drb_uri)
14
- add_subscriber drb_uri
15
- puts "Added new subscriber: #{drb_uri}"
16
- oapdaemon = DRbObject.new(nil, drb_uri)
17
- oapdaemon.subscription_result true
10
+ def initialize mom_uri
11
+ @mom_uri = mom_uri
18
12
  end
19
- end
20
13
 
21
- def publish message, topic
22
- # Tell subscribed hosts to execute their adaptors
23
- puts "-----------------------------------"
24
- puts "Received message in topic: #{topic}"
25
- puts "#{message}"
26
- puts "-----------------------------------"
27
- get_subscribers.each do |uri|
28
- puts "Calling #{uri}"
29
- DRb.start_service
30
- oapdaemon = DRbObject.new(nil, uri)
31
- oapdaemon.call_adaptor message, topic
14
+ def subscribe drb_uri
15
+ unless get_subscribers.include?(drb_uri)
16
+ add_subscriber drb_uri
17
+ puts "Added new subscriber: #{drb_uri}"
18
+ oapdaemon = DRbObject.new(nil, drb_uri)
19
+ oapdaemon.subscription_result true
20
+ end
32
21
  end
33
- end
34
22
 
35
- def start
36
- DRb.start_service(@mom_uri, self)
37
- puts "MOM started. Listening at #{@mom_uri}"
38
- DRb.thread.join # Don't exit just yet
39
- end
23
+ def publish message, topic
24
+ # Tell subscribed hosts to execute their adaptors
25
+ puts "-----------------------------------"
26
+ puts "Received message in topic: #{topic}"
27
+ puts "#{message}"
28
+ puts "-----------------------------------"
29
+ get_subscribers.each do |uri|
30
+ puts "Calling #{uri}"
31
+ DRb.start_service
32
+ oapdaemon = DRbObject.new(nil, uri)
33
+ oapdaemon.process message, topic
34
+ end
35
+ end
40
36
 
41
- def list
42
- puts "MOM subscriptions:"
43
- get_subscribers.each do |s|
44
- puts " #{s}"
37
+ def start
38
+ DRb.start_service(@mom_uri, self)
39
+ puts "MOM started. Listening at #{@mom_uri}"
40
+ DRb.thread.join # Don't exit just yet
45
41
  end
46
- return
47
- end
48
42
 
49
- private
43
+ def list
44
+ puts "MOM subscriptions:"
45
+ get_subscribers.each do |s|
46
+ puts " #{s}"
47
+ end
48
+ return
49
+ end
50
50
 
51
- def add_subscriber drb_uri
52
- subscribers = get_subscribers
53
- subscribers << drb_uri unless subscribers.include?(drb_uri)
54
- sf = File.new('subscribers.yml', 'w')
55
- sf.write(YAML::dump(subscribers))
56
- sf.close
57
- end
51
+ private
58
52
 
59
- def get_subscribers
60
- if File.exists?('subscribers.yml')
61
- subscribers = YAML::load(File.open('subscribers.yml'))
62
- else
63
- subscribers = Array.new
53
+ def add_subscriber drb_uri
54
+ subscribers = get_subscribers
55
+ subscribers << drb_uri unless subscribers.include?(drb_uri)
56
+ sf = File.new('subscribers.yml', 'w')
57
+ sf.write(YAML::dump(subscribers))
58
+ sf.close
64
59
  end
65
- subscribers
60
+
61
+ def get_subscribers
62
+ if File.exists?('subscribers.yml')
63
+ subscribers = YAML::load(File.open('subscribers.yml'))
64
+ else
65
+ subscribers = Array.new
66
+ end
67
+ subscribers
68
+ end
69
+
66
70
  end
67
71
 
68
72
  end
@@ -0,0 +1,74 @@
1
+ require 'drb'
2
+ require 'yaml'
3
+
4
+ module Adaptation
5
+
6
+ module Mom
7
+
8
+ class Mom
9
+
10
+ def initialize mom_uri
11
+ @mom_uri = mom_uri
12
+ end
13
+
14
+ def subscribe drb_uri
15
+ unless get_subscribers.include?(drb_uri)
16
+ add_subscriber drb_uri
17
+ puts "Added new subscriber: #{drb_uri}"
18
+ oapdaemon = DRbObject.new(nil, drb_uri)
19
+ oapdaemon.subscription_result true
20
+ end
21
+ end
22
+
23
+ def publish message, topic
24
+ # Tell subscribed hosts to execute their adaptors
25
+ puts "-----------------------------------"
26
+ puts "Received message in topic: #{topic}"
27
+ puts "#{message}"
28
+ puts "-----------------------------------"
29
+ get_subscribers.each do |uri|
30
+ puts "Calling #{uri}"
31
+ DRb.start_service
32
+ oapdaemon = DRbObject.new(nil, uri)
33
+ oapdaemon.call_adaptor message, topic
34
+ end
35
+ end
36
+
37
+ def start
38
+ DRb.start_service(@mom_uri, self)
39
+ puts "MOM started. Listening at #{@mom_uri}"
40
+ DRb.thread.join # Don't exit just yet
41
+ end
42
+
43
+ def list
44
+ puts "MOM subscriptions:"
45
+ get_subscribers.each do |s|
46
+ puts " #{s}"
47
+ end
48
+ return
49
+ end
50
+
51
+ private
52
+
53
+ def add_subscriber drb_uri
54
+ subscribers = get_subscribers
55
+ subscribers << drb_uri unless subscribers.include?(drb_uri)
56
+ sf = File.new('subscribers.yml', 'w')
57
+ sf.write(YAML::dump(subscribers))
58
+ sf.close
59
+ end
60
+
61
+ def get_subscribers
62
+ if File.exists?('subscribers.yml')
63
+ subscribers = YAML::load(File.open('subscribers.yml'))
64
+ else
65
+ subscribers = Array.new
66
+ end
67
+ subscribers
68
+ end
69
+
70
+ end
71
+
72
+ end
73
+
74
+ end
@@ -0,0 +1,42 @@
1
+ require 'drb'
2
+ require 'yaml'
3
+
4
+ module Adaptation
5
+
6
+ module Mom
7
+
8
+ class druby_subscriber
9
+
10
+ def initialize subscriber_uri, mom_uri, topics
11
+ @subscriber_uri = subscriber_uri
12
+ @mom_uri = mom_uri
13
+ @topics = topics
14
+ end
15
+
16
+ def call_adaptor message, topic
17
+ if ( (@topics.include?(topic)) or (@topics == "all") )
18
+ system("ruby public/dispatch.rb #{message}")
19
+ end
20
+ puts "#{topic} => #{message}"
21
+ end
22
+
23
+ def subscription_result subscribed
24
+ if subscribed
25
+ puts "Subscribed to mom (#{@mom_uri}). Listening at #{@subscriber_uri}"
26
+ end
27
+ end
28
+
29
+ def start
30
+ DRb.start_service(@subscriber_uri, self)
31
+
32
+ mom = DRbObject.new(nil, @mom_uri)
33
+ mom.subscribe @subscriber_uri
34
+
35
+ DRb.thread.join # Don't exit just yet!
36
+ end
37
+
38
+ end
39
+
40
+ end
41
+
42
+ end
@@ -103,7 +103,7 @@ class Test::Unit::TestCase
103
103
  data, message_object = load_message_fixture message_symbol
104
104
  error = build_message error,
105
105
  "? message shouldn't validate",
106
- message_symbol.to_s
106
+ message_symbol.to_s
107
107
  assert_block error do
108
108
  !message_object.check
109
109
  end
@@ -196,7 +196,7 @@ class Test::Unit::TestCase
196
196
  begin
197
197
  connection = ActiveRecord::Base.connection
198
198
  rescue Exception => e
199
- puts "Database error message: #{e}"
199
+ puts "Database says: #{e} (not necessarily bad)"
200
200
  database_exists = false
201
201
  end
202
202
 
@@ -287,7 +287,7 @@ class Test::Unit::TestCase
287
287
 
288
288
  def compare_xml_elements element1, element2 #:nodoc:
289
289
  if element1.has_attributes?
290
- if !elements2.has_attributes?
290
+ if !element2.has_attributes?
291
291
  return false
292
292
  end
293
293
  element1.attributes.to_a.each do |a|
data/lib/adaptation.rb CHANGED
@@ -1,5 +1,6 @@
1
1
  require 'roxml'
2
2
  require 'yaml'
3
+ require 'active_record'
3
4
  require 'adaptation/message'
4
5
  require 'adaptation/adaptor'
5
6
  require 'adaptation/base'