lafcadio 0.5.2 → 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
data/bin/lafcadio_schema CHANGED
@@ -7,15 +7,23 @@ require 'lafcadio/util'
7
7
  require 'getoptlong'
8
8
  include Lafcadio
9
9
 
10
- opts = GetoptLong.new( [ '--config', '-c', GetoptLong::REQUIRED_ARGUMENT ] )
10
+ opts = GetoptLong.new(
11
+ [ '--config', '-c', GetoptLong::REQUIRED_ARGUMENT ],
12
+ [ '--class', '-C', GetoptLong::OPTIONAL_ARGUMENT ]
13
+ )
11
14
  if ( ( optArray = opts.get ) && ARGV.size >= 1 )
12
15
  configFile = optArray[1]
13
16
  LafcadioConfig.set_filename( configFile )
17
+ class_opts = opts.get
14
18
  ARGV.each { |fileName|
15
19
  require "#{ fileName }"
16
- fileName =~ /(\w*)\.rb/
17
- className = $1
18
- domainClass = Class.get_class( className )
20
+ if class_opts
21
+ className = class_opts.last
22
+ else
23
+ fileName =~ /(\w*)\.rb/
24
+ className = $1
25
+ end
26
+ domainClass = Class.by_name( className )
19
27
  if domainClass
20
28
  statement = CreateTableStatement.new( domainClass )
21
29
  puts statement.to_sql
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.5.2"
19
+ Version = "0.6.0"
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.5.1"
19
+ Version = "0.5.2"
20
20
 
21
21
  require 'lafcadio/dateTime'
22
22
  require 'lafcadio/depend'
@@ -1,3 +1,5 @@
1
1
  require 'dbi'
2
+ require 'extensions/all'
3
+ require 'extensions/module'
2
4
  require 'log4r'
3
5
  require 'runit/testcase'
@@ -13,12 +13,12 @@ module Lafcadio
13
13
  def get_class_field( fieldElt )
14
14
  className = fieldElt.attributes['class'].to_s
15
15
  name = fieldElt.attributes['name']
16
- begin
17
- fieldClass = Class.get_class( 'Lafcadio::' + className )
16
+ if className != ''
17
+ fieldClass = Class.by_name( 'Lafcadio::' + className )
18
18
  register_name( name )
19
19
  field = fieldClass.instantiate_from_xml( @domain_class, fieldElt )
20
20
  set_field_attributes( field, fieldElt )
21
- rescue MissingError
21
+ else
22
22
  msg = "Couldn't find field class '#{ className }' for field " +
23
23
  "'#{ name }'"
24
24
  raise( MissingError, msg, caller )
@@ -122,14 +122,14 @@ module Lafcadio
122
122
  module DomainComparable
123
123
  include Comparable
124
124
 
125
- # A DomainObject or DomainObjectProxy is compared by +object_type+ and by
125
+ # A DomainObject or DomainObjectProxy is compared by +domain_class+ and by
126
126
  # +pk_id+.
127
127
  def <=>(anOther)
128
- if anOther.respond_to?( 'object_type' )
129
- if self.object_type == anOther.object_type
128
+ if anOther.respond_to?( 'domain_class' )
129
+ if self.domain_class == anOther.domain_class
130
130
  self.pk_id <=> anOther.pk_id
131
131
  else
132
- self.object_type.name <=> anOther.object_type.name
132
+ self.domain_class.name <=> anOther.domain_class.name
133
133
  end
134
134
  else
135
135
  nil
@@ -243,6 +243,13 @@ module Lafcadio
243
243
  class DomainObject
244
244
  @@subclassHash = {}
245
245
  @@class_fields = {}
246
+ @@pk_fields =
247
+ Hash.new { |hash, key|
248
+ pk_field = PrimaryKeyField.new( key )
249
+ pk_field.db_field_name = @@sql_primary_keys[key]
250
+ hash[key] = pk_field
251
+ }
252
+ @@sql_primary_keys = Hash.new( 'pk_id' )
246
253
 
247
254
  COMMIT_ADD = 1
248
255
  COMMIT_EDIT = 2
@@ -277,7 +284,7 @@ module Lafcadio
277
284
  def self.create_field( field_class, name, att_hash )
278
285
  class_fields = @@class_fields[self]
279
286
  if class_fields.nil?
280
- class_fields = [ PrimaryKeyField.new( self ) ]
287
+ class_fields = [ @@pk_fields[self] ]
281
288
  @@class_fields[self] = class_fields
282
289
  end
283
290
  att_hash['name'] = name
@@ -295,7 +302,8 @@ module Lafcadio
295
302
  if aClass != DomainObjectProxy &&
296
303
  (!DomainObject.abstract_subclasses.index(aClass))
297
304
  aClass.class_fields.each { |field|
298
- if field.class <= LinkField && field.linked_type == self.object_type
305
+ if ( field.is_a?( LinkField ) &&
306
+ field.linked_type == self.domain_class )
299
307
  dependent_classes[aClass] = field
300
308
  end
301
309
  }
@@ -320,7 +328,7 @@ module Lafcadio
320
328
  # them from XML if necessary.
321
329
  def self.get_class_fields
322
330
  if self.methods( false ).include?( 'get_class_fields' )
323
- [ PrimaryKeyField.new( self ) ]
331
+ [ @@pk_fields[ self ] ]
324
332
  else
325
333
  xmlParser = try_load_xml_parser
326
334
  if xmlParser
@@ -334,13 +342,10 @@ module Lafcadio
334
342
  end
335
343
 
336
344
  def self.get_domain_dirs #:nodoc:
337
- config = LafcadioConfig.new
338
- classPath = config['classpath']
339
- domainDirStr = config['domainDirs']
340
- if domainDirStr
341
- domainDirs = domainDirStr.split(',')
345
+ if ( domainDirStr = LafcadioConfig.new['domainDirs'] )
346
+ domainDirStr.split(',')
342
347
  else
343
- domainDirs = [ classPath + 'domain/' ]
348
+ []
344
349
  end
345
350
  end
346
351
 
@@ -360,20 +365,20 @@ module Lafcadio
360
365
  end
361
366
  end
362
367
 
363
- def self.get_object_type_from_string(typeString) #:nodoc:
364
- object_type = nil
368
+ def self.get_domain_class_from_string(typeString) #:nodoc:
369
+ domain_class = nil
365
370
  require_domain_file( typeString )
366
371
  subclasses.each { |subclass|
367
- object_type = subclass if subclass.to_s == typeString
372
+ domain_class = subclass if subclass.to_s == typeString
368
373
  }
369
- if object_type
370
- object_type
374
+ if domain_class
375
+ domain_class
371
376
  else
372
- raise CouldntMatchObjectTypeError,
373
- "couldn't match object_type #{typeString}", caller
377
+ raise CouldntMatchDomainClassError,
378
+ "couldn't match domain_class #{typeString}", caller
374
379
  end
375
380
  end
376
-
381
+
377
382
  def self.inherited(subclass) #:nodoc:
378
383
  @@subclassHash[subclass] = true
379
384
  end
@@ -394,7 +399,7 @@ module Lafcadio
394
399
  create_field( field_class, args[0], args[1] || {} )
395
400
  end
396
401
 
397
- def self.object_type #:nodoc:
402
+ def self.domain_class #:nodoc:
398
403
  self
399
404
  end
400
405
 
@@ -415,15 +420,25 @@ module Lafcadio
415
420
 
416
421
  def self.self_and_concrete_superclasses # :nodoc:
417
422
  classes = [ ]
418
- anObjectType = self
419
- until(anObjectType == DomainObject ||
420
- abstract_subclasses.index(anObjectType) != nil)
421
- classes << anObjectType
422
- anObjectType = anObjectType.superclass
423
+ a_domain_class = self
424
+ until( a_domain_class == DomainObject ||
425
+ abstract_subclasses.index( a_domain_class ) != nil )
426
+ classes << a_domain_class
427
+ a_domain_class = a_domain_class.superclass
423
428
  end
424
429
  classes
425
430
  end
426
431
 
432
+ def self.singleton_method_added( symbol )
433
+ if symbol.id2name == 'sql_primary_key_name' && self < DomainObject
434
+ begin
435
+ get_field( 'pk_id' ).db_field_name = self.send( symbol )
436
+ rescue NameError
437
+ @@sql_primary_keys[self] = self.send( symbol )
438
+ end
439
+ end
440
+ end
441
+
427
442
  # Returns the name of the primary key in the database, retrieving it from
428
443
  # the class definition XML if necessary.
429
444
  def self.sql_primary_key_name( set_sql_primary_key_name = nil )
@@ -451,7 +466,7 @@ module Lafcadio
451
466
  if (!xmlParser.nil? && table_name = xmlParser.table_name)
452
467
  table_name
453
468
  else
454
- table_name = self.bare_name
469
+ table_name = self.basename
455
470
  table_name[0] = table_name[0..0].downcase
456
471
  English.plural table_name
457
472
  end
@@ -461,7 +476,7 @@ module Lafcadio
461
476
  def self.try_load_xml_parser
462
477
  require 'lafcadio/domain'
463
478
  dirName = LafcadioConfig.new['classDefinitionDir']
464
- xmlFileName = self.bare_name + '.xml'
479
+ xmlFileName = self.basename + '.xml'
465
480
  xmlPath = File.join( dirName, xmlFileName )
466
481
  xml = ''
467
482
  begin
@@ -565,8 +580,8 @@ module Lafcadio
565
580
  # Returns the subclass of DomainObject that this instance represents.
566
581
  # Because of the way that proxying works, clients should call this method
567
582
  # instead of Object.class.
568
- def object_type
569
- self.class.object_type
583
+ def domain_class
584
+ self.class.domain_class
570
585
  end
571
586
 
572
587
  # This template method is called before every commit. Subclasses can
@@ -13,12 +13,12 @@ module Lafcadio
13
13
  def get_class_field( fieldElt )
14
14
  className = fieldElt.attributes['class'].to_s
15
15
  name = fieldElt.attributes['name']
16
- begin
17
- fieldClass = Class.get_class( 'Lafcadio::' + className )
16
+ if className != ''
17
+ fieldClass = Class.by_name( 'Lafcadio::' + className )
18
18
  register_name( name )
19
19
  field = fieldClass.instantiate_from_xml( @domain_class, fieldElt )
20
20
  set_field_attributes( field, fieldElt )
21
- rescue MissingError
21
+ else
22
22
  msg = "Couldn't find field class '#{ className }' for field " +
23
23
  "'#{ name }'"
24
24
  raise( MissingError, msg, caller )
@@ -122,14 +122,14 @@ module Lafcadio
122
122
  module DomainComparable
123
123
  include Comparable
124
124
 
125
- # A DomainObject or DomainObjectProxy is compared by +object_type+ and by
125
+ # A DomainObject or DomainObjectProxy is compared by +domain_class+ and by
126
126
  # +pk_id+.
127
127
  def <=>(anOther)
128
- if anOther.respond_to?( 'object_type' )
129
- if self.object_type == anOther.object_type
128
+ if anOther.respond_to?( 'domain_class' )
129
+ if self.domain_class == anOther.domain_class
130
130
  self.pk_id <=> anOther.pk_id
131
131
  else
132
- self.object_type.name <=> anOther.object_type.name
132
+ self.domain_class.name <=> anOther.domain_class.name
133
133
  end
134
134
  else
135
135
  nil
@@ -243,6 +243,13 @@ module Lafcadio
243
243
  class DomainObject
244
244
  @@subclassHash = {}
245
245
  @@class_fields = {}
246
+ @@pk_fields =
247
+ Hash.new { |hash, key|
248
+ pk_field = PrimaryKeyField.new( key )
249
+ pk_field.db_field_name = @@sql_primary_keys[key]
250
+ hash[key] = pk_field
251
+ }
252
+ @@sql_primary_keys = Hash.new( 'pk_id' )
246
253
 
247
254
  COMMIT_ADD = 1
248
255
  COMMIT_EDIT = 2
@@ -277,7 +284,7 @@ module Lafcadio
277
284
  def self.create_field( field_class, name, att_hash )
278
285
  class_fields = @@class_fields[self]
279
286
  if class_fields.nil?
280
- class_fields = [ PrimaryKeyField.new( self ) ]
287
+ class_fields = [ @@pk_fields[self] ]
281
288
  @@class_fields[self] = class_fields
282
289
  end
283
290
  att_hash['name'] = name
@@ -295,7 +302,8 @@ module Lafcadio
295
302
  if aClass != DomainObjectProxy &&
296
303
  (!DomainObject.abstract_subclasses.index(aClass))
297
304
  aClass.class_fields.each { |field|
298
- if field.class <= LinkField && field.linked_type == self.object_type
305
+ if ( field.is_a?( LinkField ) &&
306
+ field.linked_type == self.domain_class )
299
307
  dependent_classes[aClass] = field
300
308
  end
301
309
  }
@@ -320,7 +328,7 @@ module Lafcadio
320
328
  # them from XML if necessary.
321
329
  def self.get_class_fields
322
330
  if self.methods( false ).include?( 'get_class_fields' )
323
- [ PrimaryKeyField.new( self ) ]
331
+ [ @@pk_fields[ self ] ]
324
332
  else
325
333
  xmlParser = try_load_xml_parser
326
334
  if xmlParser
@@ -360,20 +368,20 @@ module Lafcadio
360
368
  end
361
369
  end
362
370
 
363
- def self.get_object_type_from_string(typeString) #:nodoc:
364
- object_type = nil
371
+ def self.get_domain_class_from_string(typeString) #:nodoc:
372
+ domain_class = nil
365
373
  require_domain_file( typeString )
366
374
  subclasses.each { |subclass|
367
- object_type = subclass if subclass.to_s == typeString
375
+ domain_class = subclass if subclass.to_s == typeString
368
376
  }
369
- if object_type
370
- object_type
377
+ if domain_class
378
+ domain_class
371
379
  else
372
- raise CouldntMatchObjectTypeError,
373
- "couldn't match object_type #{typeString}", caller
380
+ raise CouldntMatchDomainClassError,
381
+ "couldn't match domain_class #{typeString}", caller
374
382
  end
375
383
  end
376
-
384
+
377
385
  def self.inherited(subclass) #:nodoc:
378
386
  @@subclassHash[subclass] = true
379
387
  end
@@ -394,7 +402,7 @@ module Lafcadio
394
402
  create_field( field_class, args[0], args[1] || {} )
395
403
  end
396
404
 
397
- def self.object_type #:nodoc:
405
+ def self.domain_class #:nodoc:
398
406
  self
399
407
  end
400
408
 
@@ -415,15 +423,25 @@ module Lafcadio
415
423
 
416
424
  def self.self_and_concrete_superclasses # :nodoc:
417
425
  classes = [ ]
418
- anObjectType = self
419
- until(anObjectType == DomainObject ||
420
- abstract_subclasses.index(anObjectType) != nil)
421
- classes << anObjectType
422
- anObjectType = anObjectType.superclass
426
+ a_domain_class = self
427
+ until( a_domain_class == DomainObject ||
428
+ abstract_subclasses.index( a_domain_class ) != nil )
429
+ classes << a_domain_class
430
+ a_domain_class = a_domain_class.superclass
423
431
  end
424
432
  classes
425
433
  end
426
434
 
435
+ def self.singleton_method_added( symbol )
436
+ if symbol.id2name == 'sql_primary_key_name' && self < DomainObject
437
+ begin
438
+ get_field( 'pk_id' ).db_field_name = self.send( symbol )
439
+ rescue NameError
440
+ @@sql_primary_keys[self] = self.send( symbol )
441
+ end
442
+ end
443
+ end
444
+
427
445
  # Returns the name of the primary key in the database, retrieving it from
428
446
  # the class definition XML if necessary.
429
447
  def self.sql_primary_key_name( set_sql_primary_key_name = nil )
@@ -451,7 +469,7 @@ module Lafcadio
451
469
  if (!xmlParser.nil? && table_name = xmlParser.table_name)
452
470
  table_name
453
471
  else
454
- table_name = self.bare_name
472
+ table_name = self.basename
455
473
  table_name[0] = table_name[0..0].downcase
456
474
  English.plural table_name
457
475
  end
@@ -461,7 +479,7 @@ module Lafcadio
461
479
  def self.try_load_xml_parser
462
480
  require 'lafcadio/domain'
463
481
  dirName = LafcadioConfig.new['classDefinitionDir']
464
- xmlFileName = self.bare_name + '.xml'
482
+ xmlFileName = self.basename + '.xml'
465
483
  xmlPath = File.join( dirName, xmlFileName )
466
484
  xml = ''
467
485
  begin
@@ -553,6 +571,10 @@ module Lafcadio
553
571
  set_field( field, args.first )
554
572
  elsif ( field = get_getter_field( methId ) )
555
573
  get_field( field )
574
+ elsif ( methId.to_s =~ /^get_/ and
575
+ ObjectStore.get_object_store.respond_to?( methId ) )
576
+ args = [ self ].concat( args )
577
+ ObjectStore.get_object_store.send( methId, *args )
556
578
  else
557
579
  super( methId, *args )
558
580
  end
@@ -561,8 +583,8 @@ module Lafcadio
561
583
  # Returns the subclass of DomainObject that this instance represents.
562
584
  # Because of the way that proxying works, clients should call this method
563
585
  # instead of Object.class.
564
- def object_type
565
- self.class.object_type
586
+ def domain_class
587
+ self.class.domain_class
566
588
  end
567
589
 
568
590
  # This template method is called before every commit. Subclasses can
data/lib/lafcadio/mock.rb CHANGED
@@ -11,24 +11,26 @@ module Lafcadio
11
11
  end
12
12
 
13
13
  def commit(db_object)
14
- objectsByObjectType = get_objects_by_domain_class( db_object.object_type )
14
+ objects_by_domain_class = get_objects_by_domain_class(
15
+ db_object.domain_class
16
+ )
15
17
  if db_object.delete
16
- objectsByObjectType.delete db_object.pk_id
18
+ objects_by_domain_class.delete( db_object.pk_id )
17
19
  else
18
20
  object_pk_id = get_pk_id_before_committing( db_object )
19
- objectsByObjectType[object_pk_id] = db_object
21
+ objects_by_domain_class[object_pk_id] = db_object
20
22
  end
21
23
  end
22
24
 
23
- def _get_all(object_type)
24
- @retrievals_by_type[object_type] = @retrievals_by_type[object_type] + 1
25
- @objects[object_type] ? @objects[object_type].values : []
25
+ def _get_all( domain_class )
26
+ @retrievals_by_type[domain_class] = @retrievals_by_type[domain_class] + 1
27
+ @objects[domain_class] ? @objects[domain_class].values : []
26
28
  end
27
29
 
28
30
  def get_collection_by_query(query)
29
31
  @query_count[query] += 1
30
32
  objects = []
31
- _get_all( query.object_type ).each { |dbObj|
33
+ _get_all( query.domain_class ).each { |dbObj|
32
34
  if query.condition
33
35
  objects << dbObj if query.condition.object_meets(dbObj)
34
36
  else
@@ -49,7 +51,8 @@ module Lafcadio
49
51
  object_pk_id = db_object.pk_id
50
52
  unless object_pk_id
51
53
  maxpk_id = 0
52
- get_objects_by_domain_class( db_object.object_type ).keys.each { |pk_id|
54
+ pk_ids = get_objects_by_domain_class( db_object.domain_class ).keys
55
+ pk_ids.each { |pk_id|
53
56
  maxpk_id = pk_id if pk_id > maxpk_id
54
57
  }
55
58
  @last_pk_id_inserted = maxpk_id + 1
@@ -70,7 +73,7 @@ module Lafcadio
70
73
  def group_query( query )
71
74
  if query.class == Query::Max
72
75
  if ( query.field_name == 'pk_id' || query.field_name == 'rate' )
73
- query.collect( @objects[query.object_type].values )
76
+ query.collect( @objects[query.domain_class].values )
74
77
  else
75
78
  raise "Can't handle query with sql '#{ query.to_sql }'"
76
79
  end