Show
Ignore:
Timestamp:
08/01/07 02:05:43 (7 years ago)
Author:
cleverdevil
Message:

First part of moving events into the core of elixir. Next, I will be moving events.py out of ext/ and directly into the elixir namespace.

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • elixir/trunk/elixir/entity.py

    r169 r170  
    44 
    55from sqlalchemy                     import Table, Integer, desc 
    6 from sqlalchemy.orm                 import deferred, Query 
     6from sqlalchemy.orm                 import deferred, Query, MapperExtension 
    77from sqlalchemy.ext.assignmapper    import assign_mapper 
    88from sqlalchemy.util                import OrderedDict 
     
    1818import sys 
    1919import elixir 
     20import inspect 
    2021 
    2122__pudge_all__ = ['Entity', 'EntityMeta'] 
     
    106107            elixir.delayed_entities.add(self) 
    107108            return 
    108  
     109         
     110        self.setup_events() 
    109111        self.setup_table() 
    110112        self.setup_mapper() 
     
    120122        # finally, allow the statement to do any "finalization" 
    121123        Statement.finalize(self.entity) 
     124     
     125    def setup_events(self): 
     126        # create a list of callbacks for each event 
     127        methods = {} 
     128        for name, func in inspect.getmembers(self.entity, inspect.ismethod): 
     129            if hasattr(func, '_elixir_events'): 
     130                for event in func._elixir_events: 
     131                    event_methods = methods.setdefault(event, []) 
     132                    event_methods.append(func) 
     133         
     134        if not methods: 
     135            return 
     136         
     137        # transform that list into methods themselves 
     138        for event in methods: 
     139            methods[event] = self.make_proxy_method(methods[event]) 
     140         
     141        # create a custom mapper extension class, tailored to our entity 
     142        ext = type('EventMapperExtension', (MapperExtension,), methods)() 
     143         
     144        # then, make sure that the entity's mapper has our mapper extension 
     145        self.add_mapper_extension(ext) 
     146     
     147    def make_proxy_method(self, methods): 
     148        def proxy_method(self, mapper, connection, instance): 
     149            for func in methods: 
     150                func(instance) 
     151        return proxy_method 
    122152     
    123153    def translate_order_by(self, order_by):