Skip to content

Commit

Permalink
Use caller_locations again to find the right directory
Browse files Browse the repository at this point in the history
Our switch to `const_source_location` seems to be unreliable with
Zeitwerk 2.6.0, and our `caller_locations` approach works — we just
have to inject the right frame when `inherited` is called.

This is the fix discussed in 1f6fe9c

Fixes #4
  • Loading branch information
kaspth committed Sep 8, 2022
1 parent d879e93 commit 7fc25f1
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
12 changes: 9 additions & 3 deletions lib/conventional_extensions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
module ConventionalExtensions
Object.extend self # We're enriching object itself, so any object can call `load_extensions`.

def load_extensions(*extensions)
@loader = Loader.new(self, Object.const_source_location(name).first) unless loader_defined_before_entrance = defined?(@loader)
def load_extensions(*extensions, from: Frame.previous.path)
@loader = Loader.new(self, from) unless loader_defined_before_entrance = defined?(@loader)
@loader.load(*extensions)
ensure
@loader = nil unless loader_defined_before_entrance
Expand All @@ -19,7 +19,13 @@ def self.load_on_inherited() = LoadOnInherited
module LoadOnInherited
def inherited(klass)
super
klass.load_extensions
klass.load_extensions from: Frame.previous.path
end
end

module Frame
def self.previous
caller_locations(2, 1).first # Use 2 instead of 1 so we get the frame of who called us.
end
end

Expand Down
2 changes: 1 addition & 1 deletion test/test_conventional_extensions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def test_load_on_inherited_calls_load_extensions_when_inherited
klass = Class.new do
extend ConventionalExtensions.load_on_inherited

def self.load_extensions
def self.load_extensions(*)
raise NotImplementedError
end
end
Expand Down

0 comments on commit 7fc25f1

Please sign in to comment.