Skip to content

Commit

Permalink
don't include ClassMethods with MessageSending
Browse files Browse the repository at this point in the history
moves Delayed::ClassMethods out of Delayed::MessageSending because
lib/delayed_job.rb includes Delayed::MessageSending into Object, and
that has the side-effect of defining ClassMethods in EVERY object
  • Loading branch information
Kache committed Jun 22, 2016
1 parent e3772d4 commit 3c4d91c
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 26 deletions.
50 changes: 25 additions & 25 deletions lib/delayed/message_sending.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,36 +26,36 @@ def send_at(time, method, *args)
warn '[DEPRECATION] `object.send_at(time, :method)` is deprecated. Use `object.delay(:run_at => time).method'
__delay__(:run_at => time).__send__(method, *args)
end
end

module ClassMethods
def handle_asynchronously(method, opts = {}) # rubocop:disable PerceivedComplexity
aliased_method = method.to_s.sub(/([?!=])$/, '')
punctuation = $1 # rubocop:disable PerlBackrefs
with_method = "#{aliased_method}_with_delay#{punctuation}"
without_method = "#{aliased_method}_without_delay#{punctuation}"
define_method(with_method) do |*args|
curr_opts = opts.clone
curr_opts.each_key do |key|
next unless (val = curr_opts[key]).is_a?(Proc)
curr_opts[key] = if val.arity == 1
val.call(self)
else
val.call
end
module ClassMethods
def handle_asynchronously(method, opts = {}) # rubocop:disable PerceivedComplexity
aliased_method = method.to_s.sub(/([?!=])$/, '')
punctuation = $1 # rubocop:disable PerlBackrefs
with_method = "#{aliased_method}_with_delay#{punctuation}"
without_method = "#{aliased_method}_without_delay#{punctuation}"
define_method(with_method) do |*args|
curr_opts = opts.clone
curr_opts.each_key do |key|
next unless (val = curr_opts[key]).is_a?(Proc)
curr_opts[key] = if val.arity == 1
val.call(self)
else
val.call
end
delay(curr_opts).__send__(without_method, *args)
end
delay(curr_opts).__send__(without_method, *args)
end

alias_method without_method, method
alias_method method, with_method
alias_method without_method, method
alias_method method, with_method

if public_method_defined?(without_method)
public method
elsif protected_method_defined?(without_method)
protected method
elsif private_method_defined?(without_method)
private method
end
if public_method_defined?(without_method)
public method
elsif protected_method_defined?(without_method)
protected method
elsif private_method_defined?(without_method)
private method
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/delayed_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@
require 'delayed/railtie' if defined?(Rails::Railtie)

Object.send(:include, Delayed::MessageSending)
Module.send(:include, Delayed::MessageSending::ClassMethods)
Module.send(:include, Delayed::ClassMethods)
5 changes: 5 additions & 0 deletions spec/message_sending_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
require 'helper'

describe Delayed::MessageSending do
it 'does not include ClassMethods along with MessageSending' do
expect { ClassMethods }.to raise_error(NameError)
expect(defined?(String::ClassMethods)).to eq(nil)
end

describe 'handle_asynchronously' do
class Story
def tell!(_arg); end
Expand Down

0 comments on commit 3c4d91c

Please sign in to comment.