lafcadio 0.7.1 → 0.7.2

Sign up to get free protection for your applications and to get access to all the features.
data/lib/lafcadio.rb CHANGED
@@ -16,7 +16,7 @@
16
16
  # http://lafcadio.rubyforge.org/tutorial.html.
17
17
 
18
18
  module Lafcadio
19
- Version = "0.7.1"
19
+ Version = "0.7.2"
20
20
 
21
21
  require 'lafcadio/dateTime'
22
22
  require 'lafcadio/depend'
data/lib/lafcadio.rb~ CHANGED
@@ -16,7 +16,7 @@
16
16
  # http://lafcadio.rubyforge.org/tutorial.html.
17
17
 
18
18
  module Lafcadio
19
- Version = "0.7.0"
19
+ Version = "0.7.1"
20
20
 
21
21
  require 'lafcadio/dateTime'
22
22
  require 'lafcadio/depend'
@@ -1,7 +1,7 @@
1
1
  module Lafcadio
2
- # Represents a specific month in time. With the exception of Month.month_names
3
- # (which returns a zero-based array), every usage of the month value assumes
4
- # that 1 equals January and 12 equals December.
2
+ # Represents a specific month in time. With the exception of
3
+ # Month.month_names (which returns a zero-based array), every usage of the
4
+ # month value assumes that 1 equals January and 12 equals December.
5
5
  class Month
6
6
  # Returns an array of the full names of months (in English). Note that
7
7
  # "January" is the 0th element, and "December" is the 11th element.
@@ -0,0 +1,93 @@
1
+ module Lafcadio
2
+ # Represents a specific month in time. With the exception of Month.month_names
3
+ # (which returns a zero-based array), every usage of the month value assumes
4
+ # that 1 equals January and 12 equals December.
5
+ class Month
6
+ # Returns an array of the full names of months (in English). Note that
7
+ # "January" is the 0th element, and "December" is the 11th element.
8
+ def Month.month_names
9
+ [ "January", "February", "March", "April", "May", "June", "July",
10
+ "August", "September", "October", "November", "December" ]
11
+ end
12
+
13
+ include Comparable
14
+
15
+ attr_reader :month, :year
16
+
17
+ # A new month can be set to a specific +month+ and +year+, or you can call
18
+ # Month.new with no arguments to receive the current month.
19
+ def initialize( year = nil, month = nil )
20
+ require 'date'
21
+ if month.nil? || year.nil?
22
+ date = Date.today
23
+ month = date.mon unless month
24
+ year = date.year unless year
25
+ end
26
+ fail "invalid month" if month < 1 || month > 12
27
+ @month = month
28
+ @year = year
29
+ end
30
+
31
+ # Returns a new Month that is +amountToAdd+ months later.
32
+ def +( amountToAdd )
33
+ ( fullYears, remainingMonths ) = amountToAdd.divmod( 12 )
34
+ resultYear = @year + fullYears
35
+ resultMonth = @month + remainingMonths
36
+ if resultMonth > 12
37
+ resultMonth -= 12
38
+ resultYear += 1
39
+ end
40
+ Month.new( resultYear, resultMonth )
41
+ end
42
+
43
+ # Returns a new Month that is +amountToSubtract+ months earlier.
44
+ def -(amountToSubtract)
45
+ self + (-amountToSubtract)
46
+ end
47
+
48
+ # Compare this Month to another Month.
49
+ def <=>(anOther)
50
+ if @year == anOther.year
51
+ @month <=> anOther.month
52
+ else
53
+ @year <=> anOther.year
54
+ end
55
+ end
56
+
57
+ # Returns the last Date of the month.
58
+ def end_date
59
+ self.next.start_date - 1
60
+ end
61
+
62
+ # Is this Month equal to +anOther+? +anOther+ must be another Month of the
63
+ # same value.
64
+ def eql?(anOther)
65
+ self == anOther
66
+ end
67
+
68
+ # Calculate a hash value for this Month.
69
+ def hash
70
+ "#{@year}#{@month}".to_i
71
+ end
72
+
73
+ # Returns the next Month.
74
+ def next
75
+ self + 1
76
+ end
77
+
78
+ # Returns the previous Month.
79
+ def prev
80
+ self - 1
81
+ end
82
+
83
+ # Returns the first Date of the month.
84
+ def start_date
85
+ Date.new( @year, @month, 1 )
86
+ end
87
+
88
+ # Returns a string of the format "January 2001".
89
+ def to_s
90
+ Month.month_names[@month-1][0..2] + " " + @year.to_s
91
+ end
92
+ end
93
+ end
@@ -258,11 +258,15 @@ module Lafcadio
258
258
 
