module Resque::Plugins::Remora::PushPop::ClassMethods

Public Instance Methods

constantize(camel_cased_word) click to toggle source

NOTE: Prior to Resque 1.26, Resque.constantize was not defined. For 1.26 and after, overriding Resque.constantize causes compatibility issues. So, for Resque < 1.26 we want to define a constantize method here. For Resque >= 1.26 we do not.

Calls superclass method
# File lib/resque/plugins/remora/push_pop.rb, line 38
def constantize(camel_cased_word)
  defined?(super) ? super(camel_cased_word) : _constantize(camel_cased_word)
end
pop(queue) click to toggle source
# File lib/resque/plugins/remora/push_pop.rb, line 20
def pop(queue)
  job = original_pop(queue)
  begin
    attachment = job['remora']
    job_class = constantize(job['class'])
    if !attachment.nil? && remora_class?(job_class)
      job_class.process_remora(queue, attachment)
    end
  rescue
  end
  job
end
push(queue, item) click to toggle source
# File lib/resque/plugins/remora/push_pop.rb, line 14
def push(queue, item)
  job_class = constantize(item[:class])
  item = job_class.remora_attachment.merge(item) if remora_class?(job_class)
  original_push queue, item
end

Private Instance Methods

_constantize(camel_cased_word) click to toggle source

From file activesupport/lib/active_support/inflector/methods.rb, line 226

# File lib/resque/plugins/remora/push_pop.rb, line 49
def _constantize(camel_cased_word)
  names = camel_cased_word.split('::')

  # Trigger a builtin NameError exception including the ill-formed constant in the message.
  Object.const_get(camel_cased_word) if names.empty?

  # Remove the first blank element in case of '::ClassName' notation.
  names.shift if names.size > 1 && names.first.empty?

  names.inject(Object) do |constant, name|
    if constant == Object
      constant.const_get(name)
    else
      candidate = constant.const_get(name)
      next candidate if constant.const_defined?(name, false)
      next candidate unless Object.const_defined?(name)

      # Go down the ancestors to check it it's owned
      # directly before we reach Object or the end of ancestors.
      constant = constant.ancestors.inject do |const, ancestor|
        break const    if ancestor == Object
        break ancestor if ancestor.const_defined?(name, false)
        const
      end

      # owner is in Object, so raise
      constant.const_get(name, false)
    end
  end
end
remora_class?(job_class) click to toggle source
# File lib/resque/plugins/remora/push_pop.rb, line 44
def remora_class?(job_class)
  job_class && job_class.respond_to?(:process_remora) && job_class.respond_to?(:attach_remora) && job_class.respond_to?(:remora_attachment)
end