Skip to content

Commit

Permalink
Improve IntrinsicFuncion.new
Browse files Browse the repository at this point in the history
  • Loading branch information
agrare committed Aug 9, 2023
1 parent cafd499 commit 164f68a
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 23 deletions.
20 changes: 9 additions & 11 deletions lib/floe/workflow/intrinsic_function.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,15 @@ class IntrinsicFunction
INTRINSIC_FUNCTION_REGEX = /^(?<module>\w+)\.(?<function>\w+)\((?<args>.*)\)$/.freeze

class << self
def klass(payload)
def new(*args)
if self == Floe::Workflow::IntrinsicFunction
detect_class(*args).new(*args)
else
super
end
end

private def detect_class(payload)
function_module_name, function_name =
payload.match(INTRINSIC_FUNCTION_REGEX)
.named_captures
Expand All @@ -23,16 +31,6 @@ def klass(payload)
def value(payload, context, input = {})
new(payload).value(context, input)
end

private alias_method :orig_new, :new

def new(*args)
if self == Floe::Workflow::IntrinsicFunction
klass(*args).new(*args)
else
orig_new(*args)
end
end
end

attr_reader :args
Expand Down
20 changes: 8 additions & 12 deletions spec/workflow/intrinsic_function_spec.rb
Original file line number Diff line number Diff line change
@@ -1,23 +1,19 @@
RSpec.describe Floe::Workflow::IntrinsicFunction do
describe ".klass" do
it "returns the intrinsic function class" do
payload = "States.StringToJson('{\"number\": 20}')"
expect(described_class.klass(payload)).to eq(Floe::Workflow::IntrinsicFunctions::States::StringToJson)
end

it "raises an exception for an invalid intrinsic function" do
payload = "States.MyFirstFunction()"
expect { described_class.klass(payload) }.to raise_error(NotImplementedError)
end
end

describe "#initialize" do
let(:payload) { "States.StringToJson()" }

it "returns an instance of the intrinsic function" do
expect(described_class.new(payload)).to be_kind_of(Floe::Workflow::IntrinsicFunctions::States::StringToJson)
end

context "with an invalid intrinsic function" do
let(:payload) { "States.MyFirstFunction()" }

it "raises an exception for an invalid intrinsic function" do
expect { described_class.new(payload) }.to raise_error(NotImplementedError)
end
end

context "with no arguments" do
it "parses args as empty" do
function = described_class.new(payload)
Expand Down

0 comments on commit 164f68a

Please sign in to comment.