from mojo.events import postEvent


class evented(object):

    def __init__(self, subject=None, verb=None):
        self.subject = subject
        self.verb = verb

    def __call__(self, fn):
        decorator = self

        def wrapped(self, *args, **kwargs):
            subject = decorator.subject or self.__class__.__name__
            verb = decorator.verb or fn.__name__

            with EventDispatcher(self, subject, verb):
                return fn(self, *args, **kwargs)

        return wrapped


class EventDispatcher(object):

    def __init__(self, object_, subject, verb):
        self.object = object_
        self.subject = subject
        self.verb = verb

    def __enter__(self):
        self.post('will')

    def __exit__(self, error_type, value, trace):
        if error_type is not None:
            self.post('failed')
        else:
            self.post('did')

    def post(self, tense):
        postEvent(self.event_name(tense), **{self.subject: self.object})

    def event_name(self, tense):
        return ''.join(self.event_name_parts(tense))

    def event_name_parts(self, tense):
        return [
               self.subject.lower(),
               tense.capitalize(),
               self.verb.capitalize()
               ]

@@ -510,31 +510,29 @@
 %0A%0Aclass 
-EventDispatcher
+RoboFontEvent
 (object)
@@ -683,20 +683,26 @@
 f __
-enter
+call
 __(self
+, tense
 ):%0A 
@@ -712,286 +712,412 @@
     
-self.post('will')%0A%0A    def __exit__(self, error_type, value, trace):%0A        if error_type is not None:%0A            self.post('failed')%0A        else:%0A            self.post('did')%0A%0A    def post(self, tense
+postEvent(self.name(tense), **%7Bself.subject: self.object%7D)%0A%0A    def name(self, tense):%0A        return ''.join(%5B%0A                       self.subject.lower(),%0A                       tense.capitalize(),%0A                       self.verb.capitalize()%0A                       %5D%0A%0A%0Aclass EventDispatcher(object
 ):%0A
+%0A
     
-    postEvent(self.event_name(tense), **%7Bself.subject: self.object%7D
+adapter = RoboFontEvent%0A%0A    def __init__(self, *args):%0A        self.event = self.adapter(*args
 )%0A%0A 
@@ -1127,144 +1127,138 @@
 def 
-event_name(self, tense):%0A        return ''.join(self.event_name_parts(tense))%0A%0A    def event_name_parts(self, tense):%0A        return %5B%0A 
+__enter__(self):%0A        self.event('will')%0A%0A    def __exit__(self, error_type, value, trace):%0A        if error_type is not None:%0A
     
@@ -1269,66 +1269,42 @@
     
-  
 self.
-subject.lower(),%0A               tense.capitalize(),
+event('failed')%0A        else:
 %0A   
@@ -1316,47 +1316,80 @@
     
-   
 self.
+e
 ve
-rb.capitalize()%0A               %5D
+nt('did')%0A%0A    def __call__(self, tense):%0A        self.event(tense)
 %0A

use adapter pattern for event dispatcher