Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Proof of concept] Component isolation #231

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions lib/dry/system/loader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,16 +41,25 @@ def require!(component)
# @return [Object]
#
# @api public
def call(component, *args)
def call(component, *args, isolate: false)
require!(component)

constant = self.constant(component)

if singleton?(constant)
instance = if singleton?(constant)
constant.instance(*args)
else
constant.new(*args)
end

if isolate
constant_name_parts = constant.to_s.split("::")
namespace = constant_name_parts.slice(0..-2).join("::")
namespace_const = component.inflector.constantize(namespace)
namespace_const.send(:remove_const, constant_name_parts.last)
end

instance
end
ruby2_keywords(:call) if respond_to?(:ruby2_keywords, true)

Expand Down
7 changes: 7 additions & 0 deletions spec/unit/loader_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,13 @@ module Test
end

it_behaves_like "object loader"

it "isolates component by removing its constant" do
constant
instance = loader.call(component, isolate: true)
expect(Test.const_defined?("Bar")).to eq(false)
expect(instance).to be_a(constant)
end
end

context "with a constructor accepting args" do
Expand Down