0.0
No release in over a year
Build complex rules, serialize them as JSON, and execute them in ruby. See https://jsonlogic.com
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
 Dependencies

Runtime

 Project Readme

JsonLogic

Build rules and execute them in ruby. See https://jsonlogic.com

Installation

Install the gem and add to the application's Gemfile by executing:

$ bundle add json_logic_ruby

If bundler is not being used to manage dependencies, install the gem by executing:

$ gem install json_logic_ruby

Usage

Run json-logic:

RULE = <<~JSON
  { "and" : [
      {">=" : [ { "var" : "length" }, 15 ]},
      {">=" : [ { "var" : "size" }, 50 ]}
    ] }
JSON

DATA = JSON.parse('{ "length": 20, "size": 49 }')

logic = JsonLogic::Evaluator.new
logic.apply(JSON.parse(RULE), DATA)
Get all variables used in a rule
logic = JsonLogic::Evaluator.new
res = logic.extract_vars(JSON.parse(RULE))
puts res

# will print -> ["length", "size"]
Track report of all operations
logic = JsonLogic::Evaluator.new
logic.apply(JSON.parse(RULE), DATA)

puts logic.tracker.report

# will print
# LOGIC: 'and', RESULT = false
#   DATA: 'length' data:20 >= expected:15,  RESULT = true
#   DATA: 'size' data:49 >= expected:50,  RESULT = false

Development

After checking out the repo, run bundle install to install dependencies.

Contributing

Please see CONTRIBUTING.md.

License

The gem is available as open source under the terms of the MIT License.

Code of Conduct

Everyone interacting in the Json::Logic project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.