Module: DecisionAgent::Dsl::Operators::StringAggregations

Defined in:
lib/decision_agent/dsl/operators/string_aggregations.rb

Overview

Handles string aggregation operators: join, length

Class Method Summary collapse

Class Method Details

.compare_length_result(actual, expected) ⇒ Object

Compare length result



78
79
80
# File 'lib/decision_agent/dsl/operators/string_aggregations.rb', line 78

def self.compare_length_result(actual, expected)
  ConditionEvaluator.compare_length_result(actual, expected)
end

.handle(op, actual_value, expected_value, param_cache: nil, param_cache_mutex: nil) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/decision_agent/dsl/operators/string_aggregations.rb', line 8

def self.handle(op, actual_value, expected_value, param_cache: nil, param_cache_mutex: nil)
  case op
  when "join"
    # Joins array of strings with separator
    return false unless actual_value.is_a?(Array)
    return false if actual_value.empty?

    string_array = actual_value.map(&:to_s)
    params = parse_join_params(expected_value, param_cache: param_cache, param_cache_mutex: param_cache_mutex)
    return false unless params

    joined = string_array.join(params[:separator])

    if params[:result]
      joined == params[:result]
    elsif params[:contains]
      joined.include?(params[:contains])
    else
      false
    end

  when "length"
    # Gets length of string or array
    return false if actual_value.nil?

    length_value = if actual_value.is_a?(String) || actual_value.is_a?(Array)
                     actual_value.length
                   else
                     return false
                   end

    compare_length_result(length_value, expected_value)
  end
  # Returns nil if not handled by this module
end

.parse_join_params(value, param_cache: nil, param_cache_mutex: nil) ⇒ Object

Parse join parameters



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/decision_agent/dsl/operators/string_aggregations.rb', line 45

def self.parse_join_params(value, param_cache: nil, param_cache_mutex: nil)
  return nil unless value.is_a?(Hash)

  normalized = Base.normalize_params_to_hash(value, [])

  cache = param_cache
  mutex = param_cache_mutex
  if cache.nil? || mutex.nil?
    cache = ConditionEvaluator.instance_variable_get(:@param_cache)
    mutex = ConditionEvaluator.instance_variable_get(:@param_cache_mutex)
  end

  cache_key = Base.normalize_param_cache_key(normalized, "join")
  cached = cache[cache_key]
  return cached if cached

  mutex.synchronize do
    cache[cache_key] ||= parse_join_params_impl(normalized)
  end
end

.parse_join_params_impl(value) ⇒ Object



66
67
68
69
70
71
72
73
74
75
# File 'lib/decision_agent/dsl/operators/string_aggregations.rb', line 66

def self.parse_join_params_impl(value)
  separator = value[:separator] || value["separator"]
  return nil unless separator

  {
    separator: separator.to_s,
    result: value[:result] || value["result"],
    contains: value[:contains] || value["contains"]
  }
end