| 1 | import sys |
|---|
| 2 | |
|---|
| 3 | MUTATORS = '__elixir_mutators__' |
|---|
| 4 | |
|---|
| 5 | class ClassMutator(object): |
|---|
| 6 | ''' |
|---|
| 7 | DSL-style syntax |
|---|
| 8 | |
|---|
| 9 | A ``ClassMutator`` object represents a DSL term. |
|---|
| 10 | ''' |
|---|
| 11 | |
|---|
| 12 | def __init__(self, handler): |
|---|
| 13 | ''' |
|---|
| 14 | Create a new ClassMutator, using the `handler` callable to process it |
|---|
| 15 | when the time will come. |
|---|
| 16 | ''' |
|---|
| 17 | self.handler = handler |
|---|
| 18 | |
|---|
| 19 | # called when a mutator (eg. "has_field(...)") is parsed |
|---|
| 20 | def __call__(self, *args, **kwargs): |
|---|
| 21 | # self in this case is the "generic" mutator (eg "has_field") |
|---|
| 22 | |
|---|
| 23 | # jam this mutator into the class's mutator list |
|---|
| 24 | class_locals = sys._getframe(1).f_locals |
|---|
| 25 | mutators = class_locals.setdefault(MUTATORS, []) |
|---|
| 26 | mutators.append((self, args, kwargs)) |
|---|
| 27 | |
|---|
| 28 | def process(self, entity, *args, **kwargs): |
|---|
| 29 | ''' |
|---|
| 30 | Process one mutator. This version simply calls the handler callable, |
|---|
| 31 | but another mutator (sub)class could do more processing. |
|---|
| 32 | ''' |
|---|
| 33 | self.handler(entity, *args, **kwargs) |
|---|
| 34 | |
|---|
| 35 | |
|---|
| 36 | #TODO: move this to the super class (to be created here) of EntityMeta |
|---|
| 37 | def process_mutators(entity): |
|---|
| 38 | ''' |
|---|
| 39 | Apply all mutators of the given entity. That is, loop over all mutators |
|---|
| 40 | in the class's mutator list and process them. |
|---|
| 41 | ''' |
|---|
| 42 | # we don't use getattr here to not inherit from the parent mutators |
|---|
| 43 | # inadvertantly if the current entity hasn't defined any mutator. |
|---|
| 44 | mutators = entity.__dict__.get(MUTATORS, []) |
|---|
| 45 | for mutator, args, kwargs in mutators: |
|---|
| 46 | mutator.process(entity, *args, **kwargs) |
|---|
| 47 | |
|---|
| 48 | class Statement(ClassMutator): |
|---|
| 49 | |
|---|
| 50 | def process(self, entity, *args, **kwargs): |
|---|
| 51 | builder = self.handler(entity, *args, **kwargs) |
|---|
| 52 | entity._descriptor.builders.append(builder) |
|---|
| 53 | |
|---|
| 54 | class PropertyStatement(ClassMutator): |
|---|
| 55 | |
|---|
| 56 | def process(self, entity, name, *args, **kwargs): |
|---|
| 57 | prop = self.handler(*args, **kwargs) |
|---|
| 58 | prop.attach(entity, name) |
|---|