Module: DecisionAgent::Dsl::Operators::DateArithmeticOperators
- Defined in:
- lib/decision_agent/dsl/operators/date_arithmetic_operators.rb
Overview
Handles date arithmetic operators: add_days, subtract_days, add_hours, subtract_hours, add_minutes, subtract_minutes
Class Method Summary collapse
-
.compare_date_result?(actual, target, params) ⇒ Boolean
Compare date result.
- .handle(op, actual_value, expected_value, context_hash, param_cache: nil, param_cache_mutex: nil) ⇒ Object
-
.parse_date_arithmetic_params(value, unit = :days, param_cache: nil, param_cache_mutex: nil) ⇒ Object
Parse date arithmetic parameters.
- .parse_date_arithmetic_params_impl(value, unit) ⇒ Object
Class Method Details
.compare_date_result?(actual, target, params) ⇒ Boolean
Compare date result
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 |
# File 'lib/decision_agent/dsl/operators/date_arithmetic_operators.rb', line 173 def self.compare_date_result?(actual, target, params) if params[:compare] case params[:compare].to_s when "eq", "==" (actual - target).abs < 1 when "gt", ">" actual > target when "lt", "<" actual < target when "gte", ">=" actual >= target when "lte", "<=" actual <= target else false end elsif params[:eq] (actual - target).abs < 1 elsif params[:gt] actual > target elsif params[:lt] actual < target elsif params[:gte] actual >= target elsif params[:lte] actual <= target else false end end |
.handle(op, actual_value, expected_value, context_hash, 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 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/decision_agent/dsl/operators/date_arithmetic_operators.rb', line 8 def self.handle(op, actual_value, expected_value, context_hash, param_cache: nil, param_cache_mutex: nil) case op when "add_days" # Adds days to a date and compares return false unless actual_value start_date = ConditionEvaluator.parse_date(actual_value) return false unless start_date params = parse_date_arithmetic_params(expected_value, :days, param_cache: param_cache, param_cache_mutex: param_cache_mutex) return false unless params result_date = start_date + (params[:days] * 86_400) target_date = if params[:target] == "now" Time.now else ConditionEvaluator.parse_date(ConditionEvaluator.get_nested_value(context_hash, params[:target])) end return false unless target_date compare_date_result?(result_date, target_date, params) when "subtract_days" # Subtracts days from a date and compares return false unless actual_value start_date = ConditionEvaluator.parse_date(actual_value) return false unless start_date params = parse_date_arithmetic_params(expected_value, :days, param_cache: param_cache, param_cache_mutex: param_cache_mutex) return false unless params result_date = start_date - (params[:days] * 86_400) target_date = if params[:target] == "now" Time.now else ConditionEvaluator.parse_date(ConditionEvaluator.get_nested_value(context_hash, params[:target])) end return false unless target_date compare_date_result?(result_date, target_date, params) when "add_hours" # Adds hours to a date and compares return false unless actual_value start_date = ConditionEvaluator.parse_date(actual_value) return false unless start_date params = parse_date_arithmetic_params(expected_value, :hours, param_cache: param_cache, param_cache_mutex: param_cache_mutex) return false unless params result_date = start_date + (params[:hours] * 3600) target_date = if params[:target] == "now" Time.now else ConditionEvaluator.parse_date(ConditionEvaluator.get_nested_value(context_hash, params[:target])) end return false unless target_date compare_date_result?(result_date, target_date, params) when "subtract_hours" # Subtracts hours from a date and compares return false unless actual_value start_date = ConditionEvaluator.parse_date(actual_value) return false unless start_date params = parse_date_arithmetic_params(expected_value, :hours, param_cache: param_cache, param_cache_mutex: param_cache_mutex) return false unless params result_date = start_date - (params[:hours] * 3600) target_date = if params[:target] == "now" Time.now else ConditionEvaluator.parse_date(ConditionEvaluator.get_nested_value(context_hash, params[:target])) end return false unless target_date compare_date_result?(result_date, target_date, params) when "add_minutes" # Adds minutes to a date and compares return false unless actual_value start_date = ConditionEvaluator.parse_date(actual_value) return false unless start_date params = parse_date_arithmetic_params(expected_value, :minutes, param_cache: param_cache, param_cache_mutex: param_cache_mutex) return false unless params result_date = start_date + (params[:minutes] * 60) target_date = if params[:target] == "now" Time.now else ConditionEvaluator.parse_date(ConditionEvaluator.get_nested_value(context_hash, params[:target])) end return false unless target_date compare_date_result?(result_date, target_date, params) when "subtract_minutes" # Subtracts minutes from a date and compares return false unless actual_value start_date = ConditionEvaluator.parse_date(actual_value) return false unless start_date params = parse_date_arithmetic_params(expected_value, :minutes, param_cache: param_cache, param_cache_mutex: param_cache_mutex) return false unless params result_date = start_date - (params[:minutes] * 60) target_date = if params[:target] == "now" Time.now else ConditionEvaluator.parse_date(ConditionEvaluator.get_nested_value(context_hash, params[:target])) end return false unless target_date compare_date_result?(result_date, target_date, params) end # Returns nil if not handled by this module end |
.parse_date_arithmetic_params(value, unit = :days, param_cache: nil, param_cache_mutex: nil) ⇒ Object
Parse date arithmetic parameters
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 |
# File 'lib/decision_agent/dsl/operators/date_arithmetic_operators.rb', line 134 def self.parse_date_arithmetic_params(value, unit = :days, param_cache: nil, param_cache_mutex: nil) return nil unless value.is_a?(Hash) # Normalize to hash (already a hash, but normalize keys) 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, "date_arithmetic_#{unit}") cached = cache[cache_key] return cached if cached mutex.synchronize do cache[cache_key] ||= parse_date_arithmetic_params_impl(normalized, unit) end end |
.parse_date_arithmetic_params_impl(value, unit) ⇒ Object
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 |
# File 'lib/decision_agent/dsl/operators/date_arithmetic_operators.rb', line 156 def self.parse_date_arithmetic_params_impl(value, unit) 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 |