-
-
Notifications
You must be signed in to change notification settings - Fork 11
Allow #step to be given a name, surfaced to #on_failure
#42
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
Open
timriley
wants to merge
1
commit into
main
Choose a base branch
from
optional-step-names
base: main
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
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 hidden or 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 hidden or 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 hidden or 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
88 changes: 88 additions & 0 deletions
88
lib/dry/operation/class_context/failure_hook_dispatcher.rb
This file contains hidden or 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,88 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| require "dry/operation/errors" | ||
|
|
||
| module Dry | ||
| class Operation | ||
| module ClassContext | ||
| # Dispatches a failure result to an operation instance's `#on_failure` | ||
| # hook, supporting several signatures: | ||
| # | ||
| # def on_failure(failure) | ||
| # def on_failure(failure, method_name) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wonder if we could find a more descriptive name instead of |
||
| # def on_failure(failure, step_name:) | ||
| # def on_failure(failure, method_name:) | ||
| # def on_failure(failure, step_name:, method_name:) | ||
| # | ||
| # @api private | ||
| module FailureHookDispatcher | ||
| FAILURE_HOOK_METHOD_NAME = :on_failure | ||
|
|
||
| SUPPORTED_KWARGS = %i[step_name method_name].freeze | ||
| private_constant :SUPPORTED_KWARGS | ||
|
|
||
| class << self | ||
| def call(instance, method_name:, step_name:, result:) | ||
| return unless result.is_a?(Dry::Monads::Result::Failure) | ||
|
|
||
| hook = lookup_hook(instance) | ||
| return unless hook | ||
|
|
||
| invoke_hook(hook, failure: result.failure, step_name: step_name, method_name: method_name) | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def lookup_hook(instance) | ||
| return unless (instance.methods + instance.private_methods).include?(FAILURE_HOOK_METHOD_NAME) | ||
|
|
||
| instance.method(FAILURE_HOOK_METHOD_NAME) | ||
| end | ||
|
|
||
| # Dispatches to the hook based on its positional params arity. | ||
| # | ||
| # - Arity of 1: modern form. The hook receives `failure`, plus whichever of `step_name:` | ||
| # and `method_name:` is explicitly accepted. When it accepts neither, the slice is | ||
| # empty and `**{}` makes this equivalent to a plain `hook.(failure)` call. | ||
| # - Arity of 2: legacy form. The second positional is always `method_name`. Reject any | ||
| # kwargs here, because mixing the two styles would be ambiguous about which identifier | ||
| # the second positional carries. | ||
| # - Any other arity is unsupported and rejected. | ||
| def invoke_hook(hook, failure:, step_name:, method_name:) | ||
| positional, accepted_kwargs = parse_signature(hook) | ||
|
|
||
| case positional | ||
| when 1 | ||
| kwargs = {step_name:, method_name:}.slice(*accepted_kwargs) | ||
| hook.(failure, **kwargs) | ||
| when 2 | ||
| raise FailureHookArityError.new(hook: hook) unless accepted_kwargs.empty? | ||
|
|
||
| hook.(failure, method_name) | ||
| else | ||
| raise FailureHookArityError.new(hook: hook) | ||
| end | ||
| end | ||
|
|
||
| # Returns [positional_count, kwargs_array] from the failure hook method's `parameters`. | ||
| def parse_signature(hook) | ||
| positional = 0 | ||
| kwargs = [] | ||
|
|
||
| hook.parameters.each do |type, name| | ||
| case type | ||
| when :req, :opt then positional += 1 | ||
| when :key, :keyreq then kwargs << name | ||
| else raise FailureHookArityError.new(hook: hook) | ||
| end | ||
| end | ||
|
|
||
| raise FailureHookArityError.new(hook: hook) if (kwargs - SUPPORTED_KWARGS).any? | ||
|
|
||
| [positional, kwargs] | ||
| end | ||
| end | ||
| end | ||
| end | ||
| end | ||
| end | ||
This file contains hidden or 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 hidden or 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
Oops, something went wrong.
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.
Do you think keeping the value as the first positional parameter would read better?
I’m thinking about something like:
Not a strong opinion, but it could also make refactoring easier when we want to provide a name in a second thought, since appending something to a line is always easier than inserting something in the middle for most IDEs 🙂
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.
This reads more naturally. Is it possible to use reflection to infer the method's name and omit the symbol altogether, making it optional?