Skip to content

Commit

Permalink
Add RemoveMatchRule and AppendMatchRule classes
Browse files Browse the repository at this point in the history
  • Loading branch information
Toilal committed Oct 18, 2015
1 parent f0383ba commit 80dd929
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 0 deletions.
25 changes: 25 additions & 0 deletions rebulk/rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from itertools import groupby

import six
from .utils import is_iterable


@six.add_metaclass(ABCMeta)
Expand Down Expand Up @@ -70,6 +71,30 @@ def __repr__(self):
return self.name if self.name else self.__class__.__name__


class RemoveMatchRule(Rule): # pylint: disable=abstract-method
"""
Remove matches returned by then
"""
def then(self, matches, when_response, context):
if is_iterable(when_response):
for match in when_response:
matches.remove(match)
else:
matches.remove(when_response)


class AppendMatchRule(Rule): # pylint: disable=abstract-method
"""
Append matches returned by then
"""
def then(self, matches, when_response, context):
if is_iterable(when_response):
for match in when_response:
matches.append(match)
else:
matches.append(when_response)


class Rules(list):
"""
list of rules ready to execute.
Expand Down
24 changes: 24 additions & 0 deletions rebulk/test/default_rules_module.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# pylint: disable=no-self-use, pointless-statement, missing-docstring, invalid-name
from ..match import Match
from ..rules import RemoveMatchRule, AppendMatchRule


class RuleRemove0(RemoveMatchRule):
def when(self, matches, context):
return matches[0]


class RuleAppend0(AppendMatchRule):
def when(self, matches, context):
return Match(5, 10)

class RuleRemove1(RemoveMatchRule):
def when(self, matches, context):
return [matches[0]]


class RuleAppend1(AppendMatchRule):
def when(self, matches, context):
return [Match(5, 10)]
31 changes: 31 additions & 0 deletions rebulk/test/test_rules.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# pylint: disable=no-self-use, pointless-statement, missing-docstring, invalid-name
from rebulk.test.default_rules_module import RuleRemove0, RuleAppend0, RuleAppend1, RuleRemove1

from ..rules import Rules
from ..match import Matches, Match
Expand Down Expand Up @@ -53,6 +54,36 @@ def test_rule_when():
assert matches[1] == Match(3, 4)


def test_default_rules():
rules = Rules(RuleRemove0)

matches = Matches([Match(1, 2)])
rules.execute_all_rules(matches, {})

assert len(matches) == 0

rules = Rules(RuleAppend0)

matches = Matches([Match(1, 2)])
rules.execute_all_rules(matches, {})

assert len(matches) == 2

rules = Rules(RuleRemove1)

matches = Matches([Match(1, 2)])
rules.execute_all_rules(matches, {})

assert len(matches) == 0

rules = Rules(RuleAppend1)

matches = Matches([Match(1, 2)])
rules.execute_all_rules(matches, {})

assert len(matches) == 2


def test_rule_module():
rules = Rules(rm)

Expand Down

0 comments on commit 80dd929

Please sign in to comment.