Skip to content

Commit 080bfe4

Browse files
committed
Removed method_missing on Hash
1 parent 9a77350 commit 080bfe4

File tree

2 files changed

+75
-49
lines changed

2 files changed

+75
-49
lines changed

lib/rdoc/ri/driver.rb

Lines changed: 58 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,54 @@
1111

1212
class RDoc::RI::Driver
1313

14+
class Hash < ::Hash
15+
def self.convert(hash)
16+
hash = new.update hash
17+
18+
hash.each do |key, value|
19+
hash[key] = case value
20+
when ::Hash then
21+
convert value
22+
when Array then
23+
value = value.map do |v|
24+
::Hash === v ? convert(v) : v
25+
end
26+
value
27+
else
28+
value
29+
end
30+
end
31+
32+
hash
33+
end
34+
35+
def method_missing method, *args
36+
self[method.to_s]
37+
end
38+
39+
def merge_enums(other)
40+
other.each do |k, v|
41+
if self[k] then
42+
case v
43+
when Array then
44+
# HACK dunno
45+
if String === self[k] and self[k].empty? then
46+
self[k] = v
47+
else
48+
self[k] += v
49+
end
50+
when Hash then
51+
self[k].update v
52+
else
53+
# do nothing
54+
end
55+
else
56+
self[k] = v
57+
end
58+
end
59+
end
60+
end
61+
1462
class Error < RDoc::RI::Error; end
1563

1664
class NotFoundError < Error
@@ -244,7 +292,7 @@ def class_cache
244292
@class_cache = if up_to_date then
245293
load_cache_for @class_cache_name
246294
else
247-
class_cache = {}
295+
class_cache = RDoc::RI::Driver::Hash.new
248296

249297
classes = map_dirs('**/cdesc*.yaml', :sys) { |f| Dir[f] }
250298
populate_class_cache class_cache, classes
@@ -271,6 +319,7 @@ def cache_file_path
271319

272320
def display_class(name)
273321
klass = class_cache[name]
322+
klass = RDoc::RI::Driver::Hash.convert klass
274323
@display.display_class_info klass, class_cache
275324
end
276325

@@ -282,10 +331,12 @@ def get_info_for(arg)
282331
def load_cache_for(klassname)
283332
path = cache_file_for klassname
284333

334+
cache = nil
335+
285336
if File.exist? path and
286337
File.mtime(path) >= File.mtime(class_cache_file_path) then
287338
File.open path, 'rb' do |fp|
288-
Marshal.load fp.read
339+
cache = Marshal.load fp.read
289340
end
290341
else
291342
class_cache = nil
@@ -298,7 +349,7 @@ def load_cache_for(klassname)
298349
return nil unless klass
299350

300351
method_files = klass["sources"]
301-
cache = {}
352+
cache = RDoc::RI::Driver::Hash.new
302353

303354
sys_dir = @sys_dirs.first
304355
method_files.each do |f|
@@ -311,16 +362,18 @@ def load_cache_for(klassname)
311362
ext_path = f
312363
ext_path = "gem #{$1}" if f =~ %r%gems/[\d.]+/doc/([^/]+)%
313364
method["source_path"] = ext_path unless system_file
314-
cache[name] = method
365+
cache[name] = RDoc::RI::Driver::Hash.convert method
315366
end
316367
end
317368

318369
write_cache cache, path
319370
end
371+
372+
RDoc::RI::Driver::Hash.convert cache
320373
end
321374

322375
##
323-
# Finds the method
376+
# Finds the method
324377

325378
def lookup_method(name, klass)
326379
cache = load_cache_for klass
@@ -459,31 +512,3 @@ def write_cache(cache, path)
459512

460513
end
461514

