From 661413b071d3e853aaf03c9fd0a80b9fe4ad41be Mon Sep 17 00:00:00 2001 From: Anton Chuchkalov Date: Sun, 26 Mar 2017 17:47:02 +0300 Subject: [PATCH] avoid context inner logic duplicating --- lib/interactor/context.rb | 4 ++++ lib/interactor/organizer.rb | 2 +- spec/interactor/organizer_spec.rb | 5 +++-- 3 files changed, 8 insertions(+), 3 deletions(-) diff --git a/lib/interactor/context.rb b/lib/interactor/context.rb index 0d011e3..523f77b 100644 --- a/lib/interactor/context.rb +++ b/lib/interactor/context.rb @@ -123,6 +123,10 @@ def failure? def fail!(context = {}) context.each { |key, value| modifiable[key.to_sym] = value } @failure = true + signal_early_return! + end + + def signal_early_return! throw :early_return end diff --git a/lib/interactor/organizer.rb b/lib/interactor/organizer.rb index cf31ffc..b2a31d9 100644 --- a/lib/interactor/organizer.rb +++ b/lib/interactor/organizer.rb @@ -76,7 +76,7 @@ module InstanceMethods # Returns nothing. def call self.class.organized.each do |interactor| - throw(:early_return) if context.failure? + context.signal_early_return! if context.failure? interactor.call(context) end end diff --git a/spec/interactor/organizer_spec.rb b/spec/interactor/organizer_spec.rb index 8c385ea..7398e24 100644 --- a/spec/interactor/organizer_spec.rb +++ b/spec/interactor/organizer_spec.rb @@ -55,11 +55,12 @@ module Interactor instance.call end - it "throws :early_return on failure of one of organizers" do + it "signals about early_return on failure of one of organizers" do allow(context).to receive(:failure?).and_return(false, true) + expect(context).to receive(:signal_early_return!).and_throw(:foo) expect { instance.call - }.to throw_symbol(:early_return) + }.to throw_symbol end end end