Sha256: a79f88309223ff29ec3aad5dd3db452cd8a793090c1127d5126a714fc5fdcabc

Contents?: true

Size: 1.65 KB

Versions: 6

Compression:

Stored size: 1.65 KB

Contents

# frozen_string_literal: true

require 'dry/core/class_attributes'

module ROM
  module Plugins
    module Relation
      # Experimental plugin for configuring relations with an external
      # instrumentation system like dry-monitor or ActiveSupport::Notifications
      #
      # @api public
      module Instrumentation
        extend Dry::Core::ClassAttributes

        # This hooks sets up a relation class with injectible notifications object
        #
        # @api private
        def self.included(klass)
          super
          klass.option :notifications
          klass.extend(ClassInterface)
          klass.prepend(mixin)
          klass.instrument(:to_a)
        end

        defines :mixin
        mixin Module.new

        # Instrumentation extension for relation classes
        #
        # @api private
        module ClassInterface
          # Configure provided methods for instrumentation
          #
          # @param [Array<Symbol>] methods A list of method names
          #
          # @api public
          def instrument(*methods)
            (methods - Instrumentation.mixin.instance_methods).each do |meth|
              Instrumentation.mixin.send(:define_method, meth) do
                instrument { super() }
              end
            end
          end
        end

        # Execute a block using instrumentation
        #
        # @api public
        def instrument(&block)
          notifications.instrument(self.class.adapter, name: name.relation, **notification_payload(self), &block)
        end

        private

        # @api private
        def notification_payload(relation)
          EMPTY_HASH
        end
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
rom-core-5.1.2 lib/rom/plugins/relation/instrumentation.rb
rom-core-5.1.1 lib/rom/plugins/relation/instrumentation.rb
rom-core-5.1.0 lib/rom/plugins/relation/instrumentation.rb
rom-core-5.0.2 lib/rom/plugins/relation/instrumentation.rb
rom-core-5.0.1 lib/rom/plugins/relation/instrumentation.rb
rom-core-5.0.0 lib/rom/plugins/relation/instrumentation.rb