=====
Rules
=====

The Rules class is a mother class for apply rules on the cart. It's simply to
use it, initialize an instance and call it this a context (generaly the
context will be the cart instance)::

    >>> from rules.rules import Rules
    >>> process = Rules()
    >>> process('foo')
    'foo'

You could create your own rule, you juste need to inherit from the Rules class
and overload the rule fonction of the class::

    >>> class doubleContext(Rules):
    ...     def rule(self, value, *args, **kwarg):
    ...         return value+value
    >>> process = doubleContext()
    >>> process('foo')
    'foofoo'

You could also use the *args and **kwarg context into your rule method::

    >>> class deleteChar(Rules):
    ...     def rule(self, value, *args, **kwarg):
    ...         if 'char_to_del' in kwarg:
    ...             char = kwarg['char_to_del']
    ...         else:
    ...             char = ' '
    ...         return value.replace(char, '')
    >>> process = deleteChar()
    >>> process('foo')
    'foo'
    >>> process('foo', char_to_del='o')
    'f'

The particularity of the Rules class is that you could chain the rules. when
you want call an other rules just initialing it with the old rule::

    >>> chain_process = deleteChar()
    >>> chain_process = doubleContext(chain_process)
    >>> chain_process('foo', char_to_del='o')
    'ff'

The order of the chain excution is like a stack, the last rule initialed will
be the first to be execute::

    >>> class decorateA(Rules):
    ...     def rule(self, value, *args, **kwarg):
    ...         return 'A %s' % value
    >>> class decorateB(Rules):
    ...     def rule(self, value, *args, **kwarg):
    ...         return 'B %s' % value
    >>> chain_process = decorateA()
    >>> chain_process = decorateB(chain_process)
    >>> chain_process('My value')
    'A B My value'
