File tree

3 files changed

+106
-21
lines changed

3 files changed

+106
-21
lines changed
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
* The default output encoding for RDoc is now UTF-8. Previously RDoc used
55
the default external encoding which was determined from your locale.
66
Issue #106 by Justin Baker.
7+
* Removed RDoc::RI::Paths::SYSDIR and ::SITEDIR. These were hidden
8+
constants so no breakage is expected. Use RDoc::RI::Paths::system_dir
9+
and ::site_dir instead.
710

811
* Minor enhancements
912
* Added Markdown as a supported format. The markdown format can be set on a
@@ -16,6 +19,8 @@
1619
* RDoc now tracks use of extend. Pull request #118 by Michael Granger.
1720
* RDoc now tracks methods that use super. Pull request #116 by Erik
1821
Hollensbe.
22+
* Added methods ::system_dir, ::site_dir, ::home_dir and ::gem_dir to fetch
23+
the components of RDoc::RI::Paths.path individually.
1924

2025
* Bug fixes
2126
* A word that is directly followed by a multi-word tidy link label no longer
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
require 'rdoc/ri'
22

33
##
4-
# The directories where ri data lives.
4+
# The directories where ri data lives. Paths can be enumerated via ::each, or
5+
# queried individually via ::system_dir, ::site_dir, ::home_dir and ::gem_dir.
56

67
module RDoc::RI::Paths
78

@@ -10,15 +11,12 @@ module RDoc::RI::Paths
1011

1112
version = RbConfig::CONFIG['ruby_version']
1213

13-
base = if RbConfig::CONFIG.key? 'ridir' then
14+
BASE = if RbConfig::CONFIG.key? 'ridir' then
1415
File.join RbConfig::CONFIG['ridir'], version
1516
else
1617
File.join RbConfig::CONFIG['datadir'], 'ri', version
1718
end
1819

19-
SYSDIR = File.join base, "system"
20-
SITEDIR = File.join base, "site"
21-
2220
homedir = begin
2321
File.expand_path('~')
2422
rescue ArgumentError
@@ -47,14 +45,16 @@ module RDoc::RI::Paths
4745
# :extra:: ri data directory from the command line. Yielded for each
4846
# entry in +extra_dirs+
4947

50-
def self.each system, site, home, gems, *extra_dirs # :yields: directory, type
48+
def self.each system = true, site = true, home = true, gems = true, *extra_dirs # :yields: directory, type
49+
return enum_for __method__ unless block_given?
50+
5151
extra_dirs.each do |dir|
5252
yield dir, :extra
5353
end
5454

55-
yield SYSDIR, :system if system
56-
yield SITEDIR, :site if site
57-
yield HOMEDIR, :home if home and HOMEDIR
55+
yield system_dir, :system if system
56+
yield site_dir, :site if site
57+
yield home_dir, :home if home and HOMEDIR
5858

5959
gemdirs.each do |dir|
6060
yield dir, :gem
@@ -63,6 +63,17 @@ def self.each system, site, home, gems, *extra_dirs # :yields: directory, type
6363
nil
6464
end
6565

66+
##
67+
# The ri directory for the gem with +gem_name+.
68+
69+
def self.gem_dir name, version
70+
req = Gem::Requirement.new "= #{version}"
71+
72+
spec = Gem::Specification.find_by_name name, req
73+
74+
File.join spec.doc_dir, 'ri'
75+
end
76+
6677
##
6778
# The latest installed gems' ri directories
6879

@@ -96,13 +107,24 @@ def self.gemdirs
96107
@gemdirs = []
97108
end
98109

110+
##
111+
# The location of the rdoc data in the user's home directory.
112+
#
113+
# Like ::system, ri data in the user's home directory is rare and predates
114+
# libraries distributed via RubyGems. ri data is rarely generated into this
115+
# directory.
116+
117+
def self.home_dir
118+
HOMEDIR
119+
end
120+
99121
##
100122
# Returns existing directories from the selected documentation directories
101123
# as an Array.
102124
#
103125
# See also ::each
104126

105-
def self.path(system, site, home, gems, *extra_dirs)
127+
def self.path(system = true, site = true, home = true, gems = true, *extra_dirs)
106128
path = raw_path system, site, home, gems, *extra_dirs
107129

108130
path.select { |directory| File.directory? directory }
@@ -124,5 +146,29 @@ def self.raw_path(system, site, home, gems, *extra_dirs)
124146
path.compact
125147
end
126148

