Module: DecisionAgent::Dsl::Operators::Base
- Defined in:
- lib/decision_agent/dsl/operators/base.rb
Overview
Base utilities shared across all operator modules
Class Method Summary collapse
-
.compare_aggregation_result(actual, expected) ⇒ Object
Compare aggregation result with expected value (supports hash with comparison operators).
-
.epsilon_equal?(lhs, rhs, epsilon = 1e-10) ⇒ Boolean
Epsilon comparison for floating point numbers.
-
.normalize_param_cache_key(value, prefix) ⇒ Object
Normalize parameter value for cache key generation.
-
.normalize_params_to_hash(value, keys) ⇒ Object
Normalize params to hash - converts arrays to hashes for better performance If value is an array and keys are provided, convert to hash If value is already a hash, normalize keys to symbols.
Class Method Details
.compare_aggregation_result(actual, expected) ⇒ Object
Compare aggregation result with expected value (supports hash with comparison operators)
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/decision_agent/dsl/operators/base.rb', line 47 def self.compare_aggregation_result(actual, expected) if expected.is_a?(Hash) result = true result &&= (actual >= expected[:min]) if expected[:min] result &&= (actual <= expected[:max]) if expected[:max] result &&= (actual > expected[:gt]) if expected[:gt] result &&= (actual < expected[:lt]) if expected[:lt] result &&= (actual >= expected[:gte]) if expected[:gte] result &&= (actual <= expected[:lte]) if expected[:lte] result &&= (actual == expected[:eq]) if expected[:eq] result else actual == expected end end |
.epsilon_equal?(lhs, rhs, epsilon = 1e-10) ⇒ Boolean
Epsilon comparison for floating point numbers
64 65 66 |
# File 'lib/decision_agent/dsl/operators/base.rb', line 64 def self.epsilon_equal?(lhs, rhs, epsilon = 1e-10) (lhs - rhs).abs < epsilon end |
.normalize_param_cache_key(value, prefix) ⇒ Object
Normalize parameter value for cache key generation
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/decision_agent/dsl/operators/base.rb', line 29 def self.normalize_param_cache_key(value, prefix) case value when Array "#{prefix}:#{value.inspect}" when Hash # Normalize keys to symbols and sort for consistent cache keys normalized = value.each_with_object({}) do |(k, v), h| key = k.is_a?(String) ? k.to_sym : k h[key] = v end sorted_keys = normalized.keys.sort "#{prefix}:#{sorted_keys.map { |k| "#{k}:#{normalized[k]}" }.join(',')}" else "#{prefix}:#{value.inspect}" end end |
.normalize_params_to_hash(value, keys) ⇒ Object
Normalize params to hash - converts arrays to hashes for better performance If value is an array and keys are provided, convert to hash If value is already a hash, normalize keys to symbols
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/decision_agent/dsl/operators/base.rb', line 11 def self.normalize_params_to_hash(value, keys) if value.is_a?(Array) && value.size == keys.size # Convert array to hash for better performance with large params keys.each_with_index.with_object({}) do |(key, idx), hash| hash[key] = value[idx] end elsif value.is_a?(Hash) # Normalize hash keys to symbols for consistency value.each_with_object({}) do |(k, v), h| key = k.is_a?(String) ? k.to_sym : k h[key] = v end else value end end |