Skip to content

Commit

Permalink
Deprecate method #then for Success and Failure classes (#40)
Browse files Browse the repository at this point in the history
* Add tests to actual then methods

* Improve deprecation message exibition in multiple lines

* Improve deprecation methods exibition

* Deprecate Success and Failure #then methods
  • Loading branch information
bvicenzo committed Dec 16, 2022
1 parent 4c89650 commit 1b250d0
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 13 deletions.
4 changes: 2 additions & 2 deletions lib/f_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ module FService
#
# @api private
def self.deprecate!(name:, alternative:, from: nil)
warn_message = ["[DEPRECATED] #{name} is deprecated; "]
warn_message = ["\n[DEPRECATED] #{name} is deprecated; "]
warn_message << ["called from #{from}; "] unless from.nil?
warn_message << "use #{alternative} instead. "
warn_message << 'It will be removed on the next release.'

warn warn_message.join
warn warn_message.join("\n")
end
end
9 changes: 6 additions & 3 deletions lib/f_service/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,8 @@ def run
def success(data = nil)
FService.deprecate!(
name: "#{self.class}##{__method__}",
alternative: '#Success'
alternative: '#Success',
from: caller[0]
)

Result::Success.new(data)
Expand Down Expand Up @@ -250,7 +251,8 @@ def Try(type = nil, catch: StandardError)
def failure(data = nil)
FService.deprecate!(
name: "#{self.class}##{__method__}",
alternative: '#Failure'
alternative: '#Failure',
from: caller[0]
)

Result::Failure.new(data)
Expand Down Expand Up @@ -283,7 +285,8 @@ def failure(data = nil)
def result(condition, data = nil)
FService.deprecate!(
name: "#{self.class}##{__method__}",
alternative: '#Check'
alternative: '#Check',
from: caller[0]
)

condition ? success(data) : failure(data)
Expand Down
7 changes: 6 additions & 1 deletion lib/f_service/result/failure.rb
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,12 @@ def catch
def and_then
self
end
alias then and_then

# See #and_then
def then
FService.deprecate!(name: "#{self.class}##{__method__}", alternative: '#and_then', from: caller[0])
and_then
end

# Outputs a string representation of the object
#
Expand Down
8 changes: 7 additions & 1 deletion lib/f_service/result/success.rb
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,13 @@ def error
def and_then
yield(*to_ary)
end
alias then and_then

# See #and_then
def then(&block)
FService.deprecate!(name: "#{self.class}##{__method__}", alternative: '#and_then', from: caller[0])

and_then(&block)
end

# Returns itself to the given block.
# Use this to chain multiple actions or service calls (only valid when they return a Result).
Expand Down
32 changes: 28 additions & 4 deletions spec/f_service/result/failure_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -125,12 +125,36 @@
describe '#and_then' do
subject(:failure) { described_class.new('Pax', :ok) }

it 'does not yields the block' do
expect { |block| failure.and_then(&block) }.not_to yield_control
context 'when a block is given' do
it 'returns itself' do
expect(failure.and_then { 'an error happened' }).to eq(failure)
end
end

it 'returns itself' do
expect(failure.and_then { 'an error happened' }).to eq(failure)
context 'when a block is passed as argument' do
it 'does not yields the block' do
expect { |block| failure.and_then(&block) }.not_to yield_control
end
end
end

describe '#then' do
subject(:failure) { described_class.new('Pax', :ok) }

before { allow(FService).to receive(:deprecate!) }

context 'when a block is given' do
it 'returns itself', :aggregate_failures do
expect(failure.then { 'an error happened' }).to eq(failure)
expect(FService).to have_received(:deprecate!)
end
end

context 'when a block is passed as argument' do
it 'does not yields the block', :aggregate_failures do
expect { |block| failure.then(&block) }.not_to yield_control
expect(FService).to have_received(:deprecate!)
end
end
end

Expand Down
36 changes: 34 additions & 2 deletions spec/f_service/result/success_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,40 @@
describe '#and_then' do
subject(:success) { described_class.new('Pax', :ok) }

it 'returns the given block result' do
expect(success.and_then { |value| "Hello, #{value}!" }).to eq('Hello, Pax!')
context 'when a block is given' do
it 'returns the given block result' do
expect(success.and_then { |value| "Hello, #{value}!" }).to eq('Hello, Pax!')
end
end

context 'when a block is passed as argument' do
it 'returns the given block argument' do
block = ->(value, _type) { "Hello, #{value}!" }

expect(success.and_then(&block)).to eq('Hello, Pax!')
end
end
end

describe '#then' do
subject(:success) { described_class.new('Pax', :ok) }

before { allow(FService).to receive(:deprecate!) }

context 'when a block is given' do
it 'returns the given block result', :aggregate_failures do
expect(success.then { |value| "Hello, #{value}!" }).to eq('Hello, Pax!')
expect(FService).to have_received(:deprecate!)
end
end

context 'when a block is passed as argument' do
it 'returns the given block argument', :aggregate_failures do
block = ->(value, _type) { "Hello, #{value}!" }

expect(success.then(&block)).to eq('Hello, Pax!')
expect(FService).to have_received(:deprecate!)
end
end
end

Expand Down

0 comments on commit 1b250d0

Please sign in to comment.