@@ -4,59 +4,36 @@ module Floe
44 class Workflow
55 class ChoiceRule
66 class Data < Floe ::Workflow ::ChoiceRule
7- COMPARE_KEYS = %w[ IsNull IsPresent IsNumeric IsString IsBoolean IsTimestamp String Numeric Boolean Timestamp ] . freeze
7+ COMPARE_RULE = /^(String|Numeric|Boolean|Timestamp)(Equals|GreaterThan|LessThan|GreaterThanEquals|LessThanEquals|Matches)(Path)?$/ . freeze
8+ OPERATIONS = { "Equals" => :== , "LessThan" => :< , "GreaterThan" => :> , "LessThanEquals" => :<= , "GreaterThanEquals" => :>= , "Matches" => "match?" } . freeze
89
9- attr_reader :compare_key
10+ attr_reader :operation , :ref , :path
1011
1112 def initialize ( *)
1213 super
13- @compare_key = payload . keys . detect { |key | key . match? ( /^(#{ COMPARE_KEYS . join ( "|" ) } )/ ) }
14+ payload . each . detect do |key , value |
15+ _type , operator , @path = COMPARE_RULE . match ( key ) &.captures
16+ @operation = OPERATIONS [ operator ]
17+ @ref = value
18+ operator # detect aborts once an operator is found
19+ end
1420
15- raise Floe ::InvalidWorkflowError , "Data-test Expression Choice Rule must have a compare key" if @compare_key . nil?
21+ raise Floe ::InvalidWorkflowError , "Data-test Expression Choice Rule must have a valid compare key" unless operation
1622 end
1723
1824 def true? ( context , input )
1925 lhs = variable_value ( context , input )
20- rhs = compare_value ( context , input )
21- return false unless valid? ( lhs )
26+ return false if lhs . nil?
2227
23- case compare_key
24- when "StringEquals" , "StringEqualsPath" ,
25- "NumericEquals" , "NumericEqualsPath" ,
26- "BooleanEquals" , "BooleanEqualsPath" ,
27- "TimestampEquals" , "TimestampEqualsPath"
28- lhs == rhs
29- when "StringLessThan" , "StringLessThanPath" ,
30- "NumericLessThan" , "NumericLessThanPath" ,
31- "TimestampLessThan" , "TimestampLessThanPath"
32- lhs < rhs
33- when "StringGreaterThan" , "StringGreaterThanPath" ,
34- "NumericGreaterThan" , "NumericGreaterThanPath" ,
35- "TimestampGreaterThan" , "TimestampGreaterThanPath"
36- lhs > rhs
37- when "StringLessThanEquals" , "StringLessThanEqualsPath" ,
38- "NumericLessThanEquals" , "NumericLessThanEqualsPath" ,
39- "TimestampLessThanEquals" , "TimestampLessThanEqualsPath"
40- lhs <= rhs
41- when "StringGreaterThanEquals" , "StringGreaterThanEqualsPath" ,
42- "NumericGreaterThanEquals" , "NumericGreaterThanEqualsPath" ,
43- "TimestampGreaterThanEquals" , "TimestampGreaterThanEqualsPath"
44- lhs >= rhs
45- when "StringMatches"
46- lhs . match? ( Regexp . escape ( rhs ) . gsub ( '\*' , '.*?' ) )
47- else
48- raise Floe ::InvalidWorkflowError , "Invalid choice [#{ compare_key } ]"
49- end
28+ rhs = compare_value ( context , input )
29+ rhs = Regexp . escape ( rhs ) . gsub ( '\*' , '.*?' ) if operation == "match?"
30+ lhs . send ( operation , rhs )
5031 end
5132
5233 private
5334
54- def valid? ( value )
55- !value . nil?
56- end
57-
5835 def compare_value ( context , input )
59- compare_key . end_with? ( "Path" ) ? Path . value ( payload [ compare_key ] , context , input ) : payload [ compare_key ]
36+ path ? Path . value ( ref , context , input ) : ref
6037 end
6138 end
6239 end
0 commit comments