Sha256: d20e88557f730546e523b6918302ce164877d3ce748d603890e84991d984bdbc

Contents?: true

Size: 1.69 KB

Versions: 4

Compression:

Stored size: 1.69 KB

Contents

require 'sweet_notifications/version'
require 'sweet_notifications/log_subscriber'
require 'sweet_notifications/controller_runtime'
require 'sweet_notifications/railtie'

# Syntactic sugar for ActiveSupport::Notifications subscribers for logging
# purposes in Rails.
module SweetNotifications
  extend ControllerRuntime
  extend Railtie

  # Subscribe to an ActiveSupport::Notifications namespace.
  #
  # This will subscribe to the namespace given as argument and, if necessary,
  # create a Rails initializer that will be run when the application is
  # initialized.
  #
  # @param name [Symbol] event namespace
  # @param label [String] optional label for logging
  # @return [Rails::Railtie, ActiveSupport::LogSubscriber] An array consisting
  #   of a Railtie and a LogSubscriber
  # @yield event subscription
  #
  # ==== Examples
  #
  #  SweetNotifications.subscribe :active_record do
  #    color ActiveSupport::LogSubscriber::GREEN
  #    event :sql, runtime: true do |event|
  #      return unless logger.debug?
  #      debug message(event, event.payload[:name], event.payload[:sql])
  #    end
  #  end
  def self.subscribe(name, label: nil, &block)
    label ||= name
    log_subscriber = Class.new(SweetNotifications::LogSubscriber, &block)
    controller_runtime = self.controller_runtime(label, log_subscriber)
    if rails_initialized?
      initialize_rails(name, log_subscriber, controller_runtime)
      [nil, log_subscriber]
    else
      [railtie(name, log_subscriber, controller_runtime), log_subscriber]
    end
  end

  def self.rails_initialized?
    Rails.respond_to?(:application) &&
      Rails.application &&
      Rails.application.initialized?
  end

  private_class_method :rails_initialized?
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
sweet_notifications-1.1.1 lib/sweet_notifications.rb
sweet_notifications-1.1.0 lib/sweet_notifications.rb
sweet_notifications-1.0.1 lib/sweet_notifications.rb
sweet_notifications-1.0.0 lib/sweet_notifications.rb