Skip to content

Commit 23f025e

Browse files
authored
Merge pull request #8 from splitwise/ar/nested_modules_1
Fix incorrect constant name for nested modules/classes
2 parents e5e3fed + 49f7356 commit 23f025e

2 files changed

Lines changed: 29 additions & 1 deletion

File tree

lib/cacheable/method_generator.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ def cacheable(*original_method_names, **opts)
1313
private
1414

1515
def method_interceptor_module_name
16-
class_name = name || to_s.gsub(/[^a-zA-Z_0-9]/, '')
16+
class_name = name&.gsub(/:/, '') || to_s.gsub(/[^a-zA-Z_0-9]/, '')
1717
"#{class_name}Cacher"
1818
end
1919

spec/cacheable/cacheable_spec.rb

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,34 @@ class RealClassName
117117
end
118118
end
119119
end
120+
121+
context 'when classes are nested' do
122+
it 'correctly names interceptor modules' do
123+
expect do
124+
class Outer
125+
class Inner
126+
include Cacheable
127+
128+
cacheable :outer_method
129+
130+
def outer_method
131+
inner_method
132+
'outer_method'
133+
end
134+
135+
def inner_method
136+
puts 'inner_method'
137+
end
138+
end
139+
end
140+
end.not_to raise_error
141+
142+
cacheable_instance = Outer::Inner.new
143+
expect(cacheable_instance).to receive(:inner_method).once.and_call_original
144+
145+
2.times { cacheable_instance.outer_method }
146+
end
147+
end
120148
end
121149

122150
describe 'interceptor module' do

0 commit comments

Comments
 (0)