module Roda::RodaPlugins::RelativePath::InstanceMethods

def relative_path(absolute_path)

of the request by adding the appropriate prefix.
Return a relative path for the absolute path based on the current path
def relative_path(absolute_path)
  relative_prefix + absolute_path
end

def relative_prefix

based on the current path of the request.
Return a relative prefix to append to an absolute path to a relative path
def relative_prefix
  return @_relative_prefix if @_relative_prefix
  env = @_request.env
  script_name = env["SCRIPT_NAME"]
  path_info = env["PATH_INFO"]
  # Check path begins with slash.  All valid paths should, but in case this
  # request is bad, just skip using a relative prefix.
  case script_name.getbyte(0)
  when nil # SCRIPT_NAME empty
    unless path_info.getbyte(0) == 47 # PATH_INFO starts with /
      return(@_relative_prefix = '')
    end
  when 47 # SCRIPT_NAME starts with /
    # nothing
  else
    return(@_relative_prefix = '')
  end
  slash_count = script_name.count('/') + path_info.count('/')
  @_relative_prefix = if slash_count > 1
    ("../" * (slash_count - 2)) << ".."
  else
    "."
  end
end