Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use Kernel#caller_locations (2.x) #1491

Merged
merged 6 commits into from
Oct 6, 2021
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Remove the previous implementation
  • Loading branch information
julik authored and jkowens committed Oct 4, 2021
commit ac1c25124a583ebf5136e09645cc06b2f9b4e796
27 changes: 3 additions & 24 deletions lib/sinatra/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -266,22 +266,6 @@ class NotFound < Error #:nodoc:
def http_status; 404 end
end

module CallerLocationsShim
# Uses as a substitute for the built-in Thread::Backtrace::Location
# on Ruby versions that do not support / do not provide it
class Location < Struct.new(:path, :lineno)
end

# Like caller_files, but returning CallerLocationShim objects
# which work similar to https://ruby-doc.org/core-2.2.0/Thread/Backtrace/Location.html
def caller_locations(*)
call_sites = cleaned_caller 2
call_sites.map do |(path, lineno)|
Location.new(path, lineno)
end
end
end

# Methods available to routes, before/after filters, and views.
module Helpers
# Set or retrieve the response status code.
Expand Down Expand Up @@ -1573,14 +1557,9 @@ def caller_files
cleaned_caller(1).flatten
end

# In Ruby 2.x+ a built-in implementation is provided for caller_locations.
# If we do not have one, use the previous variant and provide a stub
# for the Location objects returned by the Kernel implementation
if private_instance_methods.include?(:caller_locations)
public :caller_locations
else
include CallerLocationsShim
end
# In Ruby 2.x+ a built-in implementation is provided for caller_locations,
# which is used to tag templates
public :caller_locations
jkowens marked this conversation as resolved.
Show resolved Hide resolved

private

Expand Down