Sha256: 0295ce155388a1dfcdb772e9e9dc1f001ac9308110c90206cfdc3e7add8143ea

Contents?: true

Size: 1.45 KB

Versions: 1

Compression:

Stored size: 1.45 KB

Contents

# frozen_string_literal: true

require "active_support/concern"

module Stenotype
  module Frameworks
    #
    # A namespace containing extensions for Ruby on Rails components
    #
    module Rails
      #
      # An extension for ActiveJob to enable adding a hook
      # before performing an instance of [ActiveJob::Base] subclass
      #
      module ActiveJobExtension
        # @!visibility private
        def self.extended(base)
          base.const_set(:JobExt, Module.new)
          super
        end

        #
        # @example
        #   class MyJob < ApplicationJob
        #     trackable_job! # => will prepend a perform action with event recorder
        #
        #     def perform(data)
        #       # do_something
        #     end
        #   end
        #
        #
        def trackable_job!
          proxy = const_get(:JobExt)
          proxy.module_eval do
            define_method(:perform) do |*args, **rest_args, &block|
              Stenotype::Event.emit!(
                "active_job_#{self.class.name}",
                { type: "active_job" },
                **{ eval_context: { active_job: self }},
              )
              super(*args, **rest_args, &block)
            end
          end

          # Prepend an instance of module so that
          # super() can be chained down the ancestors
          # without changing existing ActiveJob interface
          #
          public_send(:prepend, proxy)
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
stenotype-0.1.19 lib/stenotype/frameworks/rails/active_job.rb