@@ -6,12 +6,14 @@ class ChoiceRule
66 class Data < Floe ::Workflow ::ChoiceRule
77 TYPES = [ "String" , "Numeric" , "Boolean" , "Timestamp" , "Present" , "Null" ] . freeze
88 COMPARES = [ "Equals" , "LessThan" , "GreaterThan" , "LessThanEquals" , "GreaterThanEquals" , "Matches" ] . freeze
9+ OPERATIONS = TYPES . each_with_object ( { } ) { |dt , a | a [ dt ] = "is_#{ dt . downcase } ?" . to_sym } \
10+ . merge ( COMPARES . each_with_object ( { } ) { |op , a | a [ op ] = "op_#{ op . downcase } ?" . to_sym } ) . freeze
911 # e.g.: (Is)(String), (Is)(Present)
1012 TYPE_CHECK = /^(Is)(#{ TYPES . join ( "|" ) } )$/
1113 # e.g.: (String)(LessThan)(Path), (Numeric)(GreaterThanEquals)()
1214 OPERATION = /^(#{ ( TYPES - %w[ Null Present ] ) . join ( "|" ) } )(#{ COMPARES . join ( "|" ) } )(Path)?$/
1315
14- attr_reader :variable , :compare_key , :operation , :type , :compare_predicate , :path
16+ attr_reader :variable , :compare_key , :operator , :type , :compare_predicate , :path
1517
1618 def initialize ( _workflow , _name , payload )
1719 super
@@ -25,7 +27,7 @@ def true?(context, input)
2527
2628 lhs = variable_value ( context , input )
2729 rhs = compare_value ( context , input )
28- send ( operation , lhs , rhs )
30+ send ( OPERATIONS [ operator ] , lhs , rhs )
2931 end
3032
3133 private
@@ -110,23 +112,21 @@ def parse_compare_key
110112 # e.g. (String)(GreaterThan)(Path)
111113 if ( match_values = OPERATION . match ( key ) )
112114 @compare_key = key
113- @type , operator , @path = match_values . captures
114- @operation = "op_#{ operator . downcase } ?" . to_sym
115+ @type , @operator , @path = match_values . captures
115116 @compare_predicate = parse_predicate ( type )
116117 break
117118 end
118119 # e.g. (Is)(String)
119120 if ( match_value = TYPE_CHECK . match ( key ) )
120121 @compare_key = key
121- _operator , type = match_value . captures
122+ _is , @operator = match_value . captures
122123 # type: nil means no runtime type checking.
123124 @type = @path = nil
124- @operation = "is_#{ type . downcase } ?" . to_sym
125125 @compare_predicate = parse_predicate ( "Boolean" )
126126 break
127127 end
128128 end
129- parser_error! ( "requires a compare key" ) if compare_key . nil? || operation . nil?
129+ parser_error! ( "requires a compare key" ) if compare_key . nil? || operator . nil?
130130 end
131131
132132 # parse predicate at initilization time
@@ -173,7 +173,7 @@ def fetch_path(field_name, field_path, context, input)
173173 # if we have runtime checking, check against that type
174174 # otherwise assume checking a TYPE_CHECK predicate and check against Boolean
175175 def correct_type? ( value , data_type )
176- send ( "is_ #{ data_type . downcase } ?" . to_sym , value )
176+ send ( OPERATIONS [ data_type ] , value )
177177 end
178178 end
179179 end
0 commit comments