-
Notifications
You must be signed in to change notification settings - Fork 215
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 ensure #206
base: master
Are you sure you want to change the base?
Added ensure #206
Conversation
I don't why but the CI is not triggered... |
6c82935
to
734a4b2
Compare
You can achieve the same behaviour using around-hook and regular Example with exxception: class Test
include Interactor
around do |interactor|
interactor.call
ensure
puts "inside around/ensure"
end
def call
puts 'Inside #call. Before excepton.'
raise "Something went wrong"
puts 'Inside #call. After exception.'
end
end Output: > Test.call
Inside #call. Before excepton.
inside around/ensure
RuntimeError: Something went wrong Example with class Test
include Interactor
around do |interactor|
interactor.call
ensure
puts "inside around/ensure"
end
def call
puts 'Inside #call. Before excepton.'
context.fail!(error: "Something went wrong")
puts 'Inside #call. After exception.'
end
end Output: > Test.call
Inside #call. Before excepton.
inside around/ensure
=> #<Interactor::Context error="Something went wrong"> |
The beauty of this gem is in its simplicity. It allows you to make almost everything you want on top of it. module TrackCurrentInteractorClass
extend ActiveSupport::Concern
included do
before do
context._current_interactor_class = self
end
end
end
class Test
include Interactor
include TrackCurrentInteractorClass
def call
puts "Interactor: #{context._current_interactor_class}"
end
end Output: > Test.call
Interactor: #<Test:0x000055779a9dc788>
=> #<Interactor::Context _current_interactor_class=#<Test:0x000055779a9dc788 @context=#<Interactor::Context ...>>> |
This is a great example. Maybe this PR can be updated to simply add that example to the README? I needed this "ensure" behavior and was disappointed it wasn't in the "hooks" section of the README. |
@mainameiz sorry for the late response, I think your opinion is subjective. At the library point of view, you have documentation, tests and make easier the integrations (provided by community) then if you move this responsibility to the consumers each consumer needs to implement their own implementation and tests (redundant code). |
Thank you for contributing this and I'm sorry it has taken so long to get eyes on it. Could you share a more concrete example of how This PR also suffers from it including changes to GitHub Actions and added unrelated features ( |
_current_interactor_class
in the context.