Skip to content

Commit f517b02

Browse files
st0012kou
andauthored
Make Options a required constructor argument of Store (#1309)
Since `Store` relies on accessing `Options` for various tasks, it should simply hold a reference to the `Options` object itself, instead of accessing it through an `RDoc` instance and forming an unnecessary coupling. This PR requires `Store` to be initialized with an `Options` instance. --------- Co-authored-by: Sutou Kouhei <[email protected]>
1 parent 6b541aa commit f517b02

17 files changed

+91
-114
lines changed

lib/rdoc/rdoc.rb

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ class RDoc::RDoc
6969
##
7070
# The current documentation store
7171

72-
attr_reader :store
72+
attr_accessor :store
7373

7474
##
7575
# Add +klass+ that can generate output after parsing
@@ -208,15 +208,6 @@ def setup_output_dir(dir, force)
208208
last
209209
end
210210

211-
##
212-
# Sets the current documentation tree to +store+ and sets the store's rdoc
213-
# driver to this instance.
214-
215-
def store= store
216-
@store = store
217-
@store.rdoc = self
218-
end
219-
220211
##
221212
# Update the flag file in an output directory.
222213

@@ -450,8 +441,6 @@ def remove_unparseable files
450441
# current directory, so make sure you're somewhere writable before invoking.
451442

452443
def document options
453-
self.store = RDoc::Store.new
454-
455444
if RDoc::Options === options then
456445
@options = options
457446
else
@@ -460,6 +449,8 @@ def document options
460449
end
461450
@options.finish
462451

452+
@store = RDoc::Store.new(@options)
453+
463454
if @options.pipe then
464455
handle_pipe
465456
exit
@@ -469,12 +460,6 @@ def document options
469460
@last_modified = setup_output_dir @options.op_dir, @options.force_update
470461
end
471462

472-
@store.encoding = @options.encoding
473-
@store.dry_run = @options.dry_run
474-
@store.main = @options.main_page
475-
@store.title = @options.title
476-
@store.path = @options.op_dir
477-
478463
@start_time = Time.now
479464

480465
@store.load_cache

lib/rdoc/ri/driver.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -420,7 +420,7 @@ def initialize initial_options = {}
420420
*options[:extra_doc_dirs]) do |path, type|
421421
@doc_dirs << path
422422

423-
store = RDoc::RI::Store.new path, type
423+
store = RDoc::RI::Store.new(RDoc::Options.new, path: path, type: type)
424424
store.load_cache
425425
@stores << store
426426
end

lib/rdoc/rubygems_hook.rb

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -188,13 +188,7 @@ def generate
188188
@rdoc = new_rdoc
189189
@rdoc.options = options
190190

191-
store = RDoc::Store.new
192-
store.encoding = options.encoding
193-
store.dry_run = options.dry_run
194-
store.main = options.main_page
195-
store.title = options.title
196-
197-
@rdoc.store = store
191+
@rdoc.store = RDoc::Store.new(options)
198192

199193
say "Parsing documentation for #{@spec.full_name}"
200194

lib/rdoc/servlet.rb

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ def do_GET req, res
133133
show_documentation req, res
134134
end
135135
rescue WEBrick::HTTPStatus::NotFound => e
136-
generator = generator_for RDoc::Store.new
136+
generator = generator_for RDoc::Store.new(@options)
137137

138138
not_found generator, req, res, e.message
139139
rescue WEBrick::HTTPStatus::Status
@@ -291,7 +291,7 @@ def if_modified_since req, res, path = nil
291291
def installed_docs
292292
extra_counter = 0
293293
ri_paths.map do |path, type|
294-
store = RDoc::Store.new path, type
294+
store = RDoc::Store.new(@options, path: path, type: type)
295295
exists = File.exist? store.cache_path
296296

