Skip to content

Commit

Permalink
Use Kernel#caller_locations (2.x) (#1491)
Browse files Browse the repository at this point in the history
Ruby 2.x+ has a built-in implementation of `caller_locations`,
which differs slightly from the Sinatra implementation of the same name.
This removes the customized implementation in favor of the system one

Co-authored-by: Jordan Owens <jkowens@gmail.com>
  • Loading branch information
julik and jkowens authored Oct 6, 2021
1 parent 0d0e938 commit 982ad7e
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 11 deletions.
13 changes: 4 additions & 9 deletions lib/sinatra/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -898,9 +898,10 @@ def compile_template(engine, data, options, views)
end

def compile_block_template(template, options, &body)
caller = settings.caller_locations.first
path = options[:path] || caller[0]
line = options[:line] || caller[1]
first_location = caller_locations.first
path, line = first_location.path, first_location.lineno
path = options[:path] || path
line = options[:line] || line
template.new(path, line.to_i, options, &body)
end
end
Expand Down Expand Up @@ -1551,12 +1552,6 @@ def caller_files
cleaned_caller(1).flatten
end

# Like caller_files, but containing Arrays rather than strings with the
# first element being the file, and the second being the line.
def caller_locations
cleaned_caller 2
end

private

# Starts the server by running the Rack Handler.
Expand Down
5 changes: 3 additions & 2 deletions sinatra-contrib/lib/sinatra/namespace.rb
Original file line number Diff line number Diff line change
Expand Up @@ -299,8 +299,9 @@ def disable(*opts)
end

def template(name, &block)
filename, line = caller_locations.first
templates[name] = [block, filename, line.to_i]
first_location = caller_locations.first
filename, line = first_location.path, first_location.lineno
templates[name] = [block, filename, line]
end

def layout(name=:layout, &block)
Expand Down

0 comments on commit 982ad7e

Please sign in to comment.