Skip to content

Commit

Permalink
First version of RuleChecker, refactored, nice use of sintax sugar in…
Browse files Browse the repository at this point in the history
… spec
  • Loading branch information
ratem committed Apr 11, 2011
1 parent 39ed646 commit f09813a
Show file tree
Hide file tree
Showing 8 changed files with 24 additions and 13 deletions.
1 change: 1 addition & 0 deletions bank_system/decorators/bank_account_decorator.py
Expand Up @@ -28,5 +28,6 @@ def decorate(self, decorated):
@classmethod
@rule('association')
def rule_should_be_machine_instance(self, decorated):
''' Decorated object should be a Machine '''
decorated |should| be_instance_of(Machine)

1 change: 1 addition & 0 deletions bank_system/decorators/credit_analyst_decorator.py
Expand Up @@ -27,6 +27,7 @@ def decorate(self, decorated):
@classmethod
@rule('association')
def rule_should_be_person_instance(self, decorated):
''' Decorated object should be a Person '''
decorated |should| be_instance_of(Person)

#creates a loan request
Expand Down
1 change: 1 addition & 0 deletions bank_system/resources/loan.py
Expand Up @@ -19,5 +19,6 @@ def __init__(self, loan_request):

@rule('association')
def rule_should_be_loan_request_instance(self, loan_request):
''' Loan Request should be of type Loan Request '''
loan_request |should| be_instance_of(LoanRequest)

1 change: 1 addition & 0 deletions bank_system/resources/loan_request.py
Expand Up @@ -25,5 +25,6 @@ def __init__(self, account, value, analyst):

@rule('association')
def rule_should_be_bank_account_instance(self, account):
''' Account should be of type Bank Account Decorator '''
account |should| be_instance_of(BankAccountDecorator)

4 changes: 2 additions & 2 deletions configurator/rule_checker.py
Expand Up @@ -48,8 +48,8 @@ def check_rules(self, node):
for decorator in self.decorators:
for method_name, method_object in inspect.getmembers(decorator, inspect.ismethod):
if hasattr(method_object,'rule_category'):
try:
decorator.__dict__[method_name].__get__(None, decorator)(node)
try:#decorator.__dict__[method_name].__get__(None, decorator)(node)
getattr(decorator, method_name)(node)
except:
allowable = False
self.broken_rules.append([decorator, method_object])
Expand Down
10 changes: 5 additions & 5 deletions configurator/spec/rule_checker_spec.py
Expand Up @@ -15,19 +15,19 @@ def setUp(self):
def it_finds_decorators(self):
#it works with ordinary modules
self.rule_checker.find_decorators(bank_system.decorators.bank_account_decorator)
self.rule_checker.decorators |should| have(1).decorator
self.rule_checker |should| have(1).decorators
self.rule_checker.decorators = []
self.rule_checker.find_decorators(bank_system.decorators.credit_analyst_decorator)
#credit_analyst_decorator imports bank_account_decorator => two decorators in the namespace
self.rule_checker.decorators |should| have(2).decorators
self.rule_checker |should| have(2).decorators
self.rule_checker.decorators = []
#it works with configurator.import_decorators, a pure import module
self.rule_checker.find_decorators(configurator.import_decorators)
self.rule_checker.decorators |should| have(2).decorators
self.rule_checker |should| have(2).decorators

def it_checks_rules(self):
self.rule_checker.find_decorators(configurator.import_decorators)
self.rule_checker.check_rules(self.a_person)
self.rule_checker.allowable_decorators |should| have(1).decorator
self.rule_checker.broken_rules |should| have(1).broken_rule
self.rule_checker |should| have(1).allowable_decorators
self.rule_checker |should| have(1).broken_rules

10 changes: 6 additions & 4 deletions domain/resource/operation.py
@@ -1,14 +1,16 @@
#operation is a Python Decorator which is applied to Node/Person and Node/Machine methods

'''
operation is a Python Decorator which is applied to Node/Person and Node/Machine
methods to mark them as business operations. The decorated method must have a
docstring
'''

def operation(**attributes):
def add_attributes(method):
#add attributes to the method
for attr in attributes:
setattr(method, attr, attributes[attr])
#forces the method to have a docstring
if method.__doc__ == None:
raise KeyError
raise ValueError('Operation must have a docstring')
return method
return add_attributes

9 changes: 7 additions & 2 deletions domain/supportive/rule.py
@@ -1,9 +1,14 @@
#rule is a Python Decorator to typify methods as rule of association

'''
Rule is a Python Decorator to typify methods as rule of association, for them to
work with Configurator module, @classmethod must also be applyed. If the method
doesn't have a docstring, raises exception.
'''

def rule(rule_category):
def add_attribute(method):
setattr(method, 'rule_category', rule_category)
if method.__doc__ == None:
raise ValueError('Rule must have a docstring')
return method
return add_attribute

0 comments on commit f09813a

Please sign in to comment.