Sha256: 00ea90be174f7090ccbfe997aecd8ca3bc67594e72df581dcf68ba694c1a3100

Contents?: true

Size: 1.28 KB

Versions: 3

Compression:

Stored size: 1.28 KB

Contents

# frozen_string_literal: true

# @api private
# @since 0.3.0
module AnyCache::Delegation
  class << self
    # @param base_klass [Class]
    # @return [void]
    #
    # @api private
    # @since 0.3.0
    def included(base_klass)
      base_klass.extend(ClassMethods)
    end
  end

  # @api private
  # @since 0.3.0
  module ClassMethods
    # @param receiver [Symbol, String]
    # @param delegators [Array<Symbol, String>]
    # @return [void]
    #
    # @api private
    # @since 0.3.0
    def def_loggable_delegators(receiver, *delegators)
      delegators.each { |delegat| def_loggable_delegator(receiver, delegat) }
    end

    # @param receiver [Symbol, String]
    # @param delegat [Symbol, String]
    # @return [void]
    #
    # @api private
    # @since 0.3.0
    def def_loggable_delegator(receiver, delegat)
      define_method(delegat) do |*args, **opts, &block|
        send(receiver).send(delegat, *args, **opts, &block).tap do
          shared_config[:logger].tap do |logger|
            AnyCache::Logging::Activity.log(
              self, logger, activity: delegat, message:
                "performed <#{delegat}> operation with " \
                "attributes: #{args.inspect} and options: #{opts.inspect}."
            ) if logger
          end
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
any_cache-0.6.0 lib/any_cache/delegation.rb
any_cache-0.5.0 lib/any_cache/delegation.rb
any_cache-0.4.0 lib/any_cache/delegation.rb