259
259
  include DomainComparable
260
260
 
261
+ def self.[]( pk_id ); get( pk_id ); end
262
+
261
263
  def self.abstract_subclasses #:nodoc:
262
264
  require 'lafcadio/domain'
263
265
  [ MapObject ]
264
266
  end
265
-
267
+
268
+ def self.all; ObjectStore.get_object_store.get_all( self ); end
269
+
266
270
  # Returns an array of all fields defined for this class and all concrete
267
271
  # superclasses.
268
272
  def self.all_fields
@@ -282,13 +286,21 @@ module Lafcadio
282
286
  class_fields
283
287
  end
284
288
 
285
- def self.create_field( field_class, name, att_hash )
289
+ def self.create_field( field_class, *args )
286
290
  class_fields = @@class_fields[self]
287
291
  if class_fields.nil?
288
292
  class_fields = [ @@pk_fields[self] ]
289
293
  @@class_fields[self] = class_fields
290
294
  end
291
- att_hash['name'] = name
295
+ if field_class == LinkField
296
+ att_hash = args.last.is_a?( Hash ) ? args.last : {}
297
+ att_hash['linked_type'] = args.first
298
+ att_hash['name'] = args[1] if args[1] and !args[1].is_a? Hash
299
+ else
300
+ att_hash = args[1] || {}
301
+ name = args.first
302
+ att_hash['name'] = name == String ? name : name.to_s
303
+ end
292
304
  field = field_class.instantiate_with_parameters( self, att_hash )
293
305
  att_hash.each { |field_name, value|
294
306
  setter = field_name + '='
@@ -312,6 +324,8 @@ module Lafcadio
312
324
  }
313
325
  dependent_classes
314
326
  end
327
+
328
+ def self.first; all.first; end
315
329
 
316
330
  def self.get_class_field(fieldName) #:nodoc:
317
331
  field = nil
@@ -330,6 +344,8 @@ module Lafcadio
330
344
  def self.get_class_fields
331
345
  if self.methods( false ).include?( 'get_class_fields' )
332
346
  [ @@pk_fields[ self ] ]
347
+ elsif abstract_subclasses.include?( self )
348
+ []
333
349
  else
334
350
  xmlParser = try_load_xml_parser
335
351
  if xmlParser
@@ -380,6 +396,12 @@ module Lafcadio
380
396
  end
381
397
  end
382
398
 
399
+ def self.get_link_field( linked_domain_class ) # :nodoc:
400
+ class_fields.find { |field|
401
+ field.is_a? LinkField and field.linked_type == linked_domain_class
402
+ }
403
+ end
404
+
383
405
  def self.inherited(subclass) #:nodoc:
384
406
  @@subclassHash[subclass] = true
385
407
  end
@@ -392,12 +414,29 @@ module Lafcadio
392
414
  (self != DomainObject && abstract_subclasses.index(self).nil?)
393
415
  end
394
416
 
417
+ def self.last; all.last; end
418
+
395
419
  def self.method_missing( methodId, *args ) #:nodoc:
396
420
  method_name = methodId.id2name
