lib/floe/workflow/choice_rule/data.rb in floe-0.13.0 vs lib/floe/workflow/choice_rule/data.rb in floe-0.13.1
- old
+ new
@@ -2,10 +2,22 @@
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
+
+ attr_reader :variable, :compare_key, :value, :path
+
+ def initialize(_workflow, _name, payload)
+ super
+
+ @variable = parse_path("Variable", payload)
+ parse_compare_key
+ @value = path ? parse_path(compare_key, payload) : payload[compare_key]
+ end
+
def true?(context, input)
return presence_check(context, input) if compare_key == "IsPresent"
lhs = variable_value(context, input)
rhs = compare_value(context, input)
@@ -70,11 +82,11 @@
def is_present?(value) # rubocop:disable Naming/PredicateName
!value.nil?
end
def is_numeric?(value) # rubocop:disable Naming/PredicateName
- value.kind_of?(Integer) || value.kind_of?(Float)
+ value.kind_of?(Numeric)
end
def is_string?(value) # rubocop:disable Naming/PredicateName
value.kind_of?(String)
end
@@ -90,15 +102,28 @@
true
rescue TypeError, Date::Error
false
end
- def compare_key
- @compare_key ||= payload.keys.detect { |key| key.match?(/^(IsNull|IsPresent|IsNumeric|IsString|IsBoolean|IsTimestamp|String|Numeric|Boolean|Timestamp)/) }
+ def parse_compare_key
+ @compare_key = payload.keys.detect { |key| key.match?(/^(#{COMPARE_KEYS.join("|")})/) }
+ parser_error!("requires a compare key") unless compare_key
+
+ @path = compare_key.end_with?("Path")
end
def compare_value(context, input)
- compare_key.end_with?("Path") ? Path.value(payload[compare_key], context, input) : payload[compare_key]
+ path ? value.value(context, input) : value
+ end
+
+ def variable_value(context, input)
+ variable.value(context, input)
+ end
+
+ def parse_path(field_name, payload)
+ value = payload[field_name]
+ missing_field_error!(field_name) unless value
+ wrap_parser_error(field_name, value) { Path.new(value) }
end
end
end
end
end