462-
class Hash # HACK don't add stuff to Hash.
463-
def method_missing method, *args
464-
self[method.to_s]
465-
end
466-
467-
def merge_enums(other)
468-
other.each do |k,v|
469-
if self[k] then
470-
case v
471-
when Array then
472-
# HACK dunno
473-
if String === self[k] and self[k].empty? then
474-
self[k] = v
475-
else
476-
self[k] += v
477-
end
478-
when Hash then
479-
self[k].merge! v
480-
else
481-
# do nothing
482-
end
483-
else
484-
self[k] = v
485-
end
486-
end
487-
end
488-
end
489-

test/test_rdoc_ri_default_display.rb

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
require 'rdoc/ri/display'
55
require 'rdoc/ri/driver'
66

7-
class TestRDocRIDefaultDisplay < Test::Unit::TestCase
7+
class TestRdocRiDefaultDisplay < Test::Unit::TestCase
88

99
def setup
1010
@output = StringIO.new
@@ -14,7 +14,7 @@ def setup
1414
@dd = RDoc::RI::DefaultDisplay.new RDoc::RI::Formatter, @width, true,
1515
@output
1616

17-
@some_method = {
17+
@some_method = h \
1818
'aliases' => [{'name' => 'some_method_alias'}],
1919
'block_params' => 'block_param',
2020
'comment' => [RDoc::Markup::Flow::P.new('some comment')],
@@ -23,13 +23,12 @@ def setup
2323
'name' => 'some_method',
2424
'params' => '(arg1, arg2) {|block_param| ...}',
2525
'source_path' => '/nonexistent',
26-
'visibility' => 'public',
27-
}
26+
'visibility' => 'public'
2827
end
2928

3029
def test_display_class_info
3130
ri_reader = nil
32-
klass = {
31+
klass = h \
3332
'attributes' => [
3433
{ 'name' => 'attribute', 'rw' => 'RW',
3534
'comment' => [RDoc::Markup::Flow::P.new('attribute comment')] },
@@ -58,8 +57,7 @@ def test_display_class_info
5857
'instance_method_extensions' => [
5958
{ 'name' => 'instance_method_extension' },
6059
],
61-
'superclass_string' => 'Object',
62-
}
60+
'superclass_string' => 'Object'
6361

6462
@dd.display_class_info klass, ri_reader
6563

@@ -154,16 +152,15 @@ def test_display_method_info
154152
end
155153

156154
def test_display_method_info_singleton
157-
method = {
155+
method = RDoc::RI::Driver::Hash.new.update \
158156
'aliases' => [],
159157
'block_params' => nil,
160158
'comment' => nil,
161159
'full_name' => 'SomeClass::some_method',
162160
'is_singleton' => true,
163161
'name' => 'some_method',
164162
'params' => '(arg1, arg2)',
165-
'visibility' => 'public',
166-
}
163+
'visibility' => 'public'
167164

168165
@dd.display_method_info method
169166

@@ -179,26 +176,26 @@ def test_display_method_info_singleton
179176

180177
def test_display_method_list
181178
methods = [
182-
{
179+
RDoc::RI::Driver::Hash.new.update(
183180
"aliases" => [],
184181
"block_params" => nil,
185182
"comment" => nil,
186183
"full_name" => "SomeClass#some_method",
187184
"is_singleton" => false,
188185
"name" => "some_method",
189186
"params" => "()",
190-
"visibility" => "public",
191-
},
192-
{
187+
"visibility" => "public"
188+
),
189+
RDoc::RI::Driver::Hash.new.update(
193190
"aliases" => [],
194191
"block_params" => nil,
195192
"comment" => nil,
196193
"full_name" => "SomeClass#some_other_method",
197194
"is_singleton" => false,
198195
"name" => "some_other_method",
199196
"params" => "()",
200-
"visibility" => "public",
201-
},
197+
"visibility" => "public"
198+
),
202199
]
203200

204201
@dd.display_method_list methods
@@ -291,5 +288,9 @@ def test_list_known_classes_empty
291288
assert_equal expected, @output.string
292289
end
293290

291+
def h(hash)
292+
RDoc::RI::Driver::Hash.convert hash
293+
end
294+
294295
end
295296

0 commit comments

Comments
 (0)