Skip to content

Commit

Permalink
Merge pull request #155 from caalberts/batch-callback
Browse files Browse the repository at this point in the history
Support Class#method notation in batch callback
  • Loading branch information
packrat386 committed Jun 21, 2020
2 parents e17b5a4 + 56fcf12 commit 3c99979
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
12 changes: 10 additions & 2 deletions lib/rspec/sidekiq/batch.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,17 @@ def failures
def join
::Sidekiq::Worker.drain_all

@callbacks.each do |event, callback_class, options|
@callbacks.each do |event, callback, options|
if event != :success || failures == 0
callback_class.new.send("on_#{event}", self, options)
case callback
when Class
callback.new.send("on_#{event}", self, options)
when String
klass, meth = callback.split('#')
klass.constantize.new.send(meth, self, options)
else
raise ArgumentError, 'Unsupported callback notation'
end
end
end
end
Expand Down
6 changes: 6 additions & 0 deletions spec/rspec/sidekiq/batch_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,18 @@ class MyCallback
def on_event(status, options); end
end

class OtherCallback
def foo(status, options); end
end

before(:each) do
batch.on(:event, MyCallback, my_arg: 42)
batch.on(:event, 'OtherCallback#foo', my_arg: 23)
end

it 'executes callbacks' do
expect_any_instance_of(MyCallback).to receive(:on_event).with(subject, { my_arg: 42 })
expect_any_instance_of(OtherCallback).to receive(:foo).with(subject, { my_arg: 23 })
subject.join
end
end
Expand Down

0 comments on commit 3c99979

Please sign in to comment.