Sha256: 1dcdc012a7416c439feff5d137cd3774977f45f3d7ed0d4aabeb5e06883bac6b

Contents?: true

Size: 1.33 KB

Versions: 1

Compression:

Stored size: 1.33 KB

Contents

# frozen_string_literal: true

unless "".respond_to?(:safe_constantize)
  require "active_delivery/ext/string_constantize"
  using ActiveDelivery::Ext::StringConstantize
end

module ActiveDelivery
  module Lines
    # AbstractNotifier line for Active Delivery.
    #
    # You must provide custom `resolver` to infer notifier class
    # (if String#safe_constantize is defined, we convert "*Delivery" -> "*Notifier").
    #
    # Resolver is a callable object.
    class Notifier < ActiveDelivery::Lines::Base
      DEFAULT_SUFFIX = "Notifier"

      def initialize(**opts)
        super
        @resolver = opts.fetch(:resolver, build_resolver(options.fetch(:suffix, DEFAULT_SUFFIX)))
      end

      def resolve_class(klass)
        resolver&.call(klass)
      end

      def notify?(method_name)
        return unless handler_class
        handler_class.action_methods.include?(method_name.to_s)
      end

      def notify_now(handler, mid, *args)
        handler.public_send(mid, *args).notify_now
      end

      def notify_later(handler, mid, *args)
        handler.public_send(mid, *args).notify_later
      end

      private

      attr_reader :resolver

      def build_resolver(suffix)
        lambda do |klass|
          klass_name = klass.name
          klass_name&.sub(/Delivery\z/, suffix)&.safe_constantize
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
active_delivery-1.0.0.rc2 lib/active_delivery/lines/notifier.rb