28. Okt 2009

Introducing Rubel: a Ruby Expression Language

Gravatar: Christopher Hlubek

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:

  1. rubel = Rubel::Evaluator.new
  2. result = rubel.evaluate 'firstname == "Charly" or age > 16',
  3. :with => {'user' => {'firstname' => 'Charly', 'age' => 15}
  4. # 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.

Trackback-Link
  •  
  • 0 Kommentare
  •  

Mein Kommentar

Benachrichtige mich, wenn jemand einen Kommentar zu dieser Nachricht schreibt.

Zurück