Class: Floe::Workflow::ChoiceRule::Data
- Inherits:
-
Floe::Workflow::ChoiceRule
- Object
- Floe::Workflow::ChoiceRule
- Floe::Workflow::ChoiceRule::Data
- Defined in:
- lib/floe/workflow/choice_rule/data.rb
Constant Summary collapse
- TYPES =
["String", "Numeric", "Boolean", "Timestamp", "Present", "Null"].freeze
- COMPARES =
["Equals", "LessThan", "GreaterThan", "LessThanEquals", "GreaterThanEquals", "Matches"].freeze
- OPERATIONS =
TYPES.each_with_object({}) { |dt, a| a[dt] = :"is_#{dt.downcase}?" } .merge(COMPARES.each_with_object({}) { |op, a| a[op] = :"op_#{op.downcase}?" }).freeze
- TYPE_CHECK =
e.g.: (Is)(String), (Is)(Present)
/^Is(#{Regexp.union(TYPES)})$/- OPERATION =
e.g.: (String)(LessThan)(Path), (Numeric)(GreaterThanEquals)()
/^(#{Regexp.union(TYPES - %w[Null Present])})(#{Regexp.union(COMPARES)})(Path)?$/
Instance Attribute Summary collapse
-
#compare_key ⇒ Object
readonly
Returns the value of attribute compare_key.
-
#compare_predicate ⇒ Object
readonly
Returns the value of attribute compare_predicate.
-
#operator ⇒ Object
readonly
Returns the value of attribute operator.
-
#path ⇒ Object
readonly
Returns the value of attribute path.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
-
#variable ⇒ Object
readonly
Returns the value of attribute variable.
Attributes inherited from Floe::Workflow::ChoiceRule
#children, #name, #next, #payload
Instance Method Summary collapse
-
#initialize(_workflow, _name, payload) ⇒ Data
constructor
A new instance of Data.
-
#true?(context, input) ⇒ Boolean
Evaluate whether this rule is true for the given context and input (runtime).
Methods inherited from Floe::Workflow::ChoiceRule
Methods included from ValidationMixin
included, #invalid_field_error!, #missing_field_error!, #parser_error!, #runtime_field_error!, #wrap_parser_error
Constructor Details
#initialize(_workflow, _name, payload) ⇒ Data
Returns a new instance of Data.
18 19 20 21 22 23 |
# File 'lib/floe/workflow/choice_rule/data.rb', line 18 def initialize(_workflow, _name, payload) super @variable = parse_path("Variable") parse_compare_key end |
Instance Attribute Details
#compare_key ⇒ Object (readonly)
Returns the value of attribute compare_key.
16 17 18 |
# File 'lib/floe/workflow/choice_rule/data.rb', line 16 def compare_key @compare_key end |
#compare_predicate ⇒ Object (readonly)
Returns the value of attribute compare_predicate.
16 17 18 |
# File 'lib/floe/workflow/choice_rule/data.rb', line 16 def compare_predicate @compare_predicate end |
#operator ⇒ Object (readonly)
Returns the value of attribute operator.
16 17 18 |
# File 'lib/floe/workflow/choice_rule/data.rb', line 16 def operator @operator end |
#path ⇒ Object (readonly)
Returns the value of attribute path.
16 17 18 |
# File 'lib/floe/workflow/choice_rule/data.rb', line 16 def path @path end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
16 17 18 |
# File 'lib/floe/workflow/choice_rule/data.rb', line 16 def type @type end |
#variable ⇒ Object (readonly)
Returns the value of attribute variable.
16 17 18 |
# File 'lib/floe/workflow/choice_rule/data.rb', line 16 def variable @variable end |
Instance Method Details
#true?(context, input) ⇒ Boolean
Evaluate whether this rule is true for the given context and input (runtime)
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/floe/workflow/choice_rule/data.rb', line 30 def true?(context, input) # Payload pattern is: {"Variable": $lhs, $operator: $rhs} # Example: # # {"Variable": "$.foo", "IsNumeric": true} # lhs = input["$.foo"] # rhs = true # is_numeric?(lhs, rhs) # # {"Variable": "$.foo", "GreaterThanString": "aaa"} # lhs = input["$.foo"] # rhs = "aaa" # op_greaterthan?(lhs, rhs) # # {"Variable": "$.foo", "GreaterThanNumericPath": "$.bar"} # lhs = input["$.foo"] # rhs = input["$.bar"] # op_greaterthan?(lhs, rhs) # # NOTE: IsPresent works a little differently as lhs might raise a PathError. # See the exception handler below. This is why we process the rhs before the lhs. rhs = compare_value(context, input) lhs = variable_value(context, input) send(OPERATIONS[operator], lhs, rhs) rescue Floe::PathError # For IsPresent, we can expect the lhs to not be present in some cases, # This throws a PathError. We handle that special case here. # Example: # # {"Variable": "$.foo", "IsPresent": false} # lhs = input["$.foo"], but variable is not present. (The variable lookup threw PathError) # rhs = false return is_present?(:not_present, rhs) if operator == "Present" # for non "IsPresent" checks, share that lhs or rhs is not found. raise end |