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

Add ensure hook #147

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
15 changes: 15 additions & 0 deletions lib/interactor/hooks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,11 @@ def after(*hooks, &block)
hooks.each { |hook| after_hooks.unshift(hook) }
end

def ensure_hook(*hooks, &block)
hooks << block if block
hooks.each { |hook| ensure_hooks.push(hook) }
end

# Internal: An Array of declared hooks to run around Interactor
# invocation. The hooks appear in the order in which they will be run.
#
Expand Down Expand Up @@ -183,6 +188,10 @@ def before_hooks
def after_hooks
@after_hooks ||= []
end

def ensure_hooks
@ensure_hooks ||= []
end
end

private
Expand Down Expand Up @@ -213,6 +222,8 @@ def with_hooks
yield
run_after_hooks
end
ensure
run_ensure_hooks
end

# Internal: Run around hooks.
Expand All @@ -238,6 +249,10 @@ def run_after_hooks
run_hooks(self.class.after_hooks)
end

def run_ensure_hooks
run_hooks(self.class.ensure_hooks)
end

# Internal: Run a colection of hooks. The "run_hooks" method is the common
# interface by which collections of either before or after hooks are run.
#
Expand Down
34 changes: 34 additions & 0 deletions spec/interactor/hooks_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,40 @@ def process
hooked
end

context "with an ensure hook method" do
let(:hooked) {
build_hooked do
before :add_before
after :add_after_with_error
ensure_hook :add_ensure

def self.context
@context ||= OpenStruct.new
end

private

def add_before
self.class.context.resource = 1
end

def add_after_with_error
raise 'something wrong has happened'
end

def add_ensure
self.class.context.resource = 0
end
end
}

it "runs the ensure hook block" do
expect { hooked.process }.to raise_error('something wrong has happened')

expect(hooked.context.resource).to eq 0
end
end

context "with an around hook method" do
let(:hooked) {
build_hooked do
Expand Down