Method: YARD::CodeObjects::Base#relative_path

Defined in:
lib/yard/code_objects/base.rb

#relative_path(other) ⇒ String

Returns the shortest relative path from this object to other.

Parameters:

  • other (Base, String)

    another code object (or object path)

Returns:

  • (String)

    the shortest relative path from this object to other

Since:

  • 0.5.3



475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
# File 'lib/yard/code_objects/base.rb', line 475

def relative_path(other)
  other = Registry.at(other) if String === other && Registry.at(other)
  same_parent = false
  if other.respond_to?(:path)
    same_parent = other.parent == parent
    other = other.path
  end
  return other unless namespace
  common = [path, other].join(" ").match(/^(\S*)\S*(?: \1\S*)*$/)[1]
  common = path unless common =~ /(\.|::|#)$/
  common = common.sub(/(\.|::|#)[^:#\.]*?$/, '') if same_parent
  suffix = %w(. :).include?(common[-1, 1]) || other[common.size, 1] == '#' ?
    '' : '(::|\.)'
  result = other.sub(/^#{Regexp.quote common}#{suffix}/, '')
  result.empty? ? other : result
end