TODO: Write a gem description
Add this line to your application's Gemfile:
gem 'lamep'
And then execute:
$ bundle
Or install it yourself as:
$ gem install lamep
You can use lamep for:
- Parsing expression into array of tokens:
# Splits expression into single tokens
tokens = TokenParser.new.parse('material = wood && (price > 180 || price <= 250)')
Variable tokens
will be array of tokens:
%w{material = wood && ( price > 180 || price <= 250 )}
- Converting tokens into postfix notation:
# Converts tokens into postfix notation (also known as RPN)
postfix = ShuntingYard.new(tokens).posfix
Variable postfix
will be array of tokens in postfix notation:
%w{material wood = price 180 > price 250 <= || &&}
- Building Abstract Syntax Tree:
# Builds tree from postfix
ast = AbstractSyntaxTreeBuilder.new(postfix).build_tree
Variable ast
will be containing such structure:
ruby And.new( Equal.new('material', 'wood'), Or.new( GreaterThan.new('price', '180'), LessThanEqual.new('price', '250') ) )
4. Building SQL WHERE clause:
# Builds SQL WHERE clause
sql_where = ast.to_sql
Variable sql_where
will be string:
'((material = wood) AND ((price > 180) OR (price <= 250)))'
At this point we support following list of operators and their SQL conversion:
operator | description |
---|---|
= | Equal |
!= | Not Equal |
> | Greater Than |
>= | Greater Than Equal |
< | Less Than |
<= | Less Than Equal |
&& | Logical AND |
|| | Logical OR |
- | Unary Minus |
The rest of passed expression is considered as an operand i.e. either variable or a number.
TODO:
- Fork it ( https://github.com/forex-kaiz/lamep/fork )
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create a new Pull Request