From 45d4efe50ed853f56ba2b651ee21044e185a8cdc Mon Sep 17 00:00:00 2001 From: Ahmed Elakour Date: Mon, 26 Jun 2023 19:12:01 +0200 Subject: [PATCH] support passing blocks to strategy methods --- CHANGELOG.md | 8 ++++++-- lib/receptacle/repo.rb | 4 ++-- spec/receptacle_spec.rb | 31 +++++++++++++++++++++++++++++++ 3 files changed, 39 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0dfda5f..c8ab9ca 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 @@ -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) @@ -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 diff --git a/lib/receptacle/repo.rb b/lib/receptacle/repo.rb index 7f77139..0ea4f3d 100644 --- a/lib/receptacle/repo.rb +++ b/lib/receptacle/repo.rb @@ -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 diff --git a/spec/receptacle_spec.rb b/spec/receptacle_spec.rb index 8ef0571..057988f 100644 --- a/spec/receptacle_spec.rb +++ b/spec/receptacle_spec.rb @@ -13,6 +13,7 @@ mediate :find mediate :delete + mediate :update wrappers(test_wrappers) strategy(test_strategy) @@ -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 @@ -51,6 +56,7 @@ def delete(id:) # rubocop:disable Lint/UnusedMethodArgument mediate :find mediate :delete + mediate :update strategy(test_strategy) end @@ -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 @@ -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