297297
case type
@@ -420,15 +420,15 @@ def show_documentation req, res
420420
def store_for source_name
421421
case source_name
422422
when 'home' then
423-
RDoc::Store.new RDoc::RI::Paths.home_dir, :home
423+
RDoc::Store.new(@options, path: RDoc::RI::Paths.home_dir, type: :home)
424424
when 'ruby' then
425-
RDoc::Store.new RDoc::RI::Paths.system_dir, :system
425+
RDoc::Store.new(@options, path: RDoc::RI::Paths.system_dir, type: :system)
426426
when 'site' then
427-
RDoc::Store.new RDoc::RI::Paths.site_dir, :site
427+
RDoc::Store.new(@options, path: RDoc::RI::Paths.site_dir, type: :site)
428428
when /\Aextra-(\d+)\z/ then
429429
index = $1.to_i - 1
430430
ri_dir = installed_docs[index][4]
431-
RDoc::Store.new ri_dir, :extra
431+
RDoc::Store.new(@options, path: ri_dir, type: :extra)
432432
else
433433
ri_dir, type = ri_paths.find do |dir, dir_type|
434434
next unless dir_type == :gem
@@ -439,7 +439,7 @@ def store_for source_name
439439
raise WEBrick::HTTPStatus::NotFound,
440440
"Could not find gem \"#{ERB::Util.html_escape(source_name)}\". Are you sure you installed it?" unless ri_dir
441441

442-
store = RDoc::Store.new ri_dir, type
442+
store = RDoc::Store.new(@options, path: ri_dir, type: type)
443443

444444
return store if File.exist? store.cache_path
445445

lib/rdoc/store.rb

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ def message # :nodoc:
9494

9595
attr_accessor :path
9696

97-
attr_writer :rdoc
97+
attr_reader :options
9898

9999
##
100100
# Type of ri datastore this was loaded from. See RDoc::RI::Driver,
@@ -120,11 +120,11 @@ def message # :nodoc:
120120
##
121121
# Creates a new Store of +type+ that will load or save to +path+
122122

123-
def initialize path = nil, type = nil
124-
@dry_run = false
125-
@encoding = nil
126-
@path = path
127-
@rdoc = nil
123+
def initialize(options, path: nil, type: nil)
124+
@options = options
125+
@dry_run = options.dry_run
126+
@encoding = options.encoding
127+
@path = path || options.op_dir
128128
@type = type
129129

130130
@cache = {
@@ -135,10 +135,10 @@ def initialize path = nil, type = nil
135135
:c_singleton_class_variables => {},
136136
:encoding => @encoding,
137137
:instance_methods => {},
138-
:main => nil,
138+
:main => options.main_page,
139139
:modules => [],
140140
:pages => [],
141-
:title => nil,
141+
:title => options.title,
142142
}
143143

144144
@classes_hash = {}
@@ -977,10 +977,6 @@ def unique_modules
977977
@unique_modules
978978
end
979979

980-
def options
981-
@rdoc&.options
982-
end
983-
984980
private
985981
def marshal_load(file)
986982
File.open(file, 'rb') {|io| Marshal.load(io, MarshalFilter)}

test/rdoc/support/test_case.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,12 @@ def setup
4747

4848
@pwd = Dir.pwd
4949

50-
@store = RDoc::Store.new
50+
@options = RDoc::Options.new
51+
@store = RDoc::Store.new(@options)
5152

5253
@rdoc = RDoc::RDoc.new
5354
@rdoc.store = @store
54-
@rdoc.options = RDoc::Options.new
55+
@rdoc.options = @options
5556

5657
@rdoc.generator = Object.new
5758

test/rdoc/test_rdoc_any_method.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ def test_markup_code_with_line_numbers
180180
<span class="ruby-constant">B</span>
181181
EXPECTED
182182

183-
@options.line_numbers = true
183+
@c2_a.options.line_numbers = true
184184
assert_equal <<-EXPECTED.chomp, @c2_a.markup_code
185185
<span class="ruby-comment"># File xref_data.rb</span>
186186
<span class="line-num">1</span> <span class="ruby-constant">A</span>

test/rdoc/test_rdoc_class_module.rb

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -791,7 +791,7 @@ def test_merge_constants
791791
const = cm1.add_constant RDoc::Constant.new('C3', nil, 'one')
792792
const.record_location tl1
793793

