Sha256: 5ff03d8c957865ab4872ba44264bb9f375e5e027cb7973f6eb8df6b0794aef22

Contents?: true

Size: 1.62 KB

Versions: 1

Compression:

Stored size: 1.62 KB

Contents

module Quebert
  module AsyncSender
    # Perform jobs on instances of classes
    module Instance
      class InstanceJob < Job
        def perform(klass, init_args, meth, args)
          Support.constantize(klass).new(init_args).send(meth, *args)
        end
      end
      
      def self.included(base)
        # Its not as simple as including initialize in a class, we
        # have to do some tricks to make it work so we can put the include
        # before the initialize method as opposed to after. Ah, and thanks PivotalLabs for this.
        base.extend ClassMethods
        base.overwrite_initialize
        base.instance_eval do
          def method_added(name)
            return if name != :initialize
            overwrite_initialize
          end
        end
      end
      
      def initialize_with_async_sender(*args)
        initialize_without_async_sender(*(@_init_args = args))
      end
      
      module ClassMethods
        def overwrite_initialize
          class_eval do
            unless method_defined?(:initialize_with_async_sender)
              define_method(:initialize_with_async_sender) do
                initialize_without_async_sender
              end
            end
            
            if instance_method(:initialize) != instance_method(:initialize_with_async_sender)
              alias_method :initialize_without_async_sender, :initialize
              alias_method :initialize, :initialize_with_async_sender
            end
          end
        end
      end
      
      def async_send(meth, *args)
        InstanceJob.new(self.class.name, @_init_args, meth, args).enqueue
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
quebert-0.0.6 lib/quebert/async_sender/instance.rb