1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
|
# frozen_string_literal: true
require "mime/types/logger"
class << MIME::Types
# Used to mark a method as deprecated in the mime-types interface.
def deprecated(options = {}, &block) # :nodoc:
message =
if options[:message]
options[:message]
else
klass = options.fetch(:class)
msep = case klass
when Class, Module
"."
else
klass = klass.class
"#"
end
method = "#{klass}#{msep}#{options.fetch(:method)}"
pre = " #{options[:pre]}" if options[:pre]
post = case options[:next]
when :private, :protected
" and will be made #{options[:next]}"
when :removed
" and will be removed"
when nil, ""
nil
else
" #{options[:next]}"
end
<<-WARNING.chomp.strip
#{caller(2..2).first}: #{klass}#{msep}#{method}#{pre} is deprecated#{post}.
WARNING
end
if !__deprecation_logged?(message, options[:once])
MIME::Types.logger.__send__(options[:level] || :debug, message)
end
return unless block
block.call
end
private
def __deprecation_logged?(message, once)
return false unless once
@__deprecations_logged = {} unless defined?(@__deprecations_logged)
@__deprecations_logged.key?(message)
end
end
|