Class: Floe::Workflow::ChoiceRule::Data

Inherits:
Floe::Workflow::ChoiceRule show all
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

Attributes inherited from Floe::Workflow::ChoiceRule

#children, #name, #next, #payload

Instance Method Summary collapse

Methods inherited from Floe::Workflow::ChoiceRule

build, build_children

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_keyObject (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_predicateObject (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

#operatorObject (readonly)

Returns the value of attribute operator.



16
17
18
# File 'lib/floe/workflow/choice_rule/data.rb', line 16

def operator
  @operator
end

#pathObject (readonly)

Returns the value of attribute path.



16
17
18
# File 'lib/floe/workflow/choice_rule/data.rb', line 16

def path
  @path
end

#typeObject (readonly)

Returns the value of attribute type.



16
17
18
# File 'lib/floe/workflow/choice_rule/data.rb', line 16

def type
  @type
end

#variableObject (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)

Parameters:

  • context (Context)

    The workflow execution context

  • input (Hash)

    The current state input

Returns:

  • (Boolean)

    true if the rule evaluate to true



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