Sha256: 0b546d4b35f8e93352fbd39d16ad6fb132ae71780db29906b404874aaaf4ddeb

Contents?: true

Size: 1.05 KB

Versions: 3

Compression:

Stored size: 1.05 KB

Contents

module CLIUtils
  # LoggerDelegator Class
  # Delegates certain Logger methods to a number of different
  # targets.
  class LoggerDelegator
    attr_reader :targets

    # Initializes and creates methods for the passed targets.
    # @param [Logger] targets The endpoints to delegate to
    # @return [void]
    def initialize(*targets)
      @targets = targets
      LoggerDelegator.delegate
    end

    # Attaches a new target to delegate to.
    # @param [Logger] target The targets to delegate to
    # @return [void]
    def attach(target)
      @targets << target
      LoggerDelegator.delegate
    end

    # Creates delegator methods for a specific list of Logger
    # functions.
    # @return [void]
    def self.delegate
      %w(log debug info warn error section success).each do |m|
        define_method(m) do |*args|
          @targets.map { |t| t.send(m, *args) }
        end
      end
    end

    # Detaches a delegation target.
    # @return [void]
    def detach(target)
      @targets.delete(target)
      LoggerDelegator.delegate
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
cliutils-1.0.5 lib/cliutils/logger-delegator.rb
cliutils-1.0.4 lib/cliutils/logger-delegator.rb
cliutils-1.0.3 lib/cliutils/logger-delegator.rb