Skip to content

Commit

Permalink
Remove deprecated method #on (#33)
Browse files Browse the repository at this point in the history
  • Loading branch information
bvicenzo committed Mar 9, 2022
1 parent 81ceae3 commit c45d884
Show file tree
Hide file tree
Showing 3 changed files with 1 addition and 160 deletions.
40 changes: 1 addition & 39 deletions lib/f_service/result/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ module Result
#
# @abstract
class Base
%i[initialize and_then successful? failed? value value! error].each do |method_name|
%i[and_then successful? failed? value value! error].each do |method_name|
define_method(method_name) do |*_args|
raise NotImplementedError, "called #{method_name} on class Result::Base"
end
Expand All @@ -18,44 +18,6 @@ def initialize
@handled = false
end

# "Pattern matching"-like method for results.
# It will run the success path if Result is a Success.
# Otherwise, it will run the failure path.
#
#
# @example
# class UsersController < BaseController
# def update
# User::Update.(user: user).on(
# success: ->(value) { return json_success(value) },
# failure: ->(error) { return json_error(error) }
# )
# end
#
# private
#
# def user
# @user ||= User.find_by!(slug: params[:slug])
# end
# end
#
# @param success [#call] a lambda (or anything that responds to #call) to run on success
# @param failure [#call] a lambda (or anything that responds to #call) to run on failure
# @deprecated Use {#on_success} and/or {#on_failure} instead.
# @api public
def on(success:, failure:)
FService.deprecate!(
name: "#{self.class}##{__method__}",
alternative: '#on_success and/or #on_failure'
)

if successful?
success.call(value)
else
failure.call(error)
end
end

# This hook runs if the result is successful.
# Can receive one or more types to be checked before running the given block.
#
Expand Down
59 changes: 0 additions & 59 deletions spec/f_service/result/failure_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,65 +19,6 @@
it { expect(failure.type).to eq(:error) }
end

describe '#on' do
context 'when matching results' do
subject(:failure_match) do
described_class.new('Whoops!').on(
success: ->(_value) { raise "This won't ever run" },
failure: ->(error) { return error + '!' }
)
end

it 'runs on the failure path' do
expect(failure_match).to eq('Whoops!!')
end
end

context 'when chaining results' do
subject(:chain) do
FService::Result::Success.new('This...')
.then { |value| described_class.new(value + ' Fails!') }
.and_then { |_value| raise "This won't ever run!" }
end

it { expect(chain).to be_failed }

it 'shorts circuit on failures' do
expect(chain.error).to eq('This... Fails!')
end
end

context 'when chaining results with a catch block' do
subject(:chain) do
FService::Result::Success.new('This...')
.then { |value| described_class.new(value + ' Fails!') }
.catch { |error| described_class.new(error + ' Again!') }
.then { |_value| raise "This won't ever run!" }
end

it { expect(chain).to be_failed }

it 'executes the catch block ' do
expect(chain.error).to eq('This... Fails! Again!')
end
end

context 'when chaining results with a catch block using the `or` alias' do
subject(:chain) do
FService::Result::Success.new('This...')
.then { |value| described_class.new(value + ' Fails!') }
.or_else { |error| described_class.new(error + ' Again!') }
.then { |_value| raise "This won't ever run!" }
end

it { expect(chain).to be_failed }

it 'executes the catch block ' do
expect(chain.error).to eq('This... Fails! Again!')
end
end
end

describe '#on_failure' do
describe 'return' do
subject(:on_failure_callback) { failure.on_failure(unhandled: true) { 'some recovering' } }
Expand Down
62 changes: 0 additions & 62 deletions spec/f_service/result/success_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,68 +19,6 @@
it { expect(success.type).to eq(:ok) }
end

describe '#on' do
context 'when matching results' do
subject(:success_match) do
described_class.new('Yay!').on(
success: ->(value) { return value + '!' },
failure: ->(_error) { raise "This won't ever run" }
)
end

it 'runs on the success path' do
expect(success_match).to eq('Yay!!')
end
end

context 'when chaining results' do
subject(:chain) do
described_class.new('Yay!')
.then { |value| described_class.new(value + ' It') }
.then { |value| described_class.new(value + ' works!', :ok) }
.and_then { |value, type| described_class.new(value + " Type: #{type}!") }
end

it { expect(chain).to be_successful }

it 'chains successful results' do
expect(chain.value).to eq('Yay! It works! Type: ok!')
end
end

context 'when chaining results with a catch block' do
subject(:chain) do
described_class.new('Yay!')
.catch { FService::Result::Failure.new('Shoot! It failed!') }
.then { |value| described_class.new(value + ' It') }
.then { |value| described_class.new(value + ' works!', :ok) }
.then { |value, type| described_class.new(value + " Type: #{type}!") }
end

it { expect(chain).to be_successful }

it 'chains successful results' do
expect(chain.value).to eq('Yay! It works! Type: ok!')
end
end

context 'when chaining results with a catch block using the `or` alias' do
subject(:chain) do
described_class.new('Yay!')
.or_else { FService::Result::Failure.new('Shoot! It failed!') }
.then { |value| described_class.new(value + ' It') }
.then { |value| described_class.new(value + ' works!', :ok) }
.then { |value, type| described_class.new(value + " Type: #{type}!") }
end

it { expect(chain).to be_successful }

it 'chains successful results' do
expect(chain.value).to eq('Yay! It works! Type: ok!')
end
end
end

describe '#on_success' do
describe 'return' do
subject(:on_success_callback) { success.on_success(unhandled: true) { 'some recovering' } }
Expand Down

0 comments on commit c45d884

Please sign in to comment.