-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6f44fec
commit 5ad08c6
Showing
5 changed files
with
64 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
# frozen_string_literal: true | ||
|
||
module LazyRecord | ||
# Apply the same namespace nesting as self to another object. | ||
module Nesting | ||
def apply_nesting(class_name) | ||
"#{to_s.split('::')[0..-2].join('::')}::#{class_name}" | ||
end | ||
|
||
def lazily_get_class(class_name) | ||
lambda do | ||
begin | ||
const_get(class_name) | ||
rescue NameError | ||
const_get apply_nesting(class_name) | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
# frozen_string_literal: true | ||
|
||
describe LazyRecord::Nesting do | ||
module One | ||
class Two | ||
module Three | ||
extend LazyRecord::Nesting | ||
end | ||
|
||
module Four; end | ||
end | ||
end | ||
|
||
module Five; end | ||
|
||
class Top; end | ||
|
||
describe 'Three' do | ||
it 'can find One' do | ||
expect(One::Two::Three.lazily_get_class('One').call). to be One | ||
end | ||
|
||
it 'can find Four' do | ||
expect(One::Two::Three.lazily_get_class('Four').call). to be One::Two::Four | ||
end | ||
|
||
it 'can find Five' do | ||
expect(One::Two::Three.lazily_get_class('Five').call). to be One::Two::Five | ||
end | ||
end | ||
end |