Skip to content
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

added custom outputs binary state methods #64

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@ test/version_tmp
tmp
/**/*.log
.rubocop-*yml
.idea
2 changes: 1 addition & 1 deletion lib/trailblazer/operation/public_call.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def call_with_public_interface(options, flow_options, invoke_class: Activity::Ta
)

# Result is successful if the activity ended with an End event derived from Railway::End::Success.
Operation::Railway::Result(signal, ctx, flow_options)
Operation::Railway::Result(self, signal, ctx, flow_options)
end

# This interface is used for all nested OPs (and the outer-most, too).
Expand Down
14 changes: 9 additions & 5 deletions lib/trailblazer/operation/railway.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,20 @@ def self.fail_fast!; Activity::FastTrack::FailFast end
def self.pass_fast!; Activity::FastTrack::PassFast end
# @param options Context
# @param end_event The last emitted signal in a circuit is usually the end event.
def self.Result(end_event, options, *)
Result.new(end_event.kind_of?(End::Success), options, end_event)
def self.Result(activity, end_event, options, *)
Result.new(activity, options, end_event)
end

# The Railway::Result knows about its binary state, the context (data), and the last event in the circuit.
class Result < Result # Operation::Result
def initialize(success, data, event)
super(success, data)

def initialize(activity, data, event)
super(event.kind_of?(End::Success), data)
@event = event

# generate [:success?, :pass_fast?, :fail_fast?, :failure?, :<custom_output>?] methods
activity.to_h[:outputs].each do |output|
define_singleton_method("#{output[:semantic]}?") { event.to_h[:semantic] == output[:semantic] }
end
end

attr_reader :event
Expand Down
36 changes: 36 additions & 0 deletions test/custom_output_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
require "test_helper"

class CustomOutputTest < Minitest::Spec
# #success passes fast.
class Execute < Trailblazer::Operation
UsePaypal = Class.new(Trailblazer::Activity::Signal)

step :find_provider, Output(UsePaypal, :paypal) => End(:paypal)
step :charge_creditcard

def find_provider(ctx, params:, **)
return true unless params[:provider] == :paypal
UsePaypal
end

def charge_creditcard(ctx, **)
ctx[:charged] = true
end
end

describe "if a custom output is used in an operation" do
it "adds `<custom_output>?` method to the result object which returns true if the operation takes the correspondent track" do
result = Execute.(params: {provider: :paypal})
assert result.paypal?
assert !result.success?
assert !result.failure?
refute_respond_to result, :papa_johns?

result = Execute.(params: {provider: :not_paypal})
assert !result.paypal?
assert result.success?
assert !result.failure?
end
end

end
6 changes: 3 additions & 3 deletions test/result_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ class RailwayResultTest < Minitest::Spec
Result = Trailblazer::Operation::Railway::Result
Success = Trailblazer::Operation::Railway::End::Success

let(:event) { Success.new(semantic: nil) }
let(:success) { Result.new(true, {"x" => String}, event) }
let(:event) { Success.new(semantic: :success) }
let(:success) { Result.new(Create, {"x" => String}, event) }

it { success.success?.must_equal true }
it { success.failure?.must_equal false }
Expand All @@ -20,7 +20,7 @@ class RailwayResultTest < Minitest::Spec
#---
# inspect
it { success.inspect.must_equal %{<Result:true {\"x\"=>String} >} }
it { Result.new(true, {"x" => true, "y" => 1, "z" => 2}, event).inspect("z", "y").must_equal %{<Result:true [2, 1] >} }
it { Result.new(Create, {"x" => true, "y" => 1, "z" => 2}, event).inspect("z", "y").must_equal %{<Result:true [2, 1] >} }

class Create < Trailblazer::Operation
pass :call
Expand Down