-
Notifications
You must be signed in to change notification settings - Fork 8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implement If() macro #49
Open
richardboehme
wants to merge
2
commits into
trailblazer:master
Choose a base branch
from
webit-de:webit/if
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
module Trailblazer | ||
module Macro | ||
def self.If(condition, name: :default, id: Macro.id_for(condition, macro: :If, hint: condition), &block) | ||
unless block_given? | ||
raise ArgumentError, "If() requires a block" | ||
end | ||
|
||
block_activity, outputs = Macro.block_activity_for(nil, &block) | ||
success_output = outputs.find { |output| output.semantic == :success } | ||
state = Declarative::State( | ||
block_activity: [block_activity, {copy: Trailblazer::Declarative::State.method(:subclass)}], | ||
name: [name, {}], | ||
success_signal: [success_output.signal, {}] | ||
) | ||
|
||
task = Class.new(If) do | ||
extend Macro::Strategy::State | ||
initialize!(state) | ||
end | ||
|
||
merge = [ | ||
[Nested::Decider.new(condition), id: "If.compute_condition", prepend: "task_wrap.call_task"], | ||
] | ||
|
||
task_wrap_extension = Activity::TaskWrap::Extension::WrapStatic.new(extension: Activity::TaskWrap::Extension(*merge)) | ||
|
||
Activity::Railway.Subprocess(task).merge( | ||
id: id, | ||
extensions: [task_wrap_extension], | ||
) | ||
end | ||
end | ||
|
||
class If < Macro::Strategy | ||
def self.call((ctx, flow_options), **circuit_options) | ||
name = @state.get(:name) | ||
ctx[:"result.condition.#{name}"] = flow_options[:decision] | ||
|
||
if flow_options[:decision] | ||
Activity::Circuit::Runner.(block_activity, [ctx, flow_options], **circuit_options) | ||
else | ||
[@state.get(:success_signal), [ctx, flow_options]] | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,180 @@ | ||
require "test_helper" | ||
|
||
class IfMacroTest < Minitest::Spec | ||
class Song | ||
module Activity | ||
class Upload < Trailblazer::Activity::FastTrack | ||
step :model | ||
step If(:condition) { | ||
step :update # this changes the database. | ||
step :transfer # this might even break! | ||
} | ||
step :notify | ||
fail :log_error | ||
#~meths | ||
include T.def_steps(:model, :condition, :update, :transfer, :notify, :log_error) | ||
#~meths end | ||
end | ||
end | ||
end | ||
|
||
it do | ||
assert_invoke Song::Activity::Upload, condition: true, seq: "[:model, :condition, :update, :transfer, :notify]", expected_ctx_variables: { "result.condition.default": true } | ||
assert_invoke Song::Activity::Upload, condition: false, seq: "[:model, :condition, :notify]", expected_ctx_variables: { "result.condition.default": false } | ||
assert_invoke Song::Activity::Upload, condition: true, transfer: false, seq: "[:model, :condition, :update, :transfer, :log_error]", terminus: :failure, expected_ctx_variables: { "result.condition.default": true } | ||
end | ||
end | ||
|
||
class IfWithCustomNameTest < Minitest::Spec | ||
class Song | ||
module Activity | ||
class Upload < Trailblazer::Activity::FastTrack | ||
step If(:condition, name: :custom_name) { | ||
step :update # this changes the database. | ||
} | ||
#~meths | ||
include T.def_steps(:condition, :update) | ||
#~meths end | ||
end | ||
end | ||
end | ||
|
||
it do | ||
assert_invoke Song::Activity::Upload, condition: true, seq: "[:condition, :update]", expected_ctx_variables: { "result.condition.custom_name": true } | ||
assert_invoke Song::Activity::Upload, condition: false, seq: "[:condition]", expected_ctx_variables: { "result.condition.custom_name": false } | ||
end | ||
end | ||
|
||
class IfWithProcTest < Minitest::Spec | ||
class Song | ||
module Activity | ||
class Upload < Trailblazer::Activity::FastTrack | ||
step If(->(_ctx, condition:, **) { condition }) { | ||
step :update # this changes the database. | ||
} | ||
#~meths | ||
include T.def_steps(:update) | ||
#~meths end | ||
end | ||
end | ||
end | ||
|
||
it do | ||
assert_invoke Song::Activity::Upload, condition: true, seq: "[:update]", expected_ctx_variables: { "result.condition.default": true } | ||
assert_invoke Song::Activity::Upload, condition: false, seq: "[]", expected_ctx_variables: { "result.condition.default": false } | ||
end | ||
end | ||
|
||
class IfWithCallableTest < Minitest::Spec | ||
class Song | ||
module Activity | ||
class Upload < Trailblazer::Activity::FastTrack | ||
class Callable | ||
def self.call(_ctx, condition:, **) | ||
condition | ||
end | ||
end | ||
|
||
step If(Callable) { | ||
step :update # this changes the database. | ||
} | ||
#~meths | ||
include T.def_steps(:update) | ||
#~meths end | ||
end | ||
end | ||
end | ||
|
||
it do | ||
assert_invoke Song::Activity::Upload, condition: true, seq: "[:update]", expected_ctx_variables: { "result.condition.default": true } | ||
assert_invoke Song::Activity::Upload, condition: false, seq: "[]", expected_ctx_variables: { "result.condition.default": false } | ||
end | ||
end | ||
|
||
class IfWithNestedIfTest < Minitest::Spec | ||
class Song | ||
module Activity | ||
class Upload < Trailblazer::Activity::FastTrack | ||
step If(:condition) { | ||
step :update # this changes the database. | ||
step If(:nested_condition, name: :nested) { | ||
step :notify | ||
} | ||
step :finalize | ||
} | ||
#~meths | ||
include T.def_steps(:condition, :update, :nested_condition, :notify, :finalize) | ||
#~meths end | ||
end | ||
end | ||
end | ||
|
||
it do | ||
assert_invoke Song::Activity::Upload, condition: true, nested_condition: true, seq: "[:condition, :update, :nested_condition, :notify, :finalize]", expected_ctx_variables: { "result.condition.default": true, "result.condition.nested": true } | ||
assert_invoke Song::Activity::Upload, condition: false, seq: "[:condition]", expected_ctx_variables: { "result.condition.default": false } | ||
assert_invoke Song::Activity::Upload, condition: true, nested_condition: false, seq: "[:condition, :update, :nested_condition, :finalize]", expected_ctx_variables: { "result.condition.default": true, "result.condition.nested": false } | ||
end | ||
end | ||
|
||
class IfTracingTest < Minitest::Spec | ||
class Song | ||
module Activity | ||
class Upload < Trailblazer::Activity::FastTrack | ||
class DecideWhatToDo | ||
def self.call(*); end | ||
end | ||
|
||
def self.my_condition_handler(*); end | ||
|
||
step If(:condition) {} | ||
step If(DecideWhatToDo) {} | ||
step If(method(:my_condition_handler)) {} | ||
step If(->(*) {}, id: "proc.my_condition_handler") {} | ||
|
||
#~meths | ||
include T.def_steps(:condition) | ||
#~meths end | ||
end | ||
end | ||
end | ||
|
||
it do | ||
[ | ||
"If/condition", | ||
"If/IfTracingTest::Song::Activity::Upload::DecideWhatToDo", | ||
"If/method(:my_condition_handler)", | ||
"proc.my_condition_handler" | ||
].each do |id| | ||
assert_equal Trailblazer::Developer::Introspect.find_path(Song::Activity::Upload, [id])[0].id, id | ||
end | ||
|
||
assert_equal trace(Song::Activity::Upload, { seq: [], condition: false })[0], <<~SEQ.chomp | ||
TOP | ||
|-- Start.default | ||
|-- If/condition | ||
|-- If/IfTracingTest::Song::Activity::Upload::DecideWhatToDo | ||
|-- If/method(:my_condition_handler) | ||
|-- proc.my_condition_handler | ||
`-- End.success | ||
SEQ | ||
end | ||
end | ||
|
||
class IfWithoutBlockTest < Minitest::Spec | ||
it do | ||
exception = assert_raises ArgumentError do | ||
class Song | ||
module Activity | ||
class Upload < Trailblazer::Activity::FastTrack | ||
step If(:condition) | ||
|
||
#~meths | ||
include T.def_steps(:condition) | ||
#~meths end | ||
end | ||
end | ||
end | ||
end | ||
assert_equal "If() requires a block", exception.message | ||
end | ||
end |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is
ctx[:"result.condition.#{name}"]
important to keep?