Class: JsonLogic::Evaluator

Inherits:
Object
  • Object
show all
Includes:
Trackable
Defined in:
lib/json_logic/evaluator.rb

Constant Summary

Constants included from Trackable

Trackable::COMPLEX_OPERATORS

Instance Attribute Summary

Attributes included from Trackable

#tracker

Instance Method Summary collapse

Methods included from Trackable

#commit_rule_result!, #init_tracker

Instance Method Details

#apply(rules, data = {}) ⇒ Object



7
8
9
10
11
12
13
14
15
16
# File 'lib/json_logic/evaluator.rb', line 7

def apply(rules, data = {})
  return rules.map { |val| apply(val, data) } if rules.is_a?(Array)
  return rules unless rules.is_a?(Hash)

  operator = rules.keys[0]
  init_tracker(operator)
  values = operator == 'map' ? [] :  apply(rules[operator], data)

  operators(operator, values, rules, data)
end

#extract_vars(rules, vars = []) ⇒ Array

This method traverses ‘rules` (expected as a nested data structure composed of Hash and Array objects) to extract values associated with the key ’var’.

It’s a recursive method that digs into the structure to find ‘var’.

If ‘rules` is a Hash, it iterates through each key-value pair:

  • If the key is ‘var’, it appends the value to ‘vars`.

  • If the key is not ‘var’, it recurses into the value (if it’s an Array or Hash).

If ‘rules` is an Array, it iterates through each element, which should be a Hash or Array, and recurses through it.

‘vars`, is an optional accumulator Array to hold collected variables. It starts as an empty array if not provided.

and arrays that we want to extract values from.

Parameters:

  • rules (Hash, Array)

    a data structure composed of nested Hashes

  • vars (Array) (defaults to: [])

    an optional accumulator array to hold collected variables.

Returns:

  • (Array)

    an array of extracted variable values.



54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/json_logic/evaluator.rb', line 54

def extract_vars(rules, vars = [])
  if rules.is_a?(Hash)
    rules.each do |key, value|
      key == 'var' ? vars << value : extract_vars(value, vars)
    end
    return vars
  end

  rules.each { |rule| extract_vars(rule, vars) } and return vars if rules.is_a?(Array)

  vars
end

#fetch_var_values(rules, var_name, values = []) ⇒ Array

This method recursively searches through the ‘rules` data structure (composed of nested Hashes and Arrays) to find all values associated with a specified variable name.

If ‘rules` is an Array:

  • It checks if the first element of the Array has the specified variable. If so, it adds the second element of the Array (presumed to be the value of the variable) to ‘values`.

  • If not, it recurses through each element of the Array.

If ‘rules` is a Hash, it recurses through each value in the Hash.

‘values` is an optional accumulator Array used to collect the found variable values.

Arrays to be searched through.

Parameters:

  • rules (Hash, Array)

    a data structure composed of nested Hashes and

  • var_name (String, Symbol)

    the name of the variable we want to find the values for.

  • values (Array) (defaults to: [])

    an optional accumulator array to collect found values.

Returns:

  • (Array)

    an array of all values associated with the specified variable in ‘rules`.



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/json_logic/evaluator.rb', line 90

def fetch_var_values(rules, var_name, values = [])
  if rules.is_a?(Array)
    return values << rules[1] if rule_has_var?(rules.first, var_name)

    rules.each { |rule| fetch_var_values(rule, var_name, values) }
    return values
  end

  if rules.is_a?(Hash)
    rules.each_value { |rule| fetch_var_values(rule, var_name, values) }
    return values
  end

  values
end

#operators(operator, values, rules, data) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/json_logic/evaluator.rb', line 18

def operators(operator, values, rules, data)
  case operator
  when 'var' then get_var_value(data, *values)
  when 'missing' then missing(data, *values)
  when 'missing_some' then missing_some(data, *values)
  when 'map' then json_logic_map(data, *rules[operator])
  else
    raise("Unrecognized operation #{operator}") unless OPERATIONS.key?(operator)

    execute_operation(operator, rules, data, *values)
  end
end