class Uncruft::DeprecationHandler

Public Instance Methods

arity() click to toggle source
# File lib/uncruft/deprecation_handler.rb, line 11
def arity
  2
end
call(message, _callstack) click to toggle source
# File lib/uncruft/deprecation_handler.rb, line 5
def call(message, _callstack)
  line_number = line_number(message)
  message = normalize_message(message)
  handle_unknown_deprecation!(message, line_number) unless known_deprecations.include?(message)
end

Private Instance Methods

absolute_path(message) click to toggle source
# File lib/uncruft/deprecation_handler.rb, line 70
def absolute_path(message)
  message.match(/called from( .+ at)? (.+):\d/)&.[](2)
end
error_message(message, line_number) click to toggle source
# File lib/uncruft/deprecation_handler.rb, line 82
    def error_message(message, line_number)
      <<~ERROR.strip
        #{message}:#{line_number}

        To resolve this error, adjust your code according to the instructions above.
        If you did not introduce this error or are unsure why you are seeing it,
        you will find additional guidance at the URL below:
        https://github.com/Betterment/uncruft/blob/main/GUIDE.md
      ERROR
    end
file_content(deprecations) click to toggle source
# File lib/uncruft/deprecation_handler.rb, line 108
def file_content(deprecations)
  JSON.pretty_generate ignored_warnings: deprecations.sort,
                       updated: now,
                       rails_version: Rails::VERSION::STRING
end
gem_home(message) click to toggle source
# File lib/uncruft/deprecation_handler.rb, line 66
def gem_home(message)
  message.match(%r{called from( .+ at)? (#{ENV['GEM_HOME']}/(.+/)*gems)})&.[](2)
end
handle_unknown_deprecation!(message, line_number) click to toggle source
# File lib/uncruft/deprecation_handler.rb, line 17
def handle_unknown_deprecation!(message, line_number)
  if Uncruft.record_deprecations?
    known_deprecations << message
    write_deprecations_file!
  else
    raise error_message(message, line_number)
  end
end
known_deprecations() click to toggle source
# File lib/uncruft/deprecation_handler.rb, line 97
def known_deprecations
  @known_deprecations ||= begin
    if known_deprecations_file_exists?
      file = File.read(Uncruft.ignorefile_path)
      JSON.parse(file)['ignored_warnings'].to_set
    else
      Set.new
    end
  end
end
known_deprecations_file_exists?() click to toggle source
# File lib/uncruft/deprecation_handler.rb, line 93
def known_deprecations_file_exists?
  File.file?(Uncruft.ignorefile_path)
end
line_number(message) click to toggle source
# File lib/uncruft/deprecation_handler.rb, line 32
def line_number(message)
  message.match(/called from( .+ at)? .+:(\d+)/)&.[](2)
end
normalize_caller(message) click to toggle source
# File lib/uncruft/deprecation_handler.rb, line 50
def normalize_caller(message)
  normalize_require_callers(remove_view_callers(message))
end
normalize_callstack_path(message) click to toggle source
# File lib/uncruft/deprecation_handler.rb, line 42
def normalize_callstack_path(message)
  if (gem_home = gem_home(message)).present?
    message.gsub(gem_home, '$GEM_PATH')
  elsif (absolute_path = absolute_path(message)).present?
    message.gsub(absolute_path, relative_path(absolute_path))
  end
end
normalize_message(message) click to toggle source

Rails deprecation message formats found here: github.com/rails/rails/blob/5-0-stable/activesupport/lib/active_support/deprecation/reporting.rb#L75

# File lib/uncruft/deprecation_handler.rb, line 38
def normalize_message(message)
  remove_line_number(normalize_caller(normalize_callstack_path(message)))
end
normalize_require_callers(message) click to toggle source
# File lib/uncruft/deprecation_handler.rb, line 54
def normalize_require_callers(message)
  message.gsub(/ <(top \(required\)|main)> at /, ' <global scope> at ')
end
now() click to toggle source
# File lib/uncruft/deprecation_handler.rb, line 114
def now
  if defined?(Timecop)
    Timecop.return { Time.zone.now }
  else
    Time.zone.now
  end
end
relative_path(absolute_path) click to toggle source
# File lib/uncruft/deprecation_handler.rb, line 74
def relative_path(absolute_path)
  Pathname.new(absolute_path)
    .relative_path_from(Rails.root).to_s
    .gsub(%r{\A(../)*vendor/cache}, '$GEM_PATH')
rescue ArgumentError # When `relative_path_from` cannot find a relative path.
  absolute_path
end
remove_line_number(message) click to toggle source
# File lib/uncruft/deprecation_handler.rb, line 62
def remove_line_number(message)
  message.sub(/(called from( .+ at)? .+):\d+/, '\1')
end
remove_view_callers(message) click to toggle source
# File lib/uncruft/deprecation_handler.rb, line 58
def remove_view_callers(message)
  message.gsub(/ _\w+__+\d+_\d+ at /, ' ')
end
write_deprecations_file!() click to toggle source
# File lib/uncruft/deprecation_handler.rb, line 26
def write_deprecations_file!
  file = File.open(Uncruft.ignorefile_path, 'w')
  file.puts(file_content(known_deprecations))
  file.close
end