149+
##
150+
# The location of ri data installed into the site dir.
151+
#
152+
# Historically this was available for documentation installed by ruby
153+
# libraries predating RubyGems. It is unlikely to contain any content for
154+
# modern ruby installations.
155+
156+
def self.site_dir
157+
File.join BASE, 'site'
158+
end
159+
160+
##
161+
# The location of the built-in ri data.
162+
#
163+
# This data is built automatically when `make` is run when ruby is
164+
# installed. If you did not install ruby by hand you may need to install
165+
# the documentation yourself. Please consult the documentation for your
166+
# package manager or ruby installer for details. You can also use the
167+
# rdoc-data gem to install system ri data for common versions of ruby.
168+
169+
def self.system_dir
170+
File.join BASE, 'system'
171+
end
172+
127173
end
128174

Original file line numberDiff line numberDiff line change
@@ -6,40 +6,74 @@ def setup
66
super
77

88
RDoc::RI::Paths.instance_variable_set :@gemdirs, %w[/nonexistent/gemdir]
9+
10+
@spec = Gem::Specification.new 'test', '1.0'
11+
@spec.loaded_from =
12+
File.expand_path '../specifications/test-1.0.gemspec', __FILE__
13+
14+
Gem::Specification.reset
15+
Gem::Specification.all = [@spec]
916
end
1017

1118
def teardown
1219
super
1320

1421
RDoc::RI::Paths.instance_variable_set :@gemdirs, nil
22+
Gem::Specification.reset
23+
end
24+
25+
def test_class_gem_dir
26+
dir = RDoc::RI::Paths.gem_dir 'test', '1.0'
27+
28+
expected = File.expand_path '../doc/test-1.0/ri', __FILE__
29+
30+
assert_equal expected, dir
31+
end
32+
33+
def test_class_home_dir
34+
dir = RDoc::RI::Paths.home_dir
35+
36+
assert_equal RDoc::RI::Paths::HOMEDIR, dir
1537
end
1638

1739
def test_class_path_nonexistent
1840
temp_dir do |dir|
1941
nonexistent = File.join dir, 'nonexistent'
20-
path = RDoc::RI::Paths.path true, true, true, true, nonexistent
42+
dir = RDoc::RI::Paths.path true, true, true, true, nonexistent
2143

22-
refute_includes path, nonexistent
44+
refute_includes dir, nonexistent
2345
end
2446
end
2547

2648
def test_class_raw_path
2749
path = RDoc::RI::Paths.raw_path true, true, true, true
2850

29-
assert_equal RDoc::RI::Paths::SYSDIR, path.shift
30-
assert_equal RDoc::RI::Paths::SITEDIR, path.shift
31-
assert_equal RDoc::RI::Paths::HOMEDIR, path.shift
32-
assert_equal '/nonexistent/gemdir', path.shift
51+
assert_equal RDoc::RI::Paths.system_dir, path.shift
52+
assert_equal RDoc::RI::Paths.site_dir, path.shift
53+
assert_equal RDoc::RI::Paths.home_dir, path.shift
54+
assert_equal '/nonexistent/gemdir', path.shift
3355
end
3456

3557
def test_class_raw_path_extra_dirs
3658
path = RDoc::RI::Paths.raw_path true, true, true, true, '/nonexistent'
3759

38-
assert_equal '/nonexistent', path.shift
39-
assert_equal RDoc::RI::Paths::SYSDIR, path.shift
40-
assert_equal RDoc::RI::Paths::SITEDIR, path.shift
41-
assert_equal RDoc::RI::Paths::HOMEDIR, path.shift
42-
assert_equal '/nonexistent/gemdir', path.shift
60+
assert_equal '/nonexistent', path.shift
61+
assert_equal RDoc::RI::Paths.system_dir, path.shift
62+
assert_equal RDoc::RI::Paths.site_dir, path.shift
63+
assert_equal RDoc::RI::Paths.home_dir, path.shift
64+
assert_equal '/nonexistent/gemdir', path.shift
65+
end
66+
67+
def test_class_site_dir
68+
dir = RDoc::RI::Paths.site_dir
69+
70+
assert_equal File.join(RDoc::RI::Paths::BASE, 'site'), dir
71+
end
72+
73+
def test_class_system_dir
74+
dir = RDoc::RI::Paths.system_dir
75+
76+
assert_equal File.join(RDoc::RI::Paths::BASE, 'system'), dir
4377
end
4478

4579
end

0 commit comments

Comments
 (0)