Skip to content

Commit

Permalink
rename Result#event to Result#terminus for consistency.
Browse files Browse the repository at this point in the history
deprecate `Result#event`.
  • Loading branch information
apotonick committed Nov 15, 2023
1 parent 8fa7168 commit 6877f35
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 7 deletions.
20 changes: 13 additions & 7 deletions lib/trailblazer/operation/railway.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,26 @@ def self.pass! ; Activity::Right end
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)
# @param terminus The last emitted signal in a circuit is the end event/terminus.
def self.Result(terminus, options, *)
Result.new(terminus.kind_of?(End::Success), options, terminus)
end

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

@event = event
@terminus = terminus
end

attr_reader :event
def event
Activity::Deprecate.warn caller_locations[0], %(Using `Result#event` is deprecated, please use `Result#terminus`)
terminus
end

attr_reader :terminus
end

module End
Expand Down
38 changes: 38 additions & 0 deletions test/docs/result_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
require "test_helper"

module A
class DocsResultTest < Minitest::Spec
Memo = Class.new
module Memo::Operation
class Create < Trailblazer::Operation
step :validate, Output(:failure) => End(:validation_error)
step :save
include T.def_steps(:validate, :save)
end
end

it "exposes {#terminus}" do
result = Memo::Operation::Create.(seq: [])
assert_equal result.terminus.to_h.inspect, %({:semantic=>:success})

result = Memo::Operation::Create.(validate: false, seq: [])
assert_equal result.terminus.to_h.inspect, %({:semantic=>:validation_error})

result = Memo::Operation::Create.(save: false, seq: [])
assert_equal result.terminus.to_h.inspect, %({:semantic=>:failure})
end

it "deprecates Result#event" do
result = Memo::Operation::Create.(seq: [])
terminus = nil

_, warning = capture_io do
terminus = result.event
end
line_no = __LINE__ - 2

assert_equal warning, %{[Trailblazer] #{File.realpath(__FILE__)}:#{line_no} Using `Result#event` is deprecated, please use `Result#terminus`\n}
assert_equal terminus.to_h.inspect, %({:semantic=>:success})
end
end
end

0 comments on commit 6877f35

Please sign in to comment.