Introducing Rubel: a Ruby Expression Language
Ever wanted to evaluate Ruby-like expressions from your database? For example user configurable validations for models, or custom view logic that is outside of the programmers control. We need it for custom field validations on an ActiveRecord model in an upcoming Rails application.
Meet Ruble, the Ruby Expression Language. A simple, safe and fun way to evaluate expressions of the kind user.income > 100000 or user.status == "Boss".
It is based on the awesome Treetop gem that does the heavy lifting of parsing and evaluating the expressions.
With some basic helper that also caches expressions, a Rubel expression can be evaluated as simple as writing:
rubel = Rubel::Evaluator.new result = rubel.evaluate 'firstname == "Charly" or age > 16', :with => {'user' => {'firstname' => 'Charly', 'age' => 15} # result -> true
You can get the source code at http://github.com/chlu/rubel. A gem build is on the way and after only a few hours of work until now there will be more to come.
- 0 Kommentare


Mein Kommentar