397
- maybe_field_class_name = method_name.underscore_to_camel_case + 'Field'
398
- field_class = Lafcadio.const_get( maybe_field_class_name )
399
- create_field( field_class, args[0], args[1] || {} )
421
+ if method_name == 'get'
422
+ if block_given?
423
+ query = Query.infer( self ) { |dobj| yield( dobj ) }
424
+ ObjectStore.get_object_store.get_subset( query )
425
+ else
426
+ ObjectStore.get_object_store.get( self, *args )
427
+ end
428
+ else
429
+ maybe_field_class_name = method_name.underscore_to_camel_case + 'Field'
430
+ begin
431
+ field_class = Lafcadio.const_get( maybe_field_class_name )
432
+ create_field( field_class, *args )
433
+ rescue NameError
434
+ super
435
+ end
436
+ end
400
437
  end
438
+
439
+ def self.only; all.only; end
401
440
 
402
441
  def self.domain_class #:nodoc:
403
442
  self
@@ -411,10 +450,9 @@ module Lafcadio
411
450
  require "#{ domainDir }#{ fileName }"
412
451
  end
413
452
  }
414
- if (domainFilesStr = LafcadioConfig.new['domainFiles'])
415
- domainFilesStr.split(',').each { |domainFile|
416
- require domainFile
417
- }
453
+ if (domainFiles = LafcadioConfig.new['domainFiles'])
454
+ domainFiles = domainFiles.split( ',' ) if domainFiles.is_a? String
455
+ domainFiles.each { |domainFile| require domainFile }
418
456
  end
419
457
  end
420
458
 
@@ -466,9 +504,7 @@ module Lafcadio
466
504
  if (!xmlParser.nil? && table_name = xmlParser.table_name)
467
505
  table_name
468
506
  else
469
- table_name = self.basename
470
- table_name[0] = table_name[0..0].downcase
471
- English.plural table_name
507
+ English.plural( self.basename.camel_case_to_underscore )
472
508
  end
473
509
  end
474
510
  end
@@ -506,6 +542,15 @@ module Lafcadio
506
542
  # If you're creating mock objects for unit tests, you can explicitly set
507
543
  # the +pk_id+ to represent objects that already exist in the database.
508
544
  def initialize(fieldHash)
545
+ if fieldHash.respond_to? :keys
546
+ fieldHash.keys.each { |key|
547
+ begin
548
+ self.class.get_field( key )
549
+ rescue MissingError
550
+ raise ArgumentError, "Invalid field name #{ key }"
551
+ end
552
+ }
553
+ end
509
554
  @fieldHash = fieldHash
510
555
  @error_messages = []
511
556
  @fields = {}
@@ -568,12 +613,15 @@ module Lafcadio
568
613
  set_field( field, args.first )
569
614
  elsif ( field = get_getter_field( methId ) )
570
615
  get_field( field )
571
- elsif ( methId.to_s =~ /^get_/ and
572
- ObjectStore.get_object_store.respond_to?( methId ) )
573
- args = [ self ].concat( args )
574
- ObjectStore.get_object_store.send( methId, *args )
575
616
  else
576
- super( methId, *args )
617
+ new_symbol = ( 'get_' + methId.id2name ).to_sym
618
+ object_store = ObjectStore.get_object_store
619
+ if object_store.respond_to? new_symbol
620
+ args = [ self ].concat args
621
+ object_store.send( 'get_' + methId.id2name, *args )
622
+ else
623
+ super( methId, *args )
624
+ end
577
625
  end
578
626
  end
579
627
 
@@ -611,9 +659,11 @@ module Lafcadio
611
659
  end
612
660
 
613
661
  def verify
614
- self.class.get_class_fields.each { |field|
615
- field.verify( self.send( field.name ), self.pk_id )
616
- }
662
+ if ObjectStore.get_object_store.mock?
663
+ self.class.get_class_fields.each { |field|
664
+ field.verify( self.send( field.name ), self.pk_id )
665
+ }
666
+ end
617
667
  end
618
668
  end
619
669
 
@@ -262,7 +262,7 @@ module Lafcadio
262
262
  require 'lafcadio/domain'
263
263
  [ MapObject ]
264
264
  end
265
-
265
+
266
266
  # Returns an array of all fields defined for this class and all concrete
