Module: DecisionAgent::Dsl::Helpers::ParameterParsingHelpers

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

Overview

Parameter parsing helpers for ConditionEvaluator

Class Method Summary collapse

Class Method Details

.parse_atan2_params(value) ⇒ Object



65
66
67
68
69
70
71
72
73
74
# File 'lib/decision_agent/dsl/helpers/parameter_parsing_helpers.rb', line 65

def self.parse_atan2_params(value)
  normalized = Operators::Base.normalize_params_to_hash(value, %i[y result])
  return nil unless normalized.is_a?(Hash)

  y = normalized[:y] || normalized["y"]
  result = normalized[:result] || normalized["result"]
  return nil unless y && !result.nil?

  { y: y, result: result }
end

.parse_compound_interest_params(value) ⇒ Object



155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/decision_agent/dsl/helpers/parameter_parsing_helpers.rb', line 155

def self.parse_compound_interest_params(value)
  return nil unless value.is_a?(Hash)

  rate = value["rate"] || value[:rate]
  periods = value["periods"] || value[:periods]
  return nil unless rate.is_a?(Numeric) && periods.is_a?(Numeric) && periods.positive?

  {
    rate: rate.to_f,
    periods: periods.to_f,
    result: value["result"] || value[:result],
    compare: value["compare"] || value[:compare],
    threshold: value["threshold"] || value[:threshold]
  }
end

.parse_date_arithmetic_params(value, unit = :days) ⇒ Object



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/decision_agent/dsl/helpers/parameter_parsing_helpers.rb', line 121

def self.parse_date_arithmetic_params(value, unit = :days)
  return nil unless value.is_a?(Hash)

  unit_value = value[unit.to_s] || value[unit]
  return nil unless unit_value.is_a?(Numeric)

  {
    unit => unit_value.to_f,
    target: value["target"] || value[:target] || "now",
    compare: value["compare"] || value[:compare],
    eq: value["eq"] || value[:eq],
    gt: value["gt"] || value[:gt],
    lt: value["lt"] || value[:lt],
    gte: value["gte"] || value[:gte],
    lte: value["lte"] || value[:lte]
  }
end

.parse_duration_params(value) ⇒ Object



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/decision_agent/dsl/helpers/parameter_parsing_helpers.rb', line 104

def self.parse_duration_params(value)
  return nil unless value.is_a?(Hash)

  end_field = value["end"] || value[:end]
  return nil unless end_field

  {
    end: end_field.to_s,
    min: value["min"] || value[:min],
    max: value["max"] || value[:max],
    gt: value["gt"] || value[:gt],
    lt: value["lt"] || value[:lt],
    gte: value["gte"] || value[:gte],
    lte: value["lte"] || value[:lte]
  }
end

.parse_future_value_params(value) ⇒ Object



185
186
187
# File 'lib/decision_agent/dsl/helpers/parameter_parsing_helpers.rb', line 185

def self.parse_future_value_params(value)
  parse_present_value_params(value)
end

.parse_gcd_lcm_params(value) ⇒ Object



76
77
78
79
80
81
82
83
84
85
# File 'lib/decision_agent/dsl/helpers/parameter_parsing_helpers.rb', line 76

def self.parse_gcd_lcm_params(value)
  normalized = Operators::Base.normalize_params_to_hash(value, %i[other result])
  return nil unless normalized.is_a?(Hash)

  other = normalized[:other] || normalized["other"]
  result = normalized[:result] || normalized["result"]
  return nil unless other && !result.nil?

  { other: other, result: result }
end

.parse_join_params(value) ⇒ Object



193
194
195
196
197
198
199
200
201
202
# File 'lib/decision_agent/dsl/helpers/parameter_parsing_helpers.rb', line 193

def self.parse_join_params(value)
  return nil unless value.is_a?(Hash)

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

.parse_modulo_params(value, param_cache:, param_cache_mutex:) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
# File 'lib/decision_agent/dsl/helpers/parameter_parsing_helpers.rb', line 31

def self.parse_modulo_params(value, param_cache:, param_cache_mutex:)
  normalized = Operators::Base.normalize_params_to_hash(value, %i[divisor remainder])
  cache_key = Operators::Base.normalize_param_cache_key(normalized, "modulo")

  cached = param_cache[cache_key]
  return cached if cached

  param_cache_mutex.synchronize do
    param_cache[cache_key] ||= parse_modulo_params_impl(normalized)
  end
