Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Aug 8th, 2012  |  syntax: None  |  size: 0.97 KB  |  hits: 10  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. class EntityFactory:
  2.     persistenceEngine = None
  3.     def __new__(cls, type_, bases, dict_):
  4.         ''' cls is the metaclass instance (EntityFactory)?
  5.            type_ is the Entity drived class
  6.            bases should be 'Entity'
  7.            dict_ should be empty'''
  8.         instance = object.__new__(cls)
  9.         instance.persistenceEngine = persistenceEngine
  10.         instance.isDirty = False
  11.         instance.isPersisted = False
  12.         return instance
  13.  
  14.     def newA( id_ ):
  15.         a = EntityFactory.__new__(A.__name__, (Entity, ), {})
  16.         if id_ == None:
  17.             return a
  18.            
  19.         modelObject = persistenceEngine.getRecordForID(id_)
  20.         if modelObject == None:
  21.             return a
  22.  
  23.         a.modelObject = modelObject
  24.  
  25.         a.isPersisted = True
  26.         return a
  27.  
  28.  
  29. class Entity(object, metaclass=EntityFactory):
  30.         id_ = None
  31.  
  32.         #save and load to legacy file through model here
  33.  
  34. class A(Entity):
  35.     pass