OZ-Decorators
These decorators classes helps you focus on your logic and hides the issues regarding decorators.
With this module you can decorate classes, methods, members and accessors (properties with get and or set).
API
// Decorates the code (class, function) and returns afactory method to an instance of the specified DecoratorBase (any argument will be passed to the constructor) // usage: @decorate(MergeClassesDecorator)(ConsoleLogger, ExtraLog) // Shared decorator API (no need to implement, next classes are the real bases) // base for decorating classes // class to decorate a accessor (property with get and or set) // class to decorte methods (functions) // class to decorte methods parameters (functions arguments)
Usage
Add behavior to method
To wrap method with extra behavior you can use MethodDecorator This is how to add log on methods
Now let's call it
t.foo
output will be
calling foo in wrapingMethod
foo
Add functionality to class
Here is how to add log functionality to Test class.
First, we'll declare a class that will have the logging functionality
For ease of use (and 'compile time' error), we'll merge types:
Now let's decorate Test class (merge it with Logger):
We'll get:
calling foo in wrapingMethod
foo
0: debugging foo
Dispatcher class
Here is how to use decorators and Dispatcher class to dispatch actions in run-time:
First we'll declare an Action class like so:
Now let's declare a dispatcher class that will call methods accordingly.
Two methods must be defined:
- getKey - this returns the key from the dispatched object
- getParamFromDispatched - this returns a value from the dispatched object to be injected to a parameter on the target method.
Method will be "dispatchable" after decorating with @classDispacher().
Actual dispatched methods can be private.
Usage:
ad.dispatch // action1 will be invoked ad.dispatch // action2 will be invoked with 'value2' injected to inputParam1 parameter ad.dispatch // defaultAction will be invoked
output:
in action1
in action2: value2
in defaultAction: value101, value102