Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,38 @@
All notable changes to this project will be documented in this file.


## [0.3.0] - 2026-09-06
### Fixed
- Fix `Array.wrap` monkeypatch ([#21](https://github.com/tavrelkate/json-logic-rb/issues/21)).
Before, requiring this gem defined `Array.wrap` globally on the core `Array` class.
Had conflict with ActiveSupport's with different `nil` handling (`[]` vs `[nil]`). Whichever loaded last silently won.

```ruby
require 'json_logic'

Array.respond_to?(:wrap)
# <= 0.2.0 => true
```

Now the gem no longer touches `Array` globally. JsonLogic still wrap a single raw value into a array without dropping any elements (specially `nil`), but that rule now lives inside internal `JsonLogic::Semantics` instead of on core `Array`.

```ruby
require 'json_logic'

Array.respond_to?(:wrap)
# 0.3.0 => false
```

ActiveSupport's own `Array.wrap` is no longer shadowed:

```ruby
require "active_support/core_ext/array/wrap"
require 'json_logic'

Array.wrap(nil)
# 0.3.0 => [] (ActiveSupport's behavior)
```

## [0.2.0] - 2026-02-17
### Added
- Add community-extra operators: `try`, `throw`, `exists`, `val`, and `??` (`coalesce`).
Expand Down
1 change: 0 additions & 1 deletion lib/json_logic.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
require_relative 'json_logic/errors/logic_error'
require_relative 'json_logic/errors/invalid_arguments_error'
require_relative 'json_logic/errors/nan_error'
require_relative 'json_logic/ext/array'
require_relative 'json_logic/tree'
require_relative 'json_logic/scope'
require_relative 'json_logic/operation'
Expand Down
7 changes: 6 additions & 1 deletion lib/json_logic/engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,12 @@ def evaluate(rule, data = nil)
return rule.transform_values { |value| evaluate(value, data) }
end

args = op_class.values_only? ? Array.wrap(evaluate(raw_args, data)) : raw_args
if op_class.values_only?
evaluated = evaluate(raw_args, data)
args = evaluated.is_a?(Array) ? evaluated : [evaluated]
else
args = raw_args
end
begin
result = op_class.new.call(args, data)
raise JsonLogic::NaNError.new if result.is_a?(Float) && (result.nan? || result.infinite?)
Expand Down
5 changes: 0 additions & 5 deletions lib/json_logic/ext/array.rb

This file was deleted.

2 changes: 1 addition & 1 deletion lib/json_logic/operations/exists.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ def call(args, data) = JsonLogic::Tree.new(data).exists?(normalize_path(args, da

private

def normalize_path(args, data) = args.is_a?(Array) ? Array.wrap(args).map { |part| JsonLogic.apply(part, data) } : JsonLogic.apply(args, data)
def normalize_path(args, data) = args.is_a?(Array) ? args.as_array.map { |part| JsonLogic.apply(part, data) } : JsonLogic.apply(args, data)
end
2 changes: 1 addition & 1 deletion lib/json_logic/operations/missing.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ class JsonLogic::Operations::Missing < JsonLogic::Operation
def self.name = "missing"

def call(args, data)
Array.wrap(args).select { |key| JsonLogic::Tree.new(data).dig(key).nil? }
args.as_array.select { |key| JsonLogic::Tree.new(data).dig(key).nil? }
end
end
2 changes: 1 addition & 1 deletion lib/json_logic/operations/missing_some.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ def self.name = "missing_some"

def call((min_ok, list), data)
json = JsonLogic::Tree.new(data)
keys = Array.wrap(list)
keys = list.as_array
missing = keys.select { |key| json.dig(key).nil? }
(keys.size - missing.size) >= min_ok ? [] : missing
end
Expand Down
2 changes: 1 addition & 1 deletion lib/json_logic/operations/try.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class JsonLogic::Operations::Try < JsonLogic::LazyOperation
def self.name = "try"

def call(arguments, data)
arguments = Array.wrap(arguments)
arguments = arguments.as_array
raise JsonLogic::InvalidArgumentsError.new if arguments.empty?

state = { data: data, error: nil }
Expand Down
6 changes: 6 additions & 0 deletions lib/json_logic/semantics.rb
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,12 @@ def !@
def to_bool
JsonLogic::Semantics.truthy?(self)
end

def as_array = [self]
end

refine Array do
def as_array = self
end

[String, Integer, Float, NilClass, Array, TrueClass, FalseClass].each do |klass|
Expand Down
2 changes: 1 addition & 1 deletion lib/json_logic/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
# frozen_string_literal: true

module JsonLogic; VERSION = '0.2.0'; end
module JsonLogic; VERSION = '0.3.0'; end
Loading