-
-
Notifications
You must be signed in to change notification settings - Fork 55
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
Add around steps #85
Merged
Merged
Add around steps #85
Changes from 6 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
0087f7b
Add basic implementation for around steps
flash-gordon fc3651a
Add more specs for around steps
flash-gordon 220c3c2
Refactor stack class
flash-gordon 65ca2f4
Add unit tests
flash-gordon a26a356
Refactor #once
flash-gordon e89147b
Change adapter API
flash-gordon 67a204e
Make Stack#compile private
flash-gordon 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,38 @@ | ||
module Dry | ||
module Transaction | ||
# @api private | ||
class Callable | ||
def self.[](callable) | ||
if callable.is_a?(self) | ||
callable | ||
elsif callable.nil? | ||
nil | ||
else | ||
new(callable) | ||
end | ||
end | ||
|
||
attr_reader :operation | ||
attr_reader :arity | ||
|
||
def initialize(operation) | ||
@operation = case operation | ||
when Proc, Method | ||
operation | ||
else | ||
operation.method(:call) | ||
end | ||
|
||
@arity = @operation.arity | ||
end | ||
|
||
def call(*args, &block) | ||
if arity.zero? | ||
operation.(&block) | ||
else | ||
operation.(*args, &block) | ||
end | ||
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
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,22 @@ | ||
module Dry | ||
module Transaction | ||
# @api private | ||
class Stack | ||
RETURN = -> x { x } | ||
|
||
def initialize(steps) | ||
@stack = compile(steps) | ||
end | ||
|
||
def call(m) | ||
@stack.(m) | ||
end | ||
|
||
def compile(steps) | ||
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. Oh, I reckon it'd be good to drop this under a 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. Sure thing |
||
steps.reverse.reduce(RETURN) do |next_step, step| | ||
proc { |m| m.bind { |value| step.(value, next_step) } } | ||
end | ||
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
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,49 @@ | ||
require "dry/transaction/callable" | ||
|
||
module Dry | ||
module Transaction | ||
# @api private | ||
class StepAdapter | ||
def self.[](adapter, operation, options) | ||
if adapter.is_a?(self) | ||
adapter.with(operation, options) | ||
else | ||
new(adapter, operation, options) | ||
end | ||
end | ||
|
||
attr_reader :adapter | ||
attr_reader :operation | ||
attr_reader :options | ||
|
||
def initialize(adapter, operation, options) | ||
@adapter = case adapter | ||
when Proc, Method | ||
adapter | ||
else | ||
adapter.method(:call) | ||
end | ||
|
||
@operation = Callable[operation] | ||
|
||
@options = options | ||
|
||
@yields = @adapter. | ||
parameters. | ||
any? { |type, _| type == :block } | ||
end | ||
|
||
def yields? | ||
@yields | ||
end | ||
|
||
def call(args, &block) | ||
adapter.(operation, options, args, &block) | ||
end | ||
|
||
def with(operation = self.operation, new_options = {}) | ||
self.class.new(adapter, operation, options.merge(new_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
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,25 @@ | ||
require "dry/monads/result" | ||
require "dry/transaction/errors" | ||
|
||
module Dry | ||
module Transaction | ||
class StepAdapters | ||
# @api private | ||
class Around | ||
include Dry::Monads::Result::Mixin | ||
|
||
def call(operation, options, args, &block) | ||
result = operation.(*args, &block) | ||
|
||
unless result.is_a?(Dry::Monads::Result) | ||
raise InvalidResultError.new(options[:step_name]) | ||
end | ||
|
||
result | ||
end | ||
end | ||
|
||
register :around, Around.new | ||
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
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
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
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.
This is a nice naming improvement :)