I'd like to allow users to define simple business logic such as:
if (x and y) then ...
if (z or w) then ...
Let me put it concretely:
I'm developing a HR module that answers if applicants fulfill some requirements, to be defined by users.
Such requirements can be defined around logical operators:
(Must be 18 years old) or (under 18 years and must have parents permission)
Is putting this kind of logic inside the database ok? I think it is, but I'm afraid of spending time on this and find that its a poor approach.
As so often, the answer is "it depends" ;) Since it seems that the logic in this case is user-defined data, putting it into the database is absolutely reasonable.
But if you are looking to model the structure/AST of this input into separate business objects with their
and
andor
control flow reflected in the database records, I would have to say that it's very likely overkill and will - apart from the initial implementation overhead - make future refactoring very hard.A simple text field that will be evaluated at runtime is the easiest way to go as its contents can be very easily extracted and reasoned about.
Not knowing your definitive requirements, I'd suggest you take a look at Drools, a rules engine for Java, which has in its ecosystem also a rule storage backend and guided editor. Incidentally the example in your question looks a lot like it might benefit from a rules engine but unfortunately I don't have any practical experience with any of the related Ruby libraries.
Otherwise this article on the thougtbot blog - Writing a Domain Specific Language in Ruby - might be helpful in this context, too.