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

Support passing blocks to strategy methods #3

Merged
merged 1 commit into from
Jun 27, 2023
Merged
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
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
37 changes: 37 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,31 @@ def delete(id:)
end
end
end

describe ".update" do
let(:wrappers) do
[
add10_wrapper
]
end

let(:add10_wrapper) do
Class.new do
def update(id:)
yield(id: id + 10)
end
end
end

before do
allow(add10_wrapper).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