267
267
  # superclasses.
268
268
  def self.all_fields
@@ -282,13 +282,21 @@ module Lafcadio
282
282
  class_fields
283
283
  end
284
284
 
285
- def self.create_field( field_class, name, att_hash )
285
+ def self.create_field( field_class, *args )
286
286
  class_fields = @@class_fields[self]
287
287
  if class_fields.nil?
288
288
  class_fields = [ @@pk_fields[self] ]
289
289
  @@class_fields[self] = class_fields
290
290
  end
291
- att_hash['name'] = name
291
+ if field_class == LinkField
292
+ att_hash = args.last.is_a?( Hash ) ? args.last : {}
293
+ att_hash['linked_type'] = args.first
294
+ att_hash['name'] = args[1] if args[1] and !args[1].is_a? Hash
295
+ else
296
+ att_hash = args[1] || {}
297
+ name = args.first
298
+ att_hash['name'] = name == String ? name : name.to_s
299
+ end
292
300
  field = field_class.instantiate_with_parameters( self, att_hash )
293
301
  att_hash.each { |field_name, value|
294
302
  setter = field_name + '='
@@ -330,6 +338,8 @@ module Lafcadio
330
338
  def self.get_class_fields
331
339
  if self.methods( false ).include?( 'get_class_fields' )
332
340
  [ @@pk_fields[ self ] ]
341
+ elsif abstract_subclasses.include?( self )
342
+ []
333
343
  else
334
344
  xmlParser = try_load_xml_parser
335
345
  if xmlParser
@@ -380,6 +390,12 @@ module Lafcadio
380
390
  end
381
391
  end
382
392
 
393
+ def self.get_link_field( linked_domain_class ) # :nodoc:
394
+ class_fields.find { |field|
395
+ field.is_a? LinkField and field.linked_type == linked_domain_class
396
+ }
397
+ end
398
+
383
399
  def self.inherited(subclass) #:nodoc:
384
400
  @@subclassHash[subclass] = true
385
401
  end
@@ -394,10 +410,22 @@ module Lafcadio
394
410
 
395
411
  def self.method_missing( methodId, *args ) #:nodoc:
396
412
  method_name = methodId.id2name
397
- maybe_field_class_name = ( method_name.gsub( /^(.)/ ) { $&.upcase } ) +
398
- 'Field'
399
- field_class = Lafcadio.const_get( maybe_field_class_name )
400
- create_field( field_class, args[0], args[1] || {} )
413
+ if method_name == 'get'
414
+ if block_given?
415
+ query = Query.infer( self ) { |dobj| yield( dobj ) }
416
+ ObjectStore.get_object_store.get_subset( query )
417
+ else
418
+ ObjectStore.get_object_store.get( self, *args )
419
+ end
420
+ else
421
+ maybe_field_class_name = method_name.underscore_to_camel_case + 'Field'
422
+ begin
423
+ field_class = Lafcadio.const_get( maybe_field_class_name )
424
+ create_field( field_class, *args )
425
+ rescue NameError
426
+ super
427
+ end
428
+ end
401
429
  end
402
430
 
403
431
  def self.domain_class #:nodoc:
@@ -412,10 +440,9 @@ module Lafcadio
412
440
  require "#{ domainDir }#{ fileName }"
413
441
  end
414
442
  }
415
- if (domainFilesStr = LafcadioConfig.new['domainFiles'])
416
- domainFilesStr.split(',').each { |domainFile|
417
- require domainFile
418
- }
443
+ if (domainFiles = LafcadioConfig.new['domainFiles'])
444
+ domainFiles = domainFiles.split( ',' ) if domainFiles.is_a? String
445
+ domainFiles.each { |domainFile| require domainFile }
419
446
  end
420
447
  end
421
448
 
@@ -467,9 +494,7 @@ module Lafcadio
467
494
  if (!xmlParser.nil? && table_name = xmlParser.table_name)
468
495
  table_name
469
496
  else