end

.parse_modulo_params_impl(value) ⇒ Object



43
44
45
46
47
48
49
50
51
52
# File 'lib/decision_agent/dsl/helpers/parameter_parsing_helpers.rb', line 43

def self.parse_modulo_params_impl(value)
  normalized = Operators::Base.normalize_params_to_hash(value, %i[divisor remainder])
  return nil unless normalized.is_a?(Hash)

  divisor = normalized[:divisor] || normalized["divisor"]
  remainder = normalized[:remainder] || normalized["remainder"]
  return nil unless divisor && !remainder.nil?

  { divisor: divisor, remainder: remainder }
end

.parse_moving_window_params(value) ⇒ Object



139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/decision_agent/dsl/helpers/parameter_parsing_helpers.rb', line 139

def self.parse_moving_window_params(value)
  return nil unless value.is_a?(Hash)

  window = value["window"] || value[:window]
  return nil unless window.is_a?(Numeric) && window.positive?

  {
    window: window.to_i,
    threshold: value["threshold"] || value[:threshold],
    gt: value["gt"] || value[:gt],
    lt: value["lt"] || value[:lt],
    gte: value["gte"] || value[:gte],
    lte: value["lte"] || value[:lte]
  }
end

.parse_payment_params(value) ⇒ Object



189
190
191
# File 'lib/decision_agent/dsl/helpers/parameter_parsing_helpers.rb', line 189

def self.parse_payment_params(value)
  parse_compound_interest_params(value)
end

.parse_percentile_params(value) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/decision_agent/dsl/helpers/parameter_parsing_helpers.rb', line 87

def self.parse_percentile_params(value)
  return nil unless value.is_a?(Hash)

  percentile = value["percentile"] || value[:percentile]
  return nil unless percentile.is_a?(Numeric) && percentile >= 0 && percentile <= 100

  {
    percentile: percentile.to_f,
    threshold: value["threshold"] || value[:threshold],
    gt: value["gt"] || value[:gt],
    lt: value["lt"] || value[:lt],
    gte: value["gte"] || value[:gte],
    lte: value["lte"] || value[:lte],
    eq: value["eq"] || value[:eq]
  }
end

.parse_power_params(value) ⇒ Object



54
55
56
57
58
59
60
61
62
63
# File 'lib/decision_agent/dsl/helpers/parameter_parsing_helpers.rb', line 54

def self.parse_power_params(value)
  normalized = Operators::Base.normalize_params_to_hash(value, %i[exponent result])
  return nil unless normalized.is_a?(Hash)

  exponent = normalized[:exponent] || normalized["exponent"]
  result = normalized[:result] || normalized["result"]
  return nil unless exponent && !result.nil?

  { exponent: exponent, result: result }
end

.parse_present_value_params(value) ⇒ Object



171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/decision_agent/dsl/helpers/parameter_parsing_helpers.rb', line 171

def self.parse_present_value_params(value)
  return nil unless value.is_a?(Hash)

  rate = value["rate"] || value[:rate]
  periods = value["periods"] || value[:periods]
  return nil unless rate.is_a?(Numeric) && periods.is_a?(Numeric) && periods.positive?

  {
    rate: rate.to_f,
    periods: periods.to_f,
    result: value["result"] || value[:result]
  }
end

.parse_range(value, param_cache:, param_cache_mutex:) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
# File 'lib/decision_agent/dsl/helpers/parameter_parsing_helpers.rb', line 8

def self.parse_range(value, param_cache:, param_cache_mutex:)
  normalized = Operators::Base.normalize_params_to_hash(value, %i[min max])
  cache_key = Operators::Base.normalize_param_cache_key(normalized, "range")

  cached = param_cache[cache_key]
  return cached if cached

  param_cache_mutex.synchronize do
    param_cache[cache_key] ||= parse_range_impl(normalized)
  end
end

.parse_range_impl(value) ⇒ Object



20
21
22
23
24
25
26
27
28
29
# File 'lib/decision_agent/dsl/helpers/parameter_parsing_helpers.rb', line 20

def self.parse_range_impl(value)
  normalized = Operators::Base.normalize_params_to_hash(value, %i[min max])
  return nil unless normalized.is_a?(Hash)

  min = normalized[:min] || normalized["min"]
  max = normalized[:max] || normalized["max"]
  return nil unless min && max

  { min: min, max: max }
end