You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem.
4
-
Put your Ruby code in the file `lib/json_logic`. To experiment with that code, run `bin/console` for an interactive prompt.
3
+
Build rules and execute them in ruby. See https://jsonlogic.com
5
4
6
5
7
6
## Installation
@@ -15,41 +14,45 @@ If bundler is not being used to manage dependencies, install the gem by executin
15
14
$ gem install json_logic
16
15
17
16
## Usage
17
+
#### Run json-logic:
18
+
18
19
```ruby
19
20
RULE=<<~JSON
20
21
{ "and" : [
21
-
{">=" : [ { "var" : "age" }, 50 ]},
22
-
{">=" : [ { "var" : "bmi" }, 50 ]}
22
+
{">=" : [ { "var" : "length" }, 15 ]},
23
+
{">=" : [ { "var" : "size" }, 50 ]}
23
24
] }
24
25
JSON
25
26
26
-
DATA=JSON.parse('{ "age" : 51, "bmi" : 49}')
27
+
DATA=JSON.parse('{ "length": 20, "size": 49}')
27
28
28
29
logic =JsonLogic::Evaluator.new
29
-
res = logic.apply(JSON.parse(RULE), DATA)
30
-
puts"Criteria eligible: #{res}"
30
+
logic.apply(JSON.parse(RULE), DATA)
31
+
```
32
+
##### Get all variables used in a rule
31
33
32
-
# extract all variables used in criteria
34
+
```ruby
35
+
logic =JsonLogic::Evaluator.new
33
36
res = logic.extract_vars(JSON.parse(RULE))
34
-
puts"Variables used in criteria: #{res}"
37
+
puts res
35
38
36
-
# will print
37
-
#
38
-
# Variables used in criteria: ["age", "bmi"]
39
+
# will print -> ["length", "size"]
40
+
```
41
+
42
+
##### Track report of all operations
43
+
```ruby
44
+
logic =JsonLogic::Evaluator.new
45
+
logic.apply(JSON.parse(RULE), DATA)
39
46
40
-
# output report of all operations
41
47
puts logic.tracker.report
42
48
43
49
# will print
44
-
#
45
-
# Criteria eligible: false
46
50
# LOGIC: 'and', RESULT = false
47
-
# DATA: 'age' data:51 >= expected:50, RESULT = true
48
-
# DATA: 'bmi' data:49 >= expected:50, RESULT = false
51
+
# DATA: 'length' data:20 >= expected:15, RESULT = true
52
+
# DATA: 'size' data:49 >= expected:50, RESULT = false
49
53
```
50
54
51
55
52
-
53
56
## Development
54
57
55
58
After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
@@ -58,12 +61,12 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
58
61
59
62
## Contributing
60
63
61
-
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/json-logic. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/[USERNAME]/json-logic/blob/master/CODE_OF_CONDUCT.md).
64
+
Bug reports and pull requests are welcome on GitHub at https://github.com/useful-libs/json-logic. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/useful-libs/json-logic/blob/main/CODE_OF_CONDUCT.md).
62
65
63
66
## License
64
67
65
68
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
66
69
67
70
## Code of Conduct
68
71
69
-
Everyone interacting in the Json::Logic project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/json-logic/blob/master/CODE_OF_CONDUCT.md).
72
+
Everyone interacting in the Json::Logic project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/useful-libs/json-logic/blob/master/CODE_OF_CONDUCT.md).
0 commit comments