470
- table_name = self.basename
471
- table_name[0] = table_name[0..0].downcase
472
- English.plural table_name
497
+ English.plural( self.basename.camel_case_to_underscore )
473
498
  end
474
499
  end
475
500
  end
@@ -507,6 +532,15 @@ module Lafcadio
507
532
  # If you're creating mock objects for unit tests, you can explicitly set
508
533
  # the +pk_id+ to represent objects that already exist in the database.
509
534
  def initialize(fieldHash)
535
+ if fieldHash.respond_to? :keys
536
+ fieldHash.keys.each { |key|
537
+ begin
538
+ self.class.get_field( key )
539
+ rescue MissingError
540
+ raise ArgumentError, "Invalid field name #{ key }"
541
+ end
542
+ }
543
+ end
510
544
  @fieldHash = fieldHash
511
545
  @error_messages = []
512
546
  @fields = {}
@@ -612,9 +646,11 @@ module Lafcadio
612
646
  end
613
647
 
614
648
  def verify
615
- self.class.get_class_fields.each { |field|
616
- field.verify( self.send( field.name ), self.pk_id )
617
- }
649
+ if ObjectStore.get_object_store.mock?
650
+ self.class.get_class_fields.each { |field|
651
+ field.verify( self.send( field.name ), self.pk_id )
652
+ }
653
+ end
618
654
  end
619
655
  end
620
656
 
data/lib/lafcadio/mock.rb CHANGED
@@ -40,12 +40,14 @@ module Lafcadio
40
40
  objects << dbObj
41
41
  end
42
42
  }
43
- if (range = query.limit)
44
- objects = objects[0..(range.last - range.first)]
45
- end
46
43
  if ( order_by = query.order_by )
47
44
  objects = objects.sort_by { |dobj| dobj.send( order_by ) }
48
45
  objects.reverse! if query.order_by_order == Query::DESC
46
+ else
47
+ objects = objects.sort_by { |dobj| dobj.pk_id }
48
+ end
49
+ if (range = query.limit)
50
+ objects = objects[0..(range.last - range.first)]
49
51
  end
50
52
  objects
51
53
  end
@@ -94,5 +96,9 @@ module Lafcadio
94
96
  def initialize # :nodoc:
95
97
  super( MockDbBridge.new )
96
98
  end
99
+
100
+ def mock? # :nodoc:
101
+ true
102
+ end
97
103
  end
98
104
  end
@@ -40,13 +40,13 @@ module Lafcadio
40
40
  objects << dbObj
41
41
  end
42
42
  }
43
- if (range = query.limit)
44
- objects = objects[0..(range.last - range.first)]
45
- end
46
43
  if ( order_by = query.order_by )
47
44
  objects = objects.sort_by { |dobj| dobj.send( order_by ) }
48
45
  objects.reverse! if query.order_by_order == Query::DESC
49
46
  end
47
+ if (range = query.limit)
48
+ objects = objects[0..(range.last - range.first)]
49
+ end
50
50
  objects
51
51
  end
52
52
 
@@ -94,5 +94,9 @@ module Lafcadio
94
94
  def initialize # :nodoc:
95
95
  super( MockDbBridge.new )
96
96
  end
97
+
98
+ def mock? # :nodoc:
99
+ true
100
+ end
97
101
  end
98
102
  end
@@ -390,9 +390,16 @@ module Lafcadio
390
390
 
391
391
  # A LinkField is used to link from one domain class to another.
392
392
  class LinkField < ObjectField
