Sha256: 7a0384cce78f1a808568ef95e02357c252402eeb1a1bfe6eb4f381bf59392644

Contents?: true

Size: 1016 Bytes

Versions: 5

Compression:

Stored size: 1016 Bytes

Contents

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

    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

5 entries across 5 versions & 1 rubygems

Version Path
uncruft-0.3.4 lib/uncruft/warning.rb
uncruft-0.3.3 lib/uncruft/warning.rb
uncruft-0.3.2 lib/uncruft/warning.rb
uncruft-0.3.1 lib/uncruft/warning.rb
uncruft-0.3.0 lib/uncruft/warning.rb