Skip to content

Commit

Permalink
avoid context inner logic duplicating
Browse files Browse the repository at this point in the history
  • Loading branch information
hedgesky committed Mar 26, 2017
1 parent 7326e77 commit 661413b
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 3 deletions.
4 changes: 4 additions & 0 deletions lib/interactor/context.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion lib/interactor/organizer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 3 additions & 2 deletions spec/interactor/organizer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 661413b

Please sign in to comment.