dfunckt/django-rules — pull request #15 by dfunckt (2014-10-15 20:36:22+00:00)
Title: Predicate call context (WIP)

Related to #6, #7, #9.

Adds the ability to specify that a predicate wants a dict containing information about the current ``test()`` invocation.

```
>>> @rules.predicate(takes_context=True)
>>> def mypred(a, b, **kwargs):
... print(kwargs['context'])
... return True
...
>>> rules.add_rule('myrule', mypred)
>>> rules.test_rule('myrule', 1, 2)
{'args': (1, 2), 'kwargs': {}}
True
```

Predicates that want context must accept ``**kwargs``.

A nice advantage of this PR is that it allows predicates to store arbitrary data that other predicates may later use. It is possible for example, to store an expensive computation done in one predicate (maybe fetch a bunch of objects from db, related to the given user/object) and another predicate access it later on.

This is a backwards-compatible -- at least all tests pass.

Here are some things that need to be done in order for this PR to be merged:

- [ ] Decide on the API -- pretty close already, I guess.
- [ ] Write docs -- add a new section in README for now.
- [ ] Find any glaring bugs.
- [ ] Some more tests maybe.

All feedback welcome.