Module: FastRI::Util

Defined in:
lib/fastri/util.rb

Class Method Summary collapse

Class Method Details

.find_homeObject

Returns the home directory (win32-aware).



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/fastri/util.rb', line 80

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_uniqueObject

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).



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/fastri/util.rb', line 35

def gem_directories_unique
  return [] unless defined? Gem
  gemdirs = Dir["#{Gem.path}/doc/*/ri"]
  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) ⇒ Object

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



56
57
58
59
60
# File 'lib/fastri/util.rb', line 56

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) ⇒ Object

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



65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/fastri/util.rb', line 65

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