Sha256: 84879ec7fad667dc1a6651ae87d40e4dd5080ee34552c7fe3f203c62db8423c2

Contents?: true

Size: 946 Bytes

Versions: 1

Compression:

Stored size: 946 Bytes

Contents

# Provides a way of wrapping another broadcaster with logging

module Wisper
  module Broadcasters
    class LoggerBroadcaster
      def initialize(logger, broadcaster)
        @logger      = logger
        @broadcaster = broadcaster
      end

      def broadcast(subscriber, publisher, event, args)
        @logger.info("[WISPER] #{name(publisher)} published #{event} to #{name(subscriber)} with #{arg_names(args)}")
        @broadcaster.broadcast(subscriber, publisher, event, args)
      end

      private

      def name(object)
        id_method  = %w(id uuid key object_id).find { |possibility| object.respond_to?(possibility) }
        id         = object.send(id_method)
        class_name = object.class == Class ? object.name : object.class.name
        "#{class_name}##{id}"
      end

      def arg_names(args)
        return 'no arguments' if args.empty?
        args.map { |arg| name(arg) }.join(', ')
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
wisper-1.6.1 lib/wisper/broadcasters/logger_broadcaster.rb