Module: DecisionAgent::Dsl::Helpers::TemplateHelpers

Defined in:
lib/decision_agent/dsl/helpers/template_helpers.rb

Overview

Template expansion and mapping helpers for ConditionEvaluator

Class Method Summary collapse

Class Method Details

.apply_mapping(response_data, mapping, get_nested_value:) ⇒ Object



27
28
29
30
31
32
33
34
35
# File 'lib/decision_agent/dsl/helpers/template_helpers.rb', line 27

def self.apply_mapping(response_data, mapping, get_nested_value:)
  return {} unless response_data.is_a?(Hash)
  return {} unless mapping.is_a?(Hash)

  mapping.each_with_object({}) do |(source_key, target_key), result|
    source_value = get_nested_value.call(response_data, source_key.to_s)
    result[target_key.to_s] = source_value unless source_value.nil?
  end
end

.expand_template_params(params, context_hash, get_nested_value:) ⇒ Object



8
9
10
11
12
13
14
# File 'lib/decision_agent/dsl/helpers/template_helpers.rb', line 8

def self.expand_template_params(params, context_hash, get_nested_value:)
  return {} unless params.is_a?(Hash)

  params.transform_values do |value|
    expand_template_value(value, context_hash, get_nested_value: get_nested_value)
  end
end

.expand_template_value(value, context_hash, get_nested_value:) ⇒ Object



16
17
18
19
20
21
22
23
24
25
# File 'lib/decision_agent/dsl/helpers/template_helpers.rb', line 16

def self.expand_template_value(value, context_hash, get_nested_value:)
  return value unless value.is_a?(String)
  return value unless value.match?(/\{\{.*\}\}/)

  # Extract path from {{path}} syntax
  value.gsub(/\{\{([^}]+)\}\}/) do |_match|
    path = Regexp.last_match(1).strip
    get_nested_value.call(context_hash, path) || value
  end
end