From 8a473932e3a6e75bceb011322cd4531e3060387c Mon Sep 17 00:00:00 2001 From: Keenan Brock Date: Tue, 23 Jul 2024 20:51:38 -0400 Subject: [PATCH] More granular compare_key and determine path at initialization time --- lib/floe/workflow/choice_rule/data.rb | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/lib/floe/workflow/choice_rule/data.rb b/lib/floe/workflow/choice_rule/data.rb index 7714c317..1e004191 100644 --- a/lib/floe/workflow/choice_rule/data.rb +++ b/lib/floe/workflow/choice_rule/data.rb @@ -4,9 +4,14 @@ module Floe class Workflow class ChoiceRule class Data < Floe::Workflow::ChoiceRule - COMPARE_KEYS = %w[IsNull IsPresent IsNumeric IsString IsBoolean IsTimestamp String Numeric Boolean Timestamp].freeze + TYPES = ["String", "Numeric", "Boolean", "Timestamp", "Present", "Null"].freeze + COMPARES = ["Equals", "LessThan", "GreaterThan", "LessThanEquals", "GreaterThanEquals", "Matches"].freeze + # e.g.: (Is)(String), (Is)(Present) + TYPE_CHECK = /^(Is)(#{TYPES.join("|")})$/.freeze + # e.g.: (String)(LessThan)(Path), (Numeric)(GreaterThanEquals)() + OPERATION = /^(#{(TYPES - %w[Null Present]).join("|")})(#{COMPARES.join("|")})(Path)?$/.freeze - attr_reader :variable, :compare_key, :value, :path + attr_reader :variable, :compare_key, :type, :value, :path def initialize(_workflow, _name, payload) super @@ -109,10 +114,13 @@ def is_timestamp?(value, predicate = true) # rubocop:enable Style/OptionalBooleanParameter def parse_compare_key - @compare_key = payload.keys.detect { |key| key.match?(/^(#{COMPARE_KEYS.join("|")})/) } + @compare_key = payload.keys.detect { |key| key.match?(TYPE_CHECK) || key.match?(OPERATION) } parser_error!("requires a compare key") unless compare_key - @path = compare_key.end_with?("Path") + @type, _operator, @path = OPERATION.match(compare_key)&.captures + # TYPE_CHECK doesn't match this regex, so @path = @type = nil + # @path.nil? means this the compare_value will always be a static value (true or false) + # @type.nil? means we won't type check the variable or compare value end def compare_value(context, input)