Module: DecisionAgent::Dsl::Helpers::UtilityHelpers
- Defined in:
- lib/decision_agent/dsl/helpers/utility_helpers.rb
Overview
Utility helpers for ConditionEvaluator
Class Method Summary collapse
- .comparable?(val1, val2) ⇒ Boolean
- .epsilon_equal?(value1, value2, epsilon = 1e-10) ⇒ Boolean
- .get_nested_value(hash, key_path, get_cached_path:) ⇒ Object
- .string_operator?(actual_value, expected_value) ⇒ Boolean
Class Method Details
.comparable?(val1, val2) ⇒ Boolean
24 25 26 27 28 29 30 31 32 33 |
# File 'lib/decision_agent/dsl/helpers/utility_helpers.rb', line 24 def self.comparable?(val1, val2) # Both are numeric - allow comparison between different numeric types # (e.g., Integer and Float are comparable in Ruby) return true if val1.is_a?(Numeric) && val2.is_a?(Numeric) # Both are strings - require exact same type return val1.instance_of?(val2.class) if val1.is_a?(String) && val2.is_a?(String) false end |
.epsilon_equal?(value1, value2, epsilon = 1e-10) ⇒ Boolean
35 36 37 |
# File 'lib/decision_agent/dsl/helpers/utility_helpers.rb', line 35 def self.epsilon_equal?(value1, value2, epsilon = 1e-10) (value1 - value2).abs < epsilon end |
.get_nested_value(hash, key_path, get_cached_path:) ⇒ Object
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/decision_agent/dsl/helpers/utility_helpers.rb', line 8 def self.get_nested_value(hash, key_path, get_cached_path:) keys = get_cached_path.call(key_path) keys.reduce(hash) do |memo, key| return nil unless memo.is_a?(Hash) # OPTIMIZE: try symbol first (most common), then string # Check key existence first to avoid double lookup key_sym = key.to_sym if memo.key?(key_sym) memo[key_sym] elsif memo.key?(key) memo[key] end end end |
.string_operator?(actual_value, expected_value) ⇒ Boolean
39 40 41 |
# File 'lib/decision_agent/dsl/helpers/utility_helpers.rb', line 39 def self.string_operator?(actual_value, expected_value) actual_value.is_a?(String) && expected_value.is_a?(String) end |