🟢 Stable | A library that can parses boolean expressions, builds an binary expression tree and evalue given a set of values
Find a file
2023-05-09 08:24:38 +00:00
.github/workflows Update badges again 2020-01-25 17:25:01 +01:00
bin Rename project mm2ep_depend to namarara 2019-12-21 17:15:14 +01:00
doc Add doc directory 2020-05-18 12:24:35 +02:00
exe Rename namarara executable to namarara-cli 2019-12-21 20:30:31 +01:00
lib Add description + fix dependencies + bump version 2020-01-25 17:13:33 +01:00
spec Add nice examples 2019-12-27 20:43:54 +01:00
.gitignore Update gitignore 2020-05-18 12:24:45 +02:00
.rubocop.yml Update rubocop linting rules 2019-12-21 20:27:54 +01:00
.ruby-version Force ruby version 2017-11-18 16:41:52 +01:00
.tool-versions Clean unit tests 2019-12-21 16:55:15 +01:00
.travis.yml Update project architecture 2017-09-12 15:32:50 +02:00
demo.rb Rename project mm2ep_depend to namarara 2019-12-21 17:15:14 +01:00
Dockerfile Add Dockerfile 2020-05-18 12:27:06 +02:00
Gemfile Rename project mm2ep_depend to namarara 2019-12-21 17:15:14 +01:00
LICENSE.txt Update project architecture 2017-09-12 15:32:50 +02:00
namarara.gemspec Add description + fix dependencies + bump version 2020-01-25 17:13:33 +01:00
Rakefile Enable rubocop 2017-09-20 10:46:17 +02:00
README.md Update 'README.md' 2023-05-09 08:24:38 +00:00
TODO.md Add TODO 2017-09-20 10:51:18 +02:00

Namarara

Build Gem Version GitHub license Donate on patreon Please don't upload to GitHub

Namarara is a library that can parses boolean expressions, builds an binary expression tree and evalutes a result given a set of values associated to the variables used within the boolean expression.

Installation

Add this line to your application's Gemfile:

gem 'namarara'

And then execute:

$ bundle

Or install it yourself as:

$ gem install namarara

Usage

Evaluate a single expression

require 'namarara'

# Initialize Namarara
namarara = Namarara::Parser.new(Namarara::Lexer.new)

# Prepare variables 
namarara.names = {
  this: 'true',
  that: 'false',
  other: 'false',
  something_else: 'true'
}

# Build a binary expression tree (aka BET) from string
# and inject values
exp_tree = namarara.parse('this AND (that OR other) AND something_else')

# Compute tree with variables
result = exp_tree.compute
puts result # = false

Evaluating a set of rules

require 'namarara'

# Initialize Namarara
namarara = Namarara::Parser.new(Namarara::Lexer.new)

# 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'
    # ...
}

# A set of values i want to inject (values must be expressed as strings)
namarara.names = {
    "is_adult" => 'false', 
    "is_subordinate" => 'true',
    "has_children" => 'true'
}

results = rules.map { |rule, expr| [rule, namarara.parse(expr).compute] }

if results.select{ |rule, value| value }.empty?
    puts "Perfect! Nothing to say ;-)"
else
    puts "Warning: you are collecting sensitive personnal data !"
    results.each do |rule, value|
      puts "#{value ? '>>':'  '} #{rule}: #{value}" 
    end
end

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.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/glenux/namarara

Namarara is an open source project under the terms of the MIT License.

Original author : Brendon Torre

Current developer & maintainer : Glenn Y. Rolland (@glenux)

Sponsors and funding

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 <<<