Skip to content

Commit

Permalink
support passing blocks to strategy methods
Browse files Browse the repository at this point in the history
  • Loading branch information
Ahmed Elakour committed Jun 26, 2023
1 parent d604c63 commit 45d4efe
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 4 deletions.
8 changes: 6 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Changed
* support passing blocks to strategy methods


## [2.0.0] - 2023-01-17
### Changed
* switch CI to github actions
Expand All @@ -28,7 +32,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
* fix after wrapper ordering bug [PR#6](https://github.com/andreaseger/receptacle/pull/6)

## [0.3.0}
### Added
### Added
* add danger
* also support higher arity methods (== method with more than one argument)

Expand All @@ -38,7 +42,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
* enable ruby 2.1+

## [0.1.1]
## Added
## Added
* add changelog, update copyright
* add test helper method `ensure_method_delegators` to make rspec stubs/mocks work as expected

Expand Down
4 changes: 2 additions & 2 deletions lib/receptacle/repo.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ module Receptacle
module Repo
module ClassMethods
def mediate(method_name)
define_singleton_method(method_name) do |*args, **kwargs|
define_singleton_method(method_name) do |*args, **kwargs, &block|
raise Errors::NotConfigured.new(repo: self) unless @strategy

with_wrappers(@wrappers.dup, method_name, *args, **kwargs) do |*sub_args, **sub_kwargs|
strategy.new.public_send(method_name, *sub_args, **sub_kwargs)
strategy.new.public_send(method_name, *sub_args, **sub_kwargs, &block)
end
end
end
Expand Down
31 changes: 31 additions & 0 deletions spec/receptacle_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

mediate :find
mediate :delete
mediate :update

wrappers(test_wrappers)
strategy(test_strategy)
Expand All @@ -28,6 +29,10 @@ def find(id, unscoped: true) # rubocop:disable Lint/UnusedMethodArgument
def delete(id:) # rubocop:disable Lint/UnusedMethodArgument
"deleted"
end

def update(id:, &block) # rubocop:disable Lint/UnusedMethodArgument
yield + id
end
end
end

Expand All @@ -51,6 +56,7 @@ def delete(id:) # rubocop:disable Lint/UnusedMethodArgument

mediate :find
mediate :delete
mediate :update

strategy(test_strategy)
end
Expand All @@ -62,6 +68,12 @@ def delete(id:) # rubocop:disable Lint/UnusedMethodArgument
expect(strategy_instance).to have_received(:find)
.with(10, unscoped: false)
end

context "with block passed" do
it "executes the block" do
expect(repository.update(id: 1) { 1 }).to eql(2)
end
end
end

context "with two wrappers that modify input and output of the find method" do
Expand Down Expand Up @@ -202,6 +214,25 @@ def delete(id:)
end
end
end

describe ".update" do
let(:add10_flip_unscoped_and_multiply_result_wrapper) do
Class.new do
def update(id:)
yield(id: id + 10)
end
end
end

before do
allow(add10_flip_unscoped_and_multiply_result_wrapper_instance).to receive(:update)
.and_call_original
end

it "calls the strategy with the output of the wrapper and executes the block in the strategy method" do
expect(repository.update(id: 5) { 1 }).to eq(16)
end
end
end
end

Expand Down

0 comments on commit 45d4efe

Please sign in to comment.