393
+ def self.auto_name( linked_type )
394
+ linked_type.name =~ /::/
395
+ ( $' || linked_type.name ).camel_case_to_underscore
396
+ end
397
+
393
398
  def self.instantiate_with_parameters( domain_class, parameters ) #:nodoc:
399
+ linked_type = parameters['linked_type']
394
400
  instance = self.new(
395
- domain_class, parameters['linked_type'], parameters['name'],
401
+ domain_class, linked_type,
402
+ parameters['name'] || auto_name( linked_type ),
396
403
  parameters['delete_cascade']
397
404
  )
398
405
  if parameters['db_field_name']
@@ -421,11 +428,7 @@ module Lafcadio
421
428
  # as well.
422
429
  def initialize( domain_class, linked_type, name = nil,
423
430
  delete_cascade = false )
424
- unless name
425
- linked_type.name =~ /::/
426
- name = $' || linked_type.name
427
- name = name.camel_case_to_underscore
428
- end
431
+ name = self.class.auto_name( linked_type ) unless name
429
432
  super( domain_class, name )
430
433
  ( @linked_type, @delete_cascade ) = linked_type, delete_cascade
431
434
  end
@@ -424,7 +424,7 @@ module Lafcadio
424
424
  unless name
425
425
  linked_type.name =~ /::/
426
426
  name = $' || linked_type.name
427
- name = name.decapitalize
427
+ name = name.camel_case_to_underscore
428
428
  end
429
429
  super( domain_class, name )
430
430
  ( @linked_type, @delete_cascade ) = linked_type, delete_cascade
@@ -450,7 +450,9 @@ module Lafcadio
450
450
  domain_class = DomainObject.get_domain_class_from_string(
451
451
  domain_class_name
452
452
  )
453
- fieldName = get_field_name( searchTerm ) unless fieldName
453
+ unless fieldName
454
+ fieldName = domain_class.get_link_field( searchTerm.domain_class ).name
455
+ end
454
456
  get_subset( Query::Equals.new( fieldName, searchTerm, domain_class ) )
455
457
  end
456
458
 
@@ -522,6 +524,10 @@ module Lafcadio
522
524
  dispatch = MethodDispatch.new( methodId, proc, *args )
523
525
  self.send( dispatch.symbol, *dispatch.args )
524
526
  end
527
+
528
+ def mock? #:nodoc:
529
+ false
530
+ end
525
531
 
526
532
  def respond_to?( symbol, include_private = false )
527
533
  begin
@@ -443,7 +443,7 @@ module Lafcadio
443
443
  def get_db_bridge; @dbBridge; end
444
444
 
445
445
  def get_field_name( domain_object )
446
- domain_object.domain_class.basename.decapitalize
446
+ domain_object.domain_class.basename.camel_case_to_underscore
447
447
  end
448
448
 
449
449
  def get_filtered(domain_class_name, searchTerm, fieldName = nil) #:nodoc:
@@ -522,6 +522,10 @@ module Lafcadio
522
522
  dispatch = MethodDispatch.new( methodId, proc, *args )
523
523
  self.send( dispatch.symbol, *dispatch.args )
524
524
  end
525
+
526
+ def mock? #:nodoc:
527
+ false
528
+ end
525
529
 
526
530
  def respond_to?( symbol, include_private = false )
527
531
  begin
@@ -298,17 +298,19 @@ module Lafcadio
298
298
  AND = 1
299
299
  OR = 2
300
300
 
301
- def initialize(*conditions)
302
- if( [ AND, OR ].index(conditions.last) )
303
- @compound_type = conditions.last
304
- conditions.pop
301
+ def initialize( *args )
302
+ if( [ AND, OR ].index( args.last) )
303
+ @compound_type = args.last
304
+ args.pop
305
305
  else
306
306
  @compound_type = AND
307
307
  end
308
- @conditions = conditions
309
- @domain_class = conditions[0].domain_class
308
+ @conditions = args.map { |arg|
309
+ arg.respond_to?( :to_condition ) ? arg.to_condition : arg
310
+ }
311
+ @domain_class = @conditions[0].domain_class
310
312
  end
311
-
313
+
312
314
  def implied_by?( other_condition )
313
315
  @compound_type == OR && @conditions.any? { |cond|
314
316
  cond.implies?( other_condition )
@@ -147,7 +147,8 @@ module Lafcadio
147
147
 
148
148
  def order_clause
149
149
  if @order_by
150
- clause = "order by #{ @order_by } "
150
+ order_by_field = @domain_class.get_field( @order_by )
151
+ clause = "order by #{ order_by_field.db_field_name } "
151
152
  clause += @order_by_order == ASC ? 'asc' : 'desc'
152
153
  clause
153
154
  end
@@ -234,21 +235,7 @@ module Lafcadio
234
235
  other_cond.is_a?( Condition ) and other_cond.to_sql == to_sql
235
236
  end
236
237
 
237
- def get_field
238
- a_domain_class = @domain_class
239
- field = nil
240
- while ( a_domain_class < DomainObject ) && !field
241
- field = a_domain_class.get_class_field( @fieldName )
242
- a_domain_class = a_domain_class.superclass
243
- end
244
- if field
245
- field
246
- else
247
- errStr = "Couldn't find field \"#{ @fieldName }\" in " +
248
- "#{ @domain_class } domain class"
249
- raise( MissingError, errStr, caller )
250
- end
251
- end
238
+ def get_field; @domain_class.get_field( @fieldName ); end
252
239
 
253
240
  def query; Query.new( @domain_class, self ); end
254
241
 
data/lib/lafcadio/test.rb CHANGED
@@ -13,7 +13,12 @@ class LafcadioTestCase < Test::Unit::TestCase
13
13
  context.flush
14
14
  @mockObjectStore = MockObjectStore.new
15
15
  ObjectStore.set_object_store @mockObjectStore
16
- LafcadioConfig.set_filename 'lafcadio/test/testConfig.dat'
16
+ LafcadioConfig.set_values(
17
+ 'classDefinitionDir' => '../test/testData', 'dbhost' => 'localhost',
18
+ 'dbname' => 'test', 'dbpassword' => 'password', 'dbuser' => 'test',
19
+ 'domainFiles' => %w( ../test/mock/domain ),
20
+ 'logdir' => '../test/testOutput/', 'logSql' => 'n'
21
+ )
17
22
  end
18
23
 
19
24
  def default_test; end
@@ -0,0 +1,20 @@
1
+ require 'lafcadio/depend'
2
+ require 'lafcadio/mock'
3
+ require 'lafcadio/util'
4
+ require 'test/unit'
5
+
6
+ # A test case that sets up a number of mock services. In writing an application
7
+ # that uses Lafcadio you may find it convenient to inherit from this class.
8
+ class LafcadioTestCase < Test::Unit::TestCase
9
+ include Lafcadio
10
+
11
+ def setup
12
+ context = Context.instance
13
+ context.flush
14
+ @mockObjectStore = MockObjectStore.new
15
+ ObjectStore.set_object_store @mockObjectStore
16
+ LafcadioConfig.set_filename 'lafcadio/test/testConfig.dat'
17
+ end
18
+
19
+ def default_test; end
20
+ end
data/lib/lafcadio/util.rb CHANGED
@@ -22,28 +22,28 @@ module Lafcadio
22
22
  include Singleton
23
23
 
24
24
  def initialize
25
- @resources = {}
25
+ flush
26
26
  @init_procs = {}
27
27
  end
28
28
 
29
- def create_instance( service_class ) #:nodoc:
29
+ def create_instance( service_class, *init_args ) #:nodoc:
30
30
  if ( proc = @init_procs[service_class] )
31
- proc.call
31
+ proc.call( *init_args )
32
32
  else
33
- service_class.new
33
+ service_class.new( *init_args )
34
34
  end
35
35
  end
36
36
 
37
37
  # Flushes all cached ContextualServices.
38
38
  def flush
39
- @resources = {}
39
+ @resources_by_class = Hash.new { |hash, key| hash[key] = {} }
40
40
  end
41
41
 
42
- def get_resource( service_class ) #:nodoc:
43
- resource = @resources[service_class]
42
+ def get_resource( service_class, *init_args ) #:nodoc:
43
+ resource = @resources_by_class[service_class][init_args]
44
44
  unless resource
45
- resource = create_instance( service_class )
46
- set_resource service_class, resource
45
+ resource = create_instance( service_class, *init_args )
46
+ set_resource( service_class, resource, *init_args )
47
47
  end
48
48
  resource
49
49
  end
@@ -52,8 +52,8 @@ module Lafcadio
52
52
  @init_procs[service_class] = proc
53
53
  end
54
54
 
55
- def set_resource(service_class, resource) #:nodoc:
56
- @resources[service_class] = resource
55
+ def set_resource( service_class, resource, *init_args ) #:nodoc:
56
+ @resources_by_class[service_class][init_args] = resource
57
57
  end
58
58
  end
59
59
 
@@ -65,15 +65,17 @@ module Lafcadio
65
65
  # For example: ObjectStore.getObjectStore
66
66
  class ContextualService
67
67
  def self.flush; Context.instance.set_resource( self, nil ); end
68
-
69
- def self.method_missing( methodId, *args )
70
- methodName = methodId.id2name
71
- if methodName =~ /^get_(.*)/ || methodName =~ /^set_(.*)/
72
- if methodName =~ /^get_(.*)/
73
- Context.instance.get_resource( self )
74
- else
75
- Context.instance.set_resource( self, *args )
76
- end
68
+
69
+ def self.method_missing( symbol, *args )
70
+ method_name = symbol.id2name
71
+ target = nil
72
+ if method_name =~ /^get_(.*)/
73
+ target = :get_resource if $1.underscore_to_camel_case == basename
74
+ elsif method_name =~ /^set_(.*)/
75
+ target = :set_resource if $1.underscore_to_camel_case == basename
76
+ end
77
+ if target
78
+ Context.instance.send( target, self, *args )
77
79
  else
78
80
  super
79
81
  end
@@ -65,15 +65,17 @@ module Lafcadio
65
65
  # For example: ObjectStore.getObjectStore
66
66
  class ContextualService
67
67
  def self.flush; Context.instance.set_resource( self, nil ); end
68
-
69
- def self.method_missing( methodId, *args )
70
- methodName = methodId.id2name
71
- if methodName =~ /^get_(.*)/ || methodName =~ /^set_(.*)/
72
- if methodName =~ /^get_(.*)/
73
- Context.instance.get_resource( self )
74
- else
75
- Context.instance.set_resource( self, *args )
76
- end
68
+
69
+ def self.method_missing( symbol, *args )
70
+ method_name = symbol.id2name
71
+ target = nil
72
+ if method_name =~ /^get_(.*)/
73
+ target = :get_resource if $1.underscore_to_camel_case == basename
74
+ elsif method_name =~ /^set_(.*)/
75
+ target = :set_resource if $1.underscore_to_camel_case == basename
76
+ end
77
+ if target
78
+ Context.instance.send( target, self, *args )
77
79
  else
78
80
  super
79
81
  end
metadata CHANGED
@@ -3,8 +3,8 @@ rubygems_version: 0.8.1
3
3
  specification_version: 1
4
4
  name: lafcadio
5
5
  version: !ruby/object:Gem::Version
6
- version: 0.7.1
7
- date: 2005-01-27
6
+ version: 0.7.2
7
+ date: 2005-02-26
8
8
  summary: Lafcadio is an object-relational mapping layer
9
9
  require_paths:
10
10
  - lib
@@ -34,6 +34,7 @@ files:
34
34
  - lib/lafcadio.rb
35
35
  - lib/lafcadio.rb~
36
36
  - lib/lafcadio/dateTime.rb
37
+ - lib/lafcadio/dateTime.rb~
37
38
  - lib/lafcadio/depend.rb
38
39
  - lib/lafcadio/domain.rb
39
40
  - lib/lafcadio/domain.rb~
@@ -49,6 +50,7 @@ files:
49
50
  - lib/lafcadio/schema.rb
50
51
  - lib/lafcadio/test
51
52
  - lib/lafcadio/test.rb
53
+ - lib/lafcadio/test.rb~
52
54
  - lib/lafcadio/util.rb
53
55
  - lib/lafcadio/util.rb~
54
56
  - lib/lafcadio/test/testconfig.dat