Skip to content

Commit

Permalink
Merge pull request venmo#8 from venmo/ge_simons_review
Browse files Browse the repository at this point in the history
Decorate all type classes so they are exported in export_rule_data
  • Loading branch information
gregeinfrank committed Jun 16, 2014
2 parents 632a063 + e9e4813 commit 002ee9c
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 11 deletions.
1 change: 1 addition & 0 deletions business_rules/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@
from .engine import run_all
from .utils import export_rule_data

# Appease pyflakes by "using" these exports
assert run_all
assert export_rule_data
12 changes: 11 additions & 1 deletion business_rules/operators.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ def get_all_operators(cls):
for m in methods if getattr(m[1], 'is_operator', False)]


def export_type(cls):
""" Decorator to expose the given class to business_rules.export_rule_data. """
cls.export_in_rule_data = True
return cls


def type_operator(input_type, label=None,
assert_type_for_arguments=True):
""" Decorator to make a function into a type operator.
Expand All @@ -49,6 +55,7 @@ def inner(self, *args, **kwargs):
return wrapper


@export_type
class StringType(BaseType):

name = "string"
Expand Down Expand Up @@ -89,6 +96,7 @@ def non_empty(self):
return bool(self.value)


@export_type
class NumericType(BaseType):
EPSILON = 0.000001

Expand Down Expand Up @@ -122,6 +130,7 @@ def less_than_or_equal_to(self, other_numeric):
return self.less_than(other_numeric) or self.equal_to(other_numeric)


@export_type
class BooleanType(BaseType):

name = "boolean"
Expand All @@ -140,7 +149,7 @@ def is_true(self):
def is_false(self):
return not self.value


@export_type
class SelectType(BaseType):

name = "select"
Expand Down Expand Up @@ -174,6 +183,7 @@ def does_not_contain(self, other_value):
return True


@export_type
class SelectMultipleType(BaseType):

name = "select_multiple"
Expand Down
14 changes: 4 additions & 10 deletions business_rules/utils.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import inspect

def fn_name_to_pretty_label(name):
return ' '.join([w.title() for w in name.split('_')])
Expand All @@ -10,19 +11,12 @@ def export_rule_data(variables, actions):
- actions: a list of all actions along with their label and params
- variable_type_operators: a dictionary of all field_types -> list of available operators
"""
from . import operators
actions_data = actions.get_all_actions()
variables_data = variables.get_all_variables()
variable_type_operators = {}
from .operators import (StringType,
NumericType,
BooleanType,
SelectType,
SelectMultipleType)
for variable_type in [StringType,
NumericType,
BooleanType,
SelectType,
SelectMultipleType]:
for variable_class in inspect.getmembers(operators, lambda x: getattr(x, 'export_in_rule_data', False)):
variable_type = variable_class[1] # getmembers returns (name, value)
variable_type_operators[variable_type.name] = variable_type.get_all_operators()

return {"variables": variables_data,
Expand Down

0 comments on commit 002ee9c

Please sign in to comment.