aboutsummaryrefslogblamecommitdiffstats
path: root/actionmailer/lib/action_mailer/queued_message.rb
blob: 8d200617c4513d9645b579e634189316c72d440b (plain) (tree)
1
2
3
4
5
6
7
8
9






                                                           

                                                               


                  
                  

       


                                      

       













                                                                           


       
require 'delegate'

module ActionMailer
  class QueuedMessage < ::Delegator
    attr_reader :queue

    def initialize(queue, mailer_class, method_name, *args)
      @queue = queue
      @job   = DeliveryJob.new(mailer_class, method_name, args)
    end

    def __getobj__
      @job.message
    end

    # Queues the message for delivery.
    def deliver
      tap { @queue.push @job }
    end

    class DeliveryJob
      def initialize(mailer_class, method_name, args)
        @mailer_class = mailer_class
        @method_name  = method_name
        @args         = args
      end

      def message
        @message ||= @mailer_class.send(:new, @method_name, *@args).message
      end

      def run
        message.deliver
      end
    end
  end
end