json-logic-rb 0.2.0 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: bf7e4291a1fe0a56b1387ca35f03d239679f4d0c76333a3d0d0f3933efeead0b
4
- data.tar.gz: 134bb254ff81c389daa1d9527b9496d17d0417316143586ea8ade75c739f2657
3
+ metadata.gz: 810c50b59976cf832b8171f84696315690a47d99a894f65e7430b6a62f5effa0
4
+ data.tar.gz: d398558a21b6b194da078ba175f03349ddc5f8f17a51d939e0b554f267a9e15d
5
5
  SHA512:
6
- metadata.gz: 2a98a01a255cdce55c19940171b00fcf652e9735369e6316d747cbfc70108ed42c207d8beca244bf042b47f0a7aa978b962e523fed9662bc2d35970f5476d6b4
7
- data.tar.gz: f2a6bedd19099407dbd4398647553cbe9a341926b71b0cdd6c50f99fbe856015ff20a8408087e295c4a459dd13d2c2cdf360810b9af4ceff07e98e102e8012da
6
+ metadata.gz: a4fb7f21bfebbd7f852f62be1dc7acf4a9047aac1af3405b5d37e3c64fdc9b08b7c30b85a3168e1b64c716aefcf2526a6003b20ba5494ce2b8c55f1c5b01d225
7
+ data.tar.gz: 604c4a8adbe4b06fd130e0fcc07443a79899d2f1f777bf6b82da815fb9277fef5bff7c2c596329ce62042ece12d1258f0cb61fc997a43bf532546efb7881f618
data/README.md CHANGED
@@ -1,4 +1,4 @@
1
- [![jsonlogic core][src-core]](https://jsonlogic.com/tests.json) [![jsonlogic community][src-community]](https://github.com/json-logic/compat-tables/tree/main/suites) [![compliance 100%](https://img.shields.io/badge/compliance-100%25-brightgreen)](https://github.com/tavrelkate/json-logic-rb/actions/workflows/compliance.yml?query=branch%3Amain) <a href="https://rubygems.org/gems/json-logic-rb"><img alt="rubygems" src="https://img.shields.io/gem/v/json-logic-rb"></a> <a href="LICENSE"><img alt="license" src="https://img.shields.io/badge/license-MIT-informational"></a>
1
+ [![jsonlogic core][src-core]][core-tests] [![jsonlogic community][src-community]][community-suites] [![compliance 100%][badge-compliance]][ci-main] [![Build Status][badge-buildkite]][ci-main] [![rubygems][badge-rubygems]][rubygems] [![license][badge-license]][license]
2
2
 
3
3
  # json-logic-rb
4
4
 
@@ -8,12 +8,12 @@ Ruby implementation of [JsonLogic](https://jsonlogic.com/). Pure and extensible.
8
8
  - [What](#what)
9
9
  - [Install](#install)
10
10
  - [Quick start](#quick-start)
11
- - [Complience](#complience)
11
+ - [Compliance](#compliance)
12
12
  - [Supported Operations (Built-in)](#supported-operations-built-in)
13
13
  - [Adding Operations](#adding-operations)
14
14
  - [Enable JsonLogic Semantics (optional)](#enable-jsonlogic-semantics-optional)
15
15
  - [Parameters](#parameters)
16
- - [Proc / Lambda](#proc--lambda)
16
+ - [Proc and Lambda](#proc-and-lambda)
17
17
  - [Class](#class)
18
18
  - [Laziness](#laziness)
19
19
  - [1. Default Operations](#1-default-operations)
@@ -24,7 +24,7 @@ Ruby implementation of [JsonLogic](https://jsonlogic.com/). Pure and extensible.
24
24
  - [Truthiness](#truthiness)
25
25
  - [Security](#security)
26
26
  - [License](#license)
27
- - [Authors](#authors)
27
+ - [Maintainers](#maintainers)
28
28
 
29
29
  ---
30
30
 
@@ -75,11 +75,11 @@ JsonLogic.apply(rule, data)
75
75
 
76
76
 
77
77
 
78
- ## Complience
78
+ ## Compliance
79
79
 
80
80
  The JsonLogic specification provides test suites — concrete inputs with expected outputs that validate the implementation of operations. The specification comes in two variants:
81
- - [![jsonlogic core][src-core]](https://jsonlogic.com/tests.json) – [original JsonLogic website](https://jsonlogic.com/tests.json)
82
- - [![jsonlogic community][src-community]](https://github.com/json-logic/compat-tables/tree/main/suites) – [extensions built on top of the core](https://github.com/json-logic/compat-tables/tree/main/suites)
81
+ - [![jsonlogic core][src-core]](https://jsonlogic.com) – [original JsonLogic website](https://jsonlogic.com/tests.json)
82
+ - [![jsonlogic community][src-community]](https://github.com/json-logic) – [extensions built on top of the core](https://github.com/json-logic/compat-tables/tree/main/suites)
83
83
 
84
84
  The "extra" exists because "core" hasn't changed in years — and that’s fine, "core" is a solid, finished foundation. Think of it as v1, while "extra" is the v2+ evolution as there are no visible plans to change the original.
85
85
 
@@ -144,13 +144,22 @@ The JsonLogic specification provides test suites — concrete inputs with expect
144
144
 
145
145
  [src-core]: https://img.shields.io/badge/jsonlogic-core-2ea44f?style=flat-square
146
146
  [src-community]: https://img.shields.io/badge/jsonlogic--community-extra-0366d6?style=flat-square
147
+ [badge-compliance]: https://img.shields.io/badge/compliance-100%25-brightgreen
148
+ [badge-buildkite]: https://img.shields.io/github/actions/workflow/status/tavrelkate/json-logic-rb/compliance.yml?branch=main&label=build
149
+ [badge-rubygems]: https://img.shields.io/gem/v/json-logic-rb
150
+ [badge-license]: https://img.shields.io/badge/license-MIT-informational
151
+ [core-tests]: https://jsonlogic.com
152
+ [community-suites]: https://github.com/json-logic
153
+ [ci-main]: https://github.com/tavrelkate/json-logic-rb/actions/workflows/compliance.yml?query=branch%3Amain
154
+ [rubygems]: https://rubygems.org/gems/json-logic-rb
155
+ [license]: LICENSE
147
156
 
148
157
 
149
158
  ## Adding Operations
150
159
 
151
160
  Don’t expect JsonLogic to include every specialized operation. It’s intentionally small and not a programming language. It will never do everything.
152
161
 
153
- Before you reach for a custom solution, see if you can express your logic using the [§Supported Operations (Builtin)](https://www.google.com/search?q=%23supported-operations-built-in). Often, a simple change in perspective is all you need to get the job done with what's already there.
162
+ Before you reach for a custom solution, see if you can express your logic using the [§Supported Operations (Built-in)](#supported-operations-built-in). Often, a simple change in perspective is all you need to get the job done with what's already there.
154
163
 
155
164
  If that doesn't cut it, adding a custom operation is straightforward. Keep it simple: start with a Proc or a Lambda. If needed – promote it to a Class.
156
165
 
@@ -172,7 +181,7 @@ Operator function use a consistent call shape:
172
181
  ->((string, prefix), data) { string.to_s.start_with?(prefix.to_s) }
173
182
  ```
174
183
 
175
- ### Proc / Lambda
184
+ ### Proc and Lambda
176
185
 
177
186
  Pick the Operation type.
178
187
 
@@ -33,7 +33,12 @@ module JsonLogic
33
33
  return rule.transform_values { |value| evaluate(value, data) }
34
34
  end
35
35
 
36
- args = op_class.values_only? ? Array.wrap(evaluate(raw_args, data)) : raw_args
36
+ if op_class.values_only?
37
+ evaluated = evaluate(raw_args, data)
38
+ args = evaluated.is_a?(Array) ? evaluated : [evaluated]
39
+ else
40
+ args = raw_args
41
+ end
37
42
  begin
38
43
  result = op_class.new.call(args, data)
39
44
  raise JsonLogic::NaNError.new if result.is_a?(Float) && (result.nan? || result.infinite?)
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module JsonLogic
4
+ class UnknownOperatorError < Error
5
+ DEFAULT_MESSAGE = "Unknown Operator"
6
+ end
7
+ end
@@ -10,5 +10,5 @@ class JsonLogic::Operations::Exists < JsonLogic::Operation
10
10
 
11
11
  private
12
12
 
13
- def normalize_path(args, data) = args.is_a?(Array) ? Array.wrap(args).map { |part| JsonLogic.apply(part, data) } : JsonLogic.apply(args, data)
13
+ def normalize_path(args, data) = args.is_a?(Array) ? args.as_array.map { |part| JsonLogic.apply(part, data) } : JsonLogic.apply(args, data)
14
14
  end
@@ -6,6 +6,6 @@ class JsonLogic::Operations::Missing < JsonLogic::Operation
6
6
  def self.name = "missing"
7
7
 
8
8
  def call(args, data)
9
- Array.wrap(args).select { |key| JsonLogic::Tree.new(data).dig(key).nil? }
9
+ args.as_array.select { |key| JsonLogic::Tree.new(data).dig(key).nil? }
10
10
  end
11
11
  end
@@ -7,7 +7,7 @@ class JsonLogic::Operations::MissingSome < JsonLogic::Operation
7
7
 
8
8
  def call((min_ok, list), data)
9
9
  json = JsonLogic::Tree.new(data)
10
- keys = Array.wrap(list)
10
+ keys = list.as_array
11
11
  missing = keys.select { |key| json.dig(key).nil? }
12
12
  (keys.size - missing.size) >= min_ok ? [] : missing
13
13
  end
@@ -6,7 +6,7 @@ class JsonLogic::Operations::Try < JsonLogic::LazyOperation
6
6
  def self.name = "try"
7
7
 
8
8
  def call(arguments, data)
9
- arguments = Array.wrap(arguments)
9
+ arguments = arguments.as_array
10
10
  raise JsonLogic::InvalidArgumentsError.new if arguments.empty?
11
11
 
12
12
  state = { data: data, error: nil }
@@ -84,6 +84,12 @@ module JsonLogic
84
84
  def to_bool
85
85
  JsonLogic::Semantics.truthy?(self)
86
86
  end
87
+
88
+ def as_array = [self]
89
+ end
90
+
91
+ refine Array do
92
+ def as_array = self
87
93
  end
88
94
 
89
95
  [String, Integer, Float, NilClass, Array, TrueClass, FalseClass].each do |klass|
@@ -1,3 +1,3 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- module JsonLogic; VERSION = '0.2.0'; end
3
+ module JsonLogic; VERSION = '0.3.0'; end
data/lib/json_logic.rb CHANGED
@@ -6,7 +6,6 @@ require_relative 'json_logic/errors/error'
6
6
  require_relative 'json_logic/errors/logic_error'
7
7
  require_relative 'json_logic/errors/invalid_arguments_error'
8
8
  require_relative 'json_logic/errors/nan_error'
9
- require_relative 'json_logic/ext/array'
10
9
  require_relative 'json_logic/tree'
11
10
  require_relative 'json_logic/scope'
12
11
  require_relative 'json_logic/operation'
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: json-logic-rb
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tavrel Kate
8
8
  bindir: bin
9
9
  cert_chain: []
10
- date: 2026-02-18 00:00:00.000000000 Z
10
+ date: 2026-09-06 00:00:00.000000000 Z
11
11
  dependencies: []
12
12
  description: Ruby implementation of JsonLogic. JsonLogic rules are JSON trees. The
13
13
  engine walks that tree and returns a Ruby value. Full compliance with both core
@@ -25,7 +25,7 @@ files:
25
25
  - lib/json_logic/errors/invalid_arguments_error.rb
26
26
  - lib/json_logic/errors/logic_error.rb
27
27
  - lib/json_logic/errors/nan_error.rb
28
- - lib/json_logic/ext/array.rb
28
+ - lib/json_logic/errors/unknown_operator_error.rb
29
29
  - lib/json_logic/lazy_operation.rb
30
30
  - lib/json_logic/operation.rb
31
31
  - lib/json_logic/operations/add.rb
@@ -1,5 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- class Array
4
- def self.wrap(value) = value.is_a?(Array) ? value : [value]
5
- end