Permalink
Browse files

Enabled symbol lookup in context

  • Loading branch information...
1 parent fae1242 commit f61697db999186fb9d7691cb4b780c652fcc61ad @hlubek committed Oct 28, 2009
Showing with 15 additions and 2 deletions.
  1. +5 −1 README.textile
  2. +6 −1 rubel.treetop
  3. +4 −0 rubel_test.rb
View
@@ -14,11 +14,15 @@ h2. How does it work?
Rubel evaluates _expressions_ in a given _context_. Expressions are a subset of safe Ruby code. A
context is simply a Hash for now.
+h3. Example expression
+
<pre language="Ruby">
user.firstname == 'Charly' or user.age > 16
</pre>
-You can evaluate a Rubel expression like above with:
+h3. Evaluating expressions
+
+You can evaluate a Rubel expression like above inside your code:
<pre language="Ruby">
parser = RubelParser.new
View
@@ -113,7 +113,12 @@ grammar Rubel
rule identifier
identifier_letters:([a-zA-Z] [a-zA-Z0-9_]*) '' {
def value(context)
- context[identifier_letters.text_value]
+ identifier_text = identifier_letters.text_value
+ if context.has_key?(identifier_text)
+ context[identifier_text]
+ else
+ context[identifier_text.to_sym]
+ end
end
}
end
View
@@ -33,6 +33,10 @@ def test_identifier_value
tree = @parser.parse 'my_identifier'
assert_equal 42, tree.value({'my_identifier' => 42})
end
+ def test_identifier_value_from_sym
+ tree = @parser.parse 'my_identifier'
+ assert_equal 42, tree.value({:my_identifier => 42})
+ end
def test_parses_path
tree = @parser.parse 'my_path.my_identifier'
assert tree

0 comments on commit f61697d

Please sign in to comment.