2019-12-21 15:55:15 +00:00
# Namarara
2017-09-04 09:19:29 +00:00
2019-12-27 19:45:44 +00:00
Namarara is a library that can parses boolean expressions, builds an [binary
expression tree](https://en.wikipedia.org/wiki/Binary_expression_tree) and
evalutes a result given a set of values associated to the variables used within
the boolean expression.
2017-09-04 09:19:29 +00:00
2017-09-12 13:32:50 +00:00
## Installation
2017-09-04 09:19:29 +00:00
2017-09-12 13:32:50 +00:00
Add this line to your application's Gemfile:
2017-09-04 09:19:29 +00:00
2017-09-12 13:32:50 +00:00
```ruby
2019-12-21 16:15:14 +00:00
gem 'namarara'
2017-09-12 13:32:50 +00:00
```
2017-09-04 09:19:29 +00:00
2017-09-12 13:32:50 +00:00
And then execute:
2017-09-04 09:19:29 +00:00
2019-12-27 19:42:56 +00:00
```shell-session
$ bundle
```
2017-09-04 09:19:29 +00:00
2017-09-12 13:32:50 +00:00
Or install it yourself as:
2017-09-04 09:19:29 +00:00
2019-12-27 19:42:56 +00:00
```shell-session
$ gem install namarara
```
2017-09-04 09:19:29 +00:00
2017-09-12 13:32:50 +00:00
## Usage
2017-09-04 09:19:29 +00:00
2019-12-21 15:55:15 +00:00
### Evaluate a single expression
2019-12-27 19:42:56 +00:00
```ruby
require 'namarara'
2019-12-21 15:55:15 +00:00
# Initialize Namarara
2019-12-21 16:15:14 +00:00
namarara = Namarara::Parser.new(Namarara::Lexer.new)
2019-12-21 15:55:15 +00:00
# Prepare variables
2019-12-27 19:42:56 +00:00
namarara.names = {
this: 'true',
that: 'false',
other: 'false',
something_else: 'true'
2019-12-21 15:55:15 +00:00
}
2019-12-27 19:42:56 +00:00
# Build a binary expression tree (aka BET) from string
# and inject values
exp_tree = namarara.parse('this AND (that OR other) AND something_else')
2019-12-21 15:55:15 +00:00
# Compute tree with variables
2019-12-27 19:42:56 +00:00
result = exp_tree.compute
puts result # = false
2019-12-21 15:55:15 +00:00
```
### Evaluating a set of rules
```ruby
2019-12-27 19:42:56 +00:00
require 'namarara'
2019-12-21 15:55:15 +00:00
# Initialize Namarara
namarara = Namarara::Parser.new(Namarara::Lexer.new)
2019-12-27 19:42:56 +00:00
# A set of rules i want to check
# (in this example we are looking for sensitive personnal data)
rules = {
vulnerable_person: 'is_adult AND is_subordinate',
has_constraints: 'is_adult AND has_children',
is_child: 'NOT is_adult'
2019-12-21 15:55:15 +00:00
# ...
2019-12-27 19:42:56 +00:00
}
2019-12-21 15:55:15 +00:00
2019-12-27 19:42:56 +00:00
# A set of values i want to inject (values must be expressed as strings)
2019-12-21 15:55:15 +00:00
namarara.names = {
"is_adult" => 'false',
"is_subordinate" => 'true',
"has_children" => 'true'
}
2019-12-27 19:42:56 +00:00
results = rules.map { |rule, expr| [rule, namarara.parse(expr).compute] }
2019-12-21 15:55:15 +00:00
2019-12-27 19:42:56 +00:00
if results.select{ |rule, value| value }.empty?
puts "Perfect! Nothing to say ;-)"
2019-12-21 15:55:15 +00:00
else
2019-12-27 19:42:56 +00:00
puts "Warning: you are collectif sensitive personnal data !"
results.each do |rule, value|
puts "#{value ? '>>':' '} #{rule}: #{value}"
end
2019-12-21 15:55:15 +00:00
end
2019-12-21 16:15:14 +00:00
```
2017-09-12 13:32:50 +00:00
## Development
2020-01-05 16:20:57 +00:00
After checking out the repo, run `bin/setup` to install dependencies.
2017-09-12 13:32:50 +00:00
2020-01-05 16:20:57 +00:00
Then, run `rake test` to run the tests.
You can also run `bin/console` for an interactive prompt that will allow you to experiment.
To install this gem onto your local machine, run `bundle exec rake install` .
To release a new version, update the version number in `version.rb` , and then run `bundle exec rake release` , which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org ](https://rubygems.org ).
2017-09-12 13:32:50 +00:00
## Contributing
2019-12-21 16:15:14 +00:00
Bug reports and pull requests are welcome on GitHub at https://github.com/glenux/namarara
2017-09-12 13:32:50 +00:00
2020-01-05 16:20:57 +00:00
## License and copyright
[Namarara ](https://github.com/glenux/namarara ) is an open source project under the terms of the [MIT License ](http://opensource.org/licenses/MIT ).
Original author : [Brendon Torre ](https://www.linkedin.com/in/brendon-torre-b128a0168 )
Current developer & maintainer : Glenn Y. Rolland ([@glenux](https://twitter.com/glenux))
## Sponsors and funding
[Namarara ](https://github.com/glenux/namarara ) is an independent project whose development and maintenance is made possible thanks to the support of its patrons.
If you wish to join them and support the work of its author, just participate with this link :
__> > > [Become a patron or sponsor on Patreon](https://www.patreon.com/glenux) < < < __