module FastRI::Util

Public Class Methods

change_query_method_type(query) click to toggle source
# File lib/fastri/util.rb, line 114
def change_query_method_type(query)
  if md = /\A(.*)(#|\.|::)([^#.:]+)\z/.match(query)
    namespace, sep, meth = md.captures
    case sep
    when /::/ then "#{namespace}##{meth}"
    when /#/ then "#{namespace}::#{meth}"
    else 
      query
    end
  else
    query
  end
end
find_home() click to toggle source

Returns the home directory (win32-aware).

# File lib/fastri/util.rb, line 94
def find_home
  # stolen from RubyGems
  ['HOME', 'USERPROFILE'].each do |homekey|
    return ENV[homekey] if ENV[homekey]
  end
  if ENV['HOMEDRIVE'] && ENV['HOMEPATH']
    return "#{ENV['HOMEDRIVE']}:#{ENV['HOMEPATH']}"
  end
  begin
    File.expand_path("~")
  rescue StandardError => ex
    if File::ALT_SEPARATOR
      "C:/"
    else
      "/"
    end
  end
end
gem_directories_unique() click to toggle source

Return an array of [name, version, path] arrays corresponding to the last version of each installed gem. path is the base path of the RI documentation from the gem. If the version cannot be determined, it will be nil, and the corresponding gem might be repeated in the output array (once per version).

# File lib/fastri/util.rb, line 49
def gem_directories_unique
  return [] unless defined? Gem
  gemdirs = Gem.path.map{|p| Dir["#{p}/doc/*/ri"]}.flatten
  gems = Hash.new{|h,k| h[k] = []}
  gemdirs.each do |path|
    gemname, version = %r{/([^/]+)-([^-]*)/ri$}.match(path).captures
    if gemname.nil? # doesn't follow any conventions :(
      gems[path[%r{/([^/]+)/ri$}, 1]] << [nil, path]
    else
      gems[gemname] << [version, path]
    end
  end
  gems.sort_by{|name, _| name}.map do |name, versions|
    version, path = versions.sort.last
    [name, version, File.expand_path(path)]
  end
end
gem_info_for_path(path, gem_dir_info = FastRI::Util.gem_directories_unique) click to toggle source

Return the [name, version, path] array for the gem owning the RI information stored in path, or nil.

# File lib/fastri/util.rb, line 70
def gem_info_for_path(path, gem_dir_info = FastRI::Util.gem_directories_unique)
  path = File.expand_path(path)
  matches = gem_dir_info.select{|name, version, gem_path| path.index(gem_path) == 0}
  matches.sort_by{|name, version, gem_path| [gem_path.size, version, name]}.last
end
gem_relpath_to_full_name(relpath) click to toggle source

Return the full_name (in ClassEntry or MethodEntry's sense) given a path to a .yaml file relative to a “base RI DB path”.

# File lib/fastri/util.rb, line 79
def gem_relpath_to_full_name(relpath)
  case relpath
  when %r{^(.*)/cdesc-([^/]*)\.yaml$}
    path, name = $~.captures
    (path.split(%r{/})[0..-2] << name).join("::")
  when %r{^(.*)/([^/]*)-(i|c)\.yaml$}
    path, escaped_name, type = $~.captures
    name = RI::RiWriter.external_to_internal(escaped_name)
    sep = ( type == 'c' ) ? "." : "#"
    path.gsub("/", "::") + sep + name
  end
end