Sha256: 58750696efc7e14b1b6f6a26130d26654f2b73c825b832ef35f1d5eeab4f4871

Contents?: true

Size: 1.02 KB

Versions: 2

Compression:

Stored size: 1.02 KB

Contents

# frozen_string_literal: true

module Uncruft
  module Warning
    DEPRECATION_PATTERN = /(deprecation|deprecated)/i

    def warn(str, *args, **kwargs)
      if str =~ DEPRECATION_PATTERN # rubocop:disable Performance/RegexpMatch
        message = strip_caller_info(str, caller_locations(1..1).first).strip
        ActiveSupport::Deprecation.warn(message)
      elsif RUBY_VERSION < '2.7' && kwargs.empty?
        super(str, *args)
      else
        super
      end
    end

    private

    def strip_caller_info(str, cloc)
      str.sub(cloc.to_s, '') # try full caller information first
        .gsub(/#{cloc.path}(:#{cloc.lineno})?:?\s*/, '') # try path with optional line
    end
  end
end

if Rails.env.development? || Rails.env.test?
  if defined?(Warning)
    Warning.prepend(Uncruft::Warning)
    Warning.singleton_class.prepend(Uncruft::Warning)
  end
  Kernel.prepend(Uncruft::Warning)
  Kernel.singleton_class.prepend(Uncruft::Warning)
  Object.prepend(Uncruft::Warning)
  Object.singleton_class.prepend(Uncruft::Warning)
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
uncruft-0.5.0 lib/uncruft/warning.rb
uncruft-0.4.0 lib/uncruft/warning.rb