class AbstractNotifier::Base

Base class for notifiers

Attributes

driver[W]
notification_name[R]
params[R]

Public Class Methods

action_methods() click to toggle source

See github.com/rails/rails/blob/b13a5cb83ea00d6a3d71320fd276ca21049c2544/actionpack/lib/abstract_controller/base.rb#L74

# File lib/abstract_notifier/base.rb, line 132
def action_methods
  @action_methods ||= begin
    # All public instance methods of this class, including ancestors
    methods = (public_instance_methods(true) -
      # Except for public instance methods of Base and its ancestors
      Base.public_instance_methods(true) +
      # Be sure to include shadowed public instance methods of this class
      public_instance_methods(false))

    methods.map!(&:to_s)

    methods.to_set
  end
end
async_adapter() click to toggle source
# File lib/abstract_notifier/base.rb, line 71
def async_adapter
  return @async_adapter if instance_variable_defined?(:@async_adapter)

  @async_adapter =
    if superclass.respond_to?(:async_adapter)
      superclass.async_adapter
    else
      AbstractNotifier.async_adapter
    end
end
async_adapter=(args) click to toggle source
# File lib/abstract_notifier/base.rb, line 66
def async_adapter=(args)
  adapter, options = Array(args)
  @async_adapter = AsyncAdapters.lookup(adapter, options)
end
default(method_name = nil, **hargs, &block) click to toggle source
# File lib/abstract_notifier/base.rb, line 82
def default(method_name = nil, **hargs, &block)
  return @defaults_generator = block if block_given?

  return @defaults_generator = proc { send(method_name) } unless method_name.nil?

  @default_params =
    if superclass.respond_to?(:default_params)
      superclass.default_params.merge(hargs).freeze
    else
      hargs.freeze
    end
end
default_params() click to toggle source
# File lib/abstract_notifier/base.rb, line 104
def default_params
  return @default_params if instance_variable_defined?(:@default_params)

  @default_params =
    if superclass.respond_to?(:default_params)
      superclass.default_params.dup
    else
      {}
    end
end
defaults_generator() click to toggle source
# File lib/abstract_notifier/base.rb, line 95
def defaults_generator
  return @defaults_generator if instance_variable_defined?(:@defaults_generator)

  @defaults_generator =
    if superclass.respond_to?(:defaults_generator)
      superclass.defaults_generator
    end
end
driver() click to toggle source
# File lib/abstract_notifier/base.rb, line 54
def driver
  return @driver if instance_variable_defined?(:@driver)

  @driver =
    if superclass.respond_to?(:driver)
      superclass.driver
    else
      raise "Driver not found for #{name}. " \
            "Please, specify driver via `self.driver = MyDriver`"
    end
end
method_missing(method_name, *args) click to toggle source
Calls superclass method
# File lib/abstract_notifier/base.rb, line 115
def method_missing(method_name, *args)
  if action_methods.include?(method_name.to_s)
    new(method_name).public_send(method_name, *args)
  else
    super
  end
end
new(notification_name, **params) click to toggle source
# File lib/abstract_notifier/base.rb, line 150
def initialize(notification_name, **params)
  @notification_name = notification_name
  @params = params.freeze
end
respond_to_missing?(method_name, _include_private = false) click to toggle source
Calls superclass method
# File lib/abstract_notifier/base.rb, line 127
def respond_to_missing?(method_name, _include_private = false)
  action_methods.include?(method_name.to_s) || super
end
with(params) click to toggle source
# File lib/abstract_notifier/base.rb, line 123
def with(params)
  ParamsProxy.new(self, params)
end

Public Instance Methods

notification(**payload) click to toggle source
# File lib/abstract_notifier/base.rb, line 155
def notification(**payload)
  merge_defaults!(payload)

  raise ArgumentError, "Notification body must be present" if
    payload[:body].nil? || payload[:body].empty?
  Notification.new(self.class, payload)
end

Private Instance Methods

merge_defaults!(payload) click to toggle source
# File lib/abstract_notifier/base.rb, line 165
def merge_defaults!(payload)
  defaults =
    if self.class.defaults_generator
      instance_exec(&self.class.defaults_generator)
    else
      self.class.default_params
    end

  defaults.each do |k, v|
    payload[k] = v unless payload.key?(k)
  end
end