Skip to content

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
# app/models/queue/base.rb
class Queue::Base
class << self
def enqueue(object, method, *args)
meta = { 'method' => method }
ensure_queueable!(object, method, *args)
if is_model?(object)
Resque.enqueue(self, meta.merge('class' => object.class.name, 'id' => object.id), *args)
else
Resque.enqueue(self, meta.merge('class' => object.name), *args)
end
end
def perform(meta = { }, *args)
if meta.has_key?('id')
if model = meta['class'].constantize.find_by_id(meta['id'])
model.send(meta['method'], *args)
end
else
meta['class'].constantize.send(meta['method'], *args)
end
end
def is_model?(object)
object.class.respond_to?(:find_by_id)
end
private
def ensure_queueable!(object, method, *args)
ensure_responds_to!(object, method)
ensure_arity!(object, method, args.length)
end
def ensure_responds_to!(object, method)
unless object.respond_to?(method)
raise "object must respond to #{method}"
end
end
def ensure_arity!(object, method, arity)
required = object.method(method).arity
if required < 0 && arity < -required
raise "#{method}: #{arity} of #{-required} arguments given"
elsif required >= 0 && required != arity
raise "#{method}: #{arity} of #{required} arguments given"
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.