Skip to content

Commit

Permalink
Introduce IsType data rules
Browse files Browse the repository at this point in the history
  • Loading branch information
kbrock committed May 31, 2024
1 parent ec82091 commit 604fc35
Show file tree
Hide file tree
Showing 9 changed files with 104 additions and 35 deletions.
6 changes: 6 additions & 0 deletions lib/floe.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@
require_relative "floe/workflow/choice_rule/not"
require_relative "floe/workflow/choice_rule/or"
require_relative "floe/workflow/choice_rule/and"
require_relative "floe/workflow/choice_rule/is_boolean"
require_relative "floe/workflow/choice_rule/is_null"
require_relative "floe/workflow/choice_rule/is_numeric"
require_relative "floe/workflow/choice_rule/is_present"
require_relative "floe/workflow/choice_rule/is_string"
require_relative "floe/workflow/choice_rule/is_timestamp"
require_relative "floe/workflow/choice_rule/data"
require_relative "floe/workflow/context"
require_relative "floe/workflow/path"
Expand Down
12 changes: 12 additions & 0 deletions lib/floe/workflow/choice_rule.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,18 @@ def build(payload)
Floe::Workflow::ChoiceRule::And.new(payload, build_children(sub_payloads))
elsif (sub_payloads = payload["Or"])
Floe::Workflow::ChoiceRule::Or.new(payload, build_children(sub_payloads))
elsif payload["IsNull"]
Floe::Workflow::ChoiceRule::IsNull.new(payload)
elsif payload["IsPresent"]
Floe::Workflow::ChoiceRule::IsPresent.new(payload)
elsif payload["IsNumeric"]
Floe::Workflow::ChoiceRule::IsNumeric.new(payload)
elsif payload["IsString"]
Floe::Workflow::ChoiceRule::IsString.new(payload)
elsif payload["IsBoolean"]
Floe::Workflow::ChoiceRule::IsBoolean.new(payload)
elsif payload["IsTimestamp"]
Floe::Workflow::ChoiceRule::IsTimestamp.new(payload)
else
Floe::Workflow::ChoiceRule::Data.new(payload)
end
Expand Down
35 changes: 0 additions & 35 deletions lib/floe/workflow/choice_rule/data.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,6 @@ def true?(context, input)
return false unless valid?(lhs)

case compare_key
when "IsNull" then is_null?(lhs)
when "IsPresent" then is_present?(lhs)
when "IsNumeric" then is_numeric?(lhs)
when "IsString" then is_string?(lhs)
when "IsBoolean" then is_boolean?(lhs)
when "IsTimestamp" then is_timestamp?(lhs)
when "StringEquals", "StringEqualsPath",
"NumericEquals", "NumericEqualsPath",
"BooleanEquals", "BooleanEqualsPath",
Expand Down Expand Up @@ -58,38 +52,9 @@ def true?(context, input)
private

def valid?(value)
!value.nil? || %w[IsNull IsPresent].include?(compare_key)
end

def is_null?(value) # rubocop:disable Naming/PredicateName
value.nil?
end

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)
end

def is_string?(value) # rubocop:disable Naming/PredicateName
value.kind_of?(String)
end

def is_boolean?(value) # rubocop:disable Naming/PredicateName
[true, false].include?(value)
end

def is_timestamp?(value) # rubocop:disable Naming/PredicateName
require "date"

DateTime.rfc3339(value)
true
rescue TypeError, Date::Error
false
end

def compare_value(context, input)
compare_key.end_with?("Path") ? Path.value(payload[compare_key], context, input) : payload[compare_key]
end
Expand Down
14 changes: 14 additions & 0 deletions lib/floe/workflow/choice_rule/is_boolean.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# frozen_string_literal: true

module Floe
class Workflow
class ChoiceRule
class IsBoolean < Floe::Workflow::ChoiceRule
def true?(context, input)
value = variable_value(context, input)
[true, false].include?(value)
end
end
end
end
end
13 changes: 13 additions & 0 deletions lib/floe/workflow/choice_rule/is_null.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# frozen_string_literal: true

module Floe
class Workflow
class ChoiceRule
class IsNull < Floe::Workflow::ChoiceRule
def true?(context, input)
variable_value(context, input).nil?
end
end
end
end
end
14 changes: 14 additions & 0 deletions lib/floe/workflow/choice_rule/is_numeric.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# frozen_string_literal: true

module Floe
class Workflow
class ChoiceRule
class IsNumeric < Floe::Workflow::ChoiceRule
def true?(context, input)
value = variable_value(context, input)
value.kind_of?(Integer) || value.kind_of?(Float)
end
end
end
end
end
13 changes: 13 additions & 0 deletions lib/floe/workflow/choice_rule/is_present.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# frozen_string_literal: true

module Floe
class Workflow
class ChoiceRule
class IsPresent < Floe::Workflow::ChoiceRule
def true?(context, input)
!variable_value(context, input).nil?
end
end
end
end
end
13 changes: 13 additions & 0 deletions lib/floe/workflow/choice_rule/is_string.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# frozen_string_literal: true

module Floe
class Workflow
class ChoiceRule
class IsString < Floe::Workflow::ChoiceRule
def true?(context, input)
variable_value(context, input).kind_of?(String)
end
end
end
end
end
19 changes: 19 additions & 0 deletions lib/floe/workflow/choice_rule/is_timestamp.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# frozen_string_literal: true

module Floe
class Workflow
class ChoiceRule
class IsTimestamp < Floe::Workflow::ChoiceRule
def true?(context, input)
value = variable_value(context, input)
require "date"

DateTime.rfc3339(value)
true
rescue TypeError, Date::Error
false
end
end
end
end
end

0 comments on commit 604fc35

Please sign in to comment.