namarara/README.md

104 lines
2.6 KiB
Markdown
Raw Normal View History

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
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
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
```
2017-09-12 13:32:50 +00:00
## Development
After checking out the repo, run `bin/setup` to install dependencies. 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).
## Contributing
Bug reports and pull requests are welcome on GitHub at https://github.com/glenux/namarara
2017-09-12 13:32:50 +00:00
## License
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).