794-
store = RDoc::Store.new
794+
store = RDoc::Store.new(RDoc::Options.new)
795795
tl = store.add_file 'one.rb'
796796
cm2 = tl.add_class RDoc::ClassModule, 'Klass'
797797
cm2.instance_variable_set :@comment, @RM::Document.new
@@ -826,7 +826,7 @@ def test_merge_constants_version_0
826826
const = cm1.add_constant RDoc::Constant.new('C3', nil, 'one')
827827
const.record_location tl1
828828

829-
store = RDoc::Store.new
829+
store = RDoc::Store.new(RDoc::Options.new)
830830
tl = store.add_file 'one.rb'
831831
cm2 = tl.add_class RDoc::ClassModule, 'Klass'
832832
cm2.instance_variable_set :@comment, @RM::Document.new
@@ -859,7 +859,7 @@ def test_merge_extends
859859
ext.record_location tl1
860860

861861
tl2 = @store.add_file 'two.rb'
862-
tl2.store = RDoc::Store.new
862+
tl2.store = RDoc::Store.new(RDoc::Options.new)
863863

864864
cm2 = tl2.add_class RDoc::ClassModule, 'Klass'
865865
cm2.instance_variable_set :@comment, @RM::Document.new
@@ -895,7 +895,7 @@ def test_merge_includes
895895
incl.record_location tl1
896896

897897
tl2 = @store.add_file 'two.rb'
898-
tl2.store = RDoc::Store.new
898+
tl2.store = RDoc::Store.new(RDoc::Options.new)
899899

900900
cm2 = tl2.add_class RDoc::ClassModule, 'Klass'
901901
cm2.instance_variable_set :@comment, @RM::Document.new
@@ -931,7 +931,7 @@ def test_merge_includes_version_0
931931
incl.record_location tl1
932932

933933
tl2 = @store.add_file 'one.rb'
934-
tl2.store = RDoc::Store.new
934+
tl2.store = RDoc::Store.new(RDoc::Options.new)
935935

936936
cm2 = tl2.add_class RDoc::ClassModule, 'Klass'
937937
cm2.instance_variable_set :@comment, @RM::Document.new
@@ -1034,7 +1034,7 @@ def test_merge_sections
10341034
cm1.add_section 'section 2', comment('comment 2 a', tl1_1)
10351035
cm1.add_section 'section 4', comment('comment 4 a', tl1_1)
10361036

1037-
store2 = RDoc::Store.new
1037+
store2 = RDoc::Store.new(RDoc::Options.new)
10381038
tl2_1 = store2.add_file 'one.rb'
10391039
tl2_2 = store2.add_file 'two.rb'
10401040

@@ -1086,7 +1086,7 @@ def test_merge_sections_overlap
10861086
cm1.add_section 'section', comment('comment 1 a', tl1_1)
10871087
cm1.add_section 'section', comment('comment 3', tl1_3)
10881088

1089-
store2 = RDoc::Store.new
1089+
store2 = RDoc::Store.new(RDoc::Options.new)
10901090
tl2_1 = store2.add_file 'one.rb'
10911091
tl2_2 = store2.add_file 'two.rb'
10921092
tl2_3 = store2.add_file 'three.rb'
@@ -1382,7 +1382,7 @@ def test_update_aliases_reparent
13821382
end
13831383

13841384
def test_update_aliases_reparent_root
1385-
store = RDoc::Store.new
1385+
store = RDoc::Store.new(RDoc::Options.new)
13861386

13871387
top_level = store.add_file 'file.rb'
13881388

test/rdoc/test_rdoc_code_object.rb

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -275,14 +275,6 @@ def test_metadata
275275
assert_equal 'not_rdoc', @co.metadata['markup']
276276
end
277277

278-
def test_options
279-
assert_kind_of RDoc::Options, @co.options
280-
281-
@co.store = @store
282-
283-
assert_same @options, @co.options
284-
end
285-
286278
def test_parent_file_name
287279
assert_equal '(unknown)', @co.parent_file_name
288280
assert_equal 'xref_data.rb', @c1.parent_file_name

test/rdoc/test_rdoc_context.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,7 @@ def test_add_module_alias
290290
end
291291

292292
def test_add_module_alias_top_level
293-
store = RDoc::Store.new
293+
store = RDoc::Store.new(RDoc::Options.new)
294294

295295
top_level = store.add_file 'file.rb'
296296

test/rdoc/test_rdoc_generator_ri.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ def test_generate
5252
assert_file File.join(@tmpdir, 'Object', 'method-i.ri')
5353
assert_file File.join(@tmpdir, 'Object', 'method%21-i.ri')
5454

55-
store = RDoc::RI::Store.new @tmpdir
55+
store = RDoc::RI::Store.new(@options, path: @tmpdir)
5656
store.load_cache
5757

5858
encoding = Encoding::UTF_8

test/rdoc/test_rdoc_method_attr.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -185,8 +185,7 @@ def test_equals2
185185

186186
def test_pretty_print
187187
temp_dir do |tmpdir|
188-
s = RDoc::RI::Store.new tmpdir
189-
s.rdoc = @rdoc
188+
s = RDoc::RI::Store.new(RDoc::Options.new, path: tmpdir)
190189

191190
top_level = s.add_file 'file.rb'
192191
meth_bang = RDoc::AnyMethod.new nil, 'method!'

test/rdoc/test_rdoc_rdoc.rb

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ def setup
77
super
88

99
@rdoc = RDoc::RDoc.new
10-
@rdoc.options = RDoc::Options.new
10+
@options = RDoc::Options.new
11+
@rdoc.options = @options
1112

1213
@stats = RDoc::Stats.new @store, 0, 0
1314
@rdoc.instance_variable_set :@stats, @stats
@@ -244,7 +245,7 @@ def test_normalized_file_list_with_skipping_tests_disabled
244245
end
245246

246247
def test_parse_file
247-
@rdoc.store = RDoc::Store.new
248+
@rdoc.store = RDoc::Store.new(@options)
248249

249250
temp_dir do |dir|
250251
@rdoc.options.root = Pathname(Dir.pwd)
@@ -261,7 +262,7 @@ def test_parse_file
261262
end
262263

263264
def test_parse_file_binary
264-
@rdoc.store = RDoc::Store.new
265+
@rdoc.store = RDoc::Store.new(@options)
265266

266267
root = File.dirname __FILE__
267268

@@ -278,7 +279,7 @@ def test_parse_file_binary
278279
end
279280

280281
def test_parse_file_include_root
281-
@rdoc.store = RDoc::Store.new
282+
@rdoc.store = RDoc::Store.new(@options)
282283

283284
test_path = File.expand_path('..', __FILE__)
284285
top_level = nil
@@ -300,7 +301,7 @@ def test_parse_file_include_root
300301
end
301302

302303
def test_parse_file_page_dir
303-
@rdoc.store = RDoc::Store.new
304+
@rdoc.store = RDoc::Store.new(@options)
304305

305306
temp_dir do |dir|
306307
FileUtils.mkdir 'pages'
@@ -321,7 +322,7 @@ def test_parse_file_page_dir
321322
def test_parse_file_relative
322323
pwd = Dir.pwd
323324

324-
@rdoc.store = RDoc::Store.new
325+
@rdoc.store = RDoc::Store.new(@options)
325326

326327
temp_dir do |dir|
327328
@rdoc.options.root = Pathname(dir)
@@ -343,7 +344,7 @@ def test_parse_file_relative
343344

344345
def test_parse_file_encoding
345346
@rdoc.options.encoding = Encoding::ISO_8859_1
346-
@rdoc.store = RDoc::Store.new
347+
@rdoc.store = RDoc::Store.new(@options)
347348

348349
tf = Tempfile.open 'test.txt' do |io|
349350
io.write 'hi'
@@ -361,7 +362,7 @@ def test_parse_file_forbidden
361362
omit 'chmod not supported' if Gem.win_platform?
362363
omit "assumes that euid is not root" if Process.euid == 0
363364

364-
@rdoc.store = RDoc::Store.new
365+
@rdoc.store = RDoc::Store.new(@options)
365366

366367
tf = Tempfile.open 'test.txt' do |io|
367368
io.write 'hi'

0 commit comments

Comments
 (0)