@@ -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 } ?" } \
10+ . merge ( COMPARES . each_with_object ( { } ) { |op , a | a [ op ] = :"op_#{ op . downcase } ?" } ) . 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,22 +112,20 @@ 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 } ?"
115+ @type , @operator , @path = match_values . captures
115116 @compare_predicate = parse_predicate ( type )
116117 break
117118 # e.g. (Is)(String)
118119 elsif ( match_value = TYPE_CHECK . match ( key ) )
119120 @compare_key = key
120- _operator , type = match_value . captures
121+ _is , @operator = match_value . captures
121122 # type: nil means no runtime type checking.
122123 @type = @path = nil
123- @operation = :"is_#{ type . downcase } ?"
124124 @compare_predicate = parse_predicate ( "Boolean" )
125125 break
126126 end
127127 end
128- parser_error! ( "requires a compare key" ) if compare_key . nil? || operation . nil?
128+ parser_error! ( "requires a compare key" ) if compare_key . nil? || operator . nil?
129129 end
130130
131131 # parse predicate at initialization time
@@ -180,7 +180,7 @@ def fetch_path(field_name, field_path, context, input)
180180 # if we have runtime checking, check against that type
181181 # otherwise assume checking a TYPE_CHECK predicate and check against Boolean
182182 def correct_type? ( value , data_type )
183- send ( :"is_ #{ data_type . downcase } ?" , value )
183+ send ( OPERATIONS [ data_type ] , value )
184184 end
185185 end
186186 end
0 commit comments