Skip to content
bnorton edited this page Feb 18, 2013 · 1 revision

##Custom Loaders

A Loader is a way of transforming a class, into an instance at message runtime. This instance is used as the receiver of the message invocation.

##Examples

###ActiveRecord When asynchronously updating a User

# controllers/users_controller.rb#update
current_user.update(params[:token])
current_user.id
#=> 123

To load a User record from the database at runtime, call find on the User class with the user's id

A message in the queue looks like this

{ 'class' => 'User', 'method' => 'update', 'loader' => { 'method' => 'find', 'args' => [123] } }

# In this case the worker essentially calls
User.find(123).update

###Classes that are created with arguments

class CustomWorker
  attr_accessor :type, :something
  def initialize(options)
    options.each {|(opt, value)| send(:"#{opt}=", value) }
  end

  def perform
  end
end

To enqueue a message that passes options to initialize

init_args = { :type => 'message', :something => 'a-value' }
message = {:class => CustomWorker, :method => 'perform', :loader => { :method => 'new', :args => [init_args] } }

MicroQ.push(message)

# In this case the worker essentially calls
CustomWorker.new('type' => 'message', 'something' => 'a-value').perform
Clone this wiki locally