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

Add around steps #85

Merged
merged 7 commits into from
Nov 27, 2017
Merged
Show file tree
Hide file tree
Changes from 6 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
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ gem "dry-matcher", git: "https://github.com/dry-rb/dry-matcher.git"
group :test do
gem "simplecov"
gem "codeclimate-test-reporter"
gem "byebug", platform: :mri
gem "pry-byebug", platform: :mri
gem "dry-container"
end

Expand Down
38 changes: 38 additions & 0 deletions lib/dry/transaction/callable.rb
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
20 changes: 16 additions & 4 deletions lib/dry/transaction/errors.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,26 @@
module Dry
module Transaction
class InvalidStepError < ArgumentError
def initialize(key)
super("Transaction step `#{key}` must respond to `#call`")
def initialize(step_name)
super("step +`#{step_name}`+ must respond to `#call`")
end
end

class MissingStepError < ArgumentError
def initialize(key)
super("Definition for transaction step `#{key}` is missing")
def initialize(step_name)
super("Definition for step +`#{step_name}`+ is missing")
end
end

class InvalidResultError < ArgumentError
def initialize(step_name)
super("step +#{step_name}+ must return a Result object")
end
end

class MissingCatchListError < ArgumentError
def initialize(step_name)
super("step +#{step_name}+ requires one or more exception classes provided via +catch:+")
end
end
end
Expand Down
5 changes: 4 additions & 1 deletion lib/dry/transaction/instance_methods.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
require "dry/monads/result"
require "dry/transaction/result_matcher"
require "dry/transaction/stack"

module Dry
module Transaction
Expand All @@ -9,6 +10,7 @@ module InstanceMethods
attr_reader :steps
attr_reader :operations
attr_reader :listeners
attr_reader :stack

def initialize(steps: (self.class.steps), listeners: nil, **operations)
@steps = steps.map { |step|
Expand All @@ -25,13 +27,14 @@ def initialize(steps: (self.class.steps), listeners: nil, **operations)
step.with(operation: operation)
}
@operations = operations
@stack = Stack.new(@steps)
subscribe(listeners) unless listeners.nil?
end

def call(input = nil, &block)
assert_step_arity

result = steps.inject(Success(input), :bind)
result = stack.(Success(input))

if block
ResultMatcher.(result, &block)
Expand Down
22 changes: 22 additions & 0 deletions lib/dry/transaction/stack.rb
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 }
Copy link
Member

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 :)


def initialize(steps)
@stack = compile(steps)
end

def call(m)
@stack.(m)
end

def compile(steps)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, I reckon it'd be good to drop this under a private, if you don't mind :)

Copy link
Member Author

Choose a reason for hiding this comment

The 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
49 changes: 23 additions & 26 deletions lib/dry/transaction/step.rb
Original file line number Diff line number Diff line change
@@ -1,53 +1,59 @@
require "dry/monads/result"
require "wisper"
require "dry/transaction/step_failure"
require "dry/transaction/step_adapter"

module Dry
module Transaction
# @api private
class Step
UNDEFINED = Object.new.freeze
RETURN = -> x { x }

include Wisper::Publisher
include Dry::Monads::Result::Mixin

attr_reader :step_adapter
attr_reader :step_name
attr_reader :operation_name
attr_reader :operation
attr_reader :options
attr_reader :call_args

def initialize(step_adapter, step_name, operation_name, operation, options, call_args = [])
@step_adapter = step_adapter
@step_adapter = StepAdapter[step_adapter, operation, **options, step_name: step_name]
@step_name = step_name
@operation_name = operation_name
@operation = operation
@options = options
@call_args = call_args
end

def with(operation: UNDEFINED, call_args: UNDEFINED)
return self if operation == UNDEFINED && call_args == UNDEFINED
new_operation = operation == UNDEFINED ? self.operation : operation
new_call_args = call_args == UNDEFINED ? self.call_args : call_args
new_operation = operation == UNDEFINED ? step_adapter.operation : operation
new_call_args = call_args == UNDEFINED ? self.call_args : Array(call_args)

self.class.new(
step_adapter,
step_name,
operation_name,
new_operation,
options,
new_call_args,
step_adapter.options,
new_call_args
)
end

def call(input)
args = [input] + Array(call_args)
def call(input, continue = RETURN)
args = [input, *call_args]

if step_adapter.yields?
with_broadcast(args) { step_adapter.(args, &continue) }
else
continue.(with_broadcast(args) { step_adapter.(args) })
end
end

def with_broadcast(args)
broadcast :step_called, step_name, *args
result = step_adapter.call(self, *args)

result.fmap { |value|
yield.fmap { |value|
broadcast :step_succeeded, step_name, *args
value
}.or { |value|
Expand All @@ -56,21 +62,12 @@ def call(input)
}
end

def call_operation(*input)
if arity.zero?
operation.call
else
operation.call(*input)
end
def arity
step_adapter.operation.arity
end

def arity
case operation
when Proc, Method
operation.arity
else
operation.method(:call).arity
end
def operation
step_adapter.operation
end
end
end
Expand Down
49 changes: 49 additions & 0 deletions lib/dry/transaction/step_adapter.rb
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
1 change: 1 addition & 0 deletions lib/dry/transaction/step_adapters.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ class StepAdapters
require "dry/transaction/step_adapters/raw"
require "dry/transaction/step_adapters/tee"
require "dry/transaction/step_adapters/try"
require "dry/transaction/step_adapters/around"
25 changes: 25 additions & 0 deletions lib/dry/transaction/step_adapters/around.rb
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
5 changes: 3 additions & 2 deletions lib/dry/transaction/step_adapters/check.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ class StepAdapters
class Check
include Dry::Monads::Either::Mixin

def call(step, input, *args)
res = step.operation.call(*args, input)
def call(operation, _options, args)
input = args[0]
res = operation.(*args)
res == true || res.is_a?(Success) ? Success(input) : Failure(input)
end
end
Expand Down
4 changes: 2 additions & 2 deletions lib/dry/transaction/step_adapters/map.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ class StepAdapters
class Map
include Dry::Monads::Result::Mixin

def call(step, input, *args)
Success(step.call_operation(input, *args))
def call(operation, _options, args)
Success(operation.(*args))
end
end

Expand Down
16 changes: 5 additions & 11 deletions lib/dry/transaction/step_adapters/raw.rb
Original file line number Diff line number Diff line change
@@ -1,20 +1,14 @@
require "dry/monads/result"
require "dry/transaction/errors"
require "dry/transaction/step_adapters/around"

module Dry
module Transaction
class StepAdapters
# @api private
class Raw
include Dry::Monads::Result::Mixin

def call(step, input, *args)
result = step.call_operation(input, *args)

unless result.is_a?(Dry::Monads::Result)
raise ArgumentError, "step +#{step.step_name}+ must return a Result object"
end

result
class Raw < Around
def call(operation, options, args)
super(operation, options, args, &nil)
end
end

Expand Down
6 changes: 3 additions & 3 deletions lib/dry/transaction/step_adapters/tee.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ class StepAdapters
class Tee
include Dry::Monads::Result::Mixin

def call(step, input, *args)
step.call_operation(input, *args)
Success(input)
def call(operation, _options, args)
operation.(*args)
Success(args[0])
end
end

Expand Down
Loading