Sha256: f93065949850600ff6e5051ea678f96ff3f54d652822342e8063293104af0b89

Contents?: true

Size: 1.14 KB

Versions: 12

Compression:

Stored size: 1.14 KB

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(listener, publisher, event, args)
        @logger.info("[WISPER] #{name(publisher)} published #{event} to #{name(listener)} with #{args_info(args)}")
        @broadcaster.broadcast(listener, publisher, event, args)
      end

      private

      def name(object)
        id_method  = %w(id uuid key object_id).find do |method_name|
          object.respond_to?(method_name) && object.method(method_name).arity <= 0
        end
        id         = object.send(id_method)
        class_name = object.class == Class ? object.name : object.class.name
        "#{class_name}##{id}"
      end

      def args_info(args)
        return 'no arguments' if args.empty?
        args.map do |arg|
          arg_string = name(arg)
          arg_string += ": #{arg.inspect}" if [Numeric, Array, Hash, String].any? {|klass| arg.is_a?(klass) }
          arg_string
        end.join(', ')
      end
    end
  end
end

Version data entries

12 entries across 12 versions & 4 rubygems

Version Path
lotrd-0.1.9 vendor/cache/ruby/2.6.0/gems/wisper-2.0.1/lib/wisper/broadcasters/logger_broadcaster.rb
lotrd-0.1.8 vendor/cache/ruby/2.6.0/gems/wisper-2.0.1/lib/wisper/broadcasters/logger_broadcaster.rb
lotrd-0.1.6 vendor/cache/ruby/2.6.0/gems/wisper-2.0.1/lib/wisper/broadcasters/logger_broadcaster.rb
lotrd-0.1.5 vendor/cache/ruby/2.6.0/gems/wisper-2.0.1/lib/wisper/broadcasters/logger_broadcaster.rb
pokedex-terminal-0.2.8 vendor/bundle/ruby/2.7.0/gems/wisper-2.0.1/lib/wisper/broadcasters/logger_broadcaster.rb
pokedex-terminal-0.2.7 vendor/bundle/ruby/2.7.0/gems/wisper-2.0.1/lib/wisper/broadcasters/logger_broadcaster.rb
pokedex-terminal-0.2.6 vendor/bundle/ruby/2.7.0/gems/wisper-2.0.1/lib/wisper/broadcasters/logger_broadcaster.rb
pokedex-terminal-0.2.5 vendor/bundle/ruby/2.7.0/gems/wisper-2.0.1/lib/wisper/broadcasters/logger_broadcaster.rb
pokedex-terminal-0.2.4 vendor/bundle/ruby/2.7.0/gems/wisper-2.0.1/lib/wisper/broadcasters/logger_broadcaster.rb
wisper-2.0.1 lib/wisper/broadcasters/logger_broadcaster.rb
siftery-wisper-2.0.1 lib/wisper/broadcasters/logger_broadcaster.rb
wisper-2.0.0 lib/wisper/broadcasters/logger_broadcaster.rb