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

Alias Dry::Monads::Result as Dry::Operation::Result #17

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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
26 changes: 14 additions & 12 deletions lib/dry/operation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,19 @@ module Dry
# end
#
# def validate(input)
# # Dry::Monads::Result::Success or Dry::Monads::Result::Failure
# # Dry::Operation::Result::Success or Dry::Operation::Result::Failure
# end
#
# def persist(attrs)
# # Dry::Monads::Result::Success or Dry::Monads::Result::Failure
# # Dry::Operation::Result::Success or Dry::Operation::Result::Failure
# end
#
# def notify(user)
# # Dry::Monads::Result::Success or Dry::Monads::Result::Failure
# # Dry::Operation::Result::Success or Dry::Operation::Result::Failure
# end
# end
#
# include Dry::Monads[:result]
# include Dry::Operation::Result::Mixin
#
# case MyOperation.new.call(input)
# in Success(user)
Expand Down Expand Up @@ -141,28 +141,30 @@ def self.loader
private_constant :FAILURE_TAG

extend ClassContext
include Dry::Monads::Result::Mixin

# Wraps block's return value in a {Dry::Monads::Result::Success}
Result = Dry::Monads::Result
include Result::Mixin

# Wraps block's return value in a {Dry::Operation::Result::Success}
#
# Catches `:halt` and returns it
#
# @yieldreturn [Object]
# @return [Dry::Monads::Result::Success]
# @return [Dry::Operation::Result::Success]
# @see #step
def steps(&block)
catching_failure { Success(block.call) }
end

# Unwraps a {Dry::Monads::Result::Success}
# Unwraps a {Dry::Operation::Result::Success}
#
# Throws `:halt` with a {Dry::Monads::Result::Failure} on failure.
# Throws `:halt` with a {Dry::Operation::Result::Failure} on failure.
#
# @param result [Dry::Monads::Result]
# @param result [Dry::Operation::Result]
# @return [Object] wrapped value
# @see #steps
def step(result)
if result.is_a?(Dry::Monads::Result)
if result.is_a?(Dry::Operation::Result)
result.value_or { throw_failure(result) }
else
raise InvalidStepResultError.new(result: result)
Expand All @@ -171,7 +173,7 @@ def step(result)

# Invokes a callable in case of block's failure
#
# Throws `:halt` with a {Dry::Monads::Result::Failure} on failure.
# Throws `:halt` with a {Dry::Operation::Result::Failure} on failure.
#
# This method is useful when you want to perform some side-effect when a
# failure is encountered. It's meant to be used within the {#steps} block
Expand Down
2 changes: 1 addition & 1 deletion lib/dry/operation/errors.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class InvalidStepResultError < Error
def initialize(result:)
super <<~MSG
Your step must return `Success(..)` or `Failure(..)`, \
from `Dry::Monads::Result`. Instead, it was `#{result.inspect}`.
from `Dry::Operation::Result`. Instead, it was `#{result.inspect}`.
MSG
end
end
Expand Down
6 changes: 3 additions & 3 deletions spec/unit/operation_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
require "spec_helper"

RSpec.describe Dry::Operation do
include Dry::Monads[:result]
include Dry::Operation::Result::Mixin

describe "#steps" do
it "wraps block's return value in a Success" do
Expand Down Expand Up @@ -40,7 +40,7 @@ def foo(value)

# Make sure we don't use pattern matching to extract the value, as that
# would be a problem with a value that is an array. See
# https://https://github.com/dry-rb/dry-monads/issues/173
# https://github.com/dry-rb/dry-monads/issues/173
it "is able to extract an array from a success result" do
expect(
described_class.new.step(Success([:foo]))
Expand All @@ -62,7 +62,7 @@ def foo(value)
.with_message(
<<~MSG
Your step must return `Success(..)` or `Failure(..)`, \
from `Dry::Monads::Result`. Instead, it was `123`.
from `Dry::Operation::Result`. Instead, it was `123`.
MSG
)
end
Expand Down
Loading