-
-
Notifications
You must be signed in to change notification settings - Fork 69
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
Add :injector_mixin
plugin
#221
Open
timriley
wants to merge
2
commits into
main
Choose a base branch
from
injector-mixin-plugin
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
# frozen_string_literal: true | ||
|
||
module Dry | ||
module System | ||
module Plugins | ||
# @api private | ||
class InjectorMixin < Module | ||
MODULE_SEPARATOR = "::" | ||
|
||
attr_reader :name | ||
|
||
def initialize(name: "Deps") | ||
@name = name | ||
end | ||
|
||
def extended(container) | ||
container.after(:configure, &method(:define_mixin)) | ||
end | ||
|
||
private | ||
|
||
def define_mixin(container) | ||
inflector = container.config.inflector | ||
|
||
name_parts = name.split(MODULE_SEPARATOR) | ||
|
||
if name_parts[0] == "" | ||
name_parts.delete_at(0) | ||
root_module = Object | ||
else | ||
root_module = container_parent_module(container) | ||
end | ||
|
||
mixin_parent_mod = define_parent_modules( | ||
root_module, | ||
name_parts, | ||
inflector | ||
) | ||
|
||
mixin_parent_mod.const_set( | ||
inflector.camelize(name_parts.last), | ||
container.injector | ||
) | ||
end | ||
|
||
def container_parent_module(container) | ||
if container.name | ||
parent_name = container.name.split(MODULE_SEPARATOR)[0..-2].join(MODULE_SEPARATOR) | ||
container.config.inflector.constantize(parent_name) | ||
else | ||
Object | ||
end | ||
end | ||
|
||
def define_parent_modules(root_mod, name_parts, inflector) | ||
return root_mod if name_parts.length == 1 | ||
|
||
name_parts[0..-2].reduce(root_mod) { |parent_mod, mod_name| | ||
parent_mod.const_set(inflector.camelize(mod_name), Module.new) | ||
} | ||
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
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,83 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe "Plugins / Injector mixin" do | ||
describe "default options" do | ||
it "creates a 'Deps' mixin in the container's parent module" do | ||
module Test | ||
class Container < Dry::System::Container | ||
use :injector_mixin | ||
configured! | ||
end | ||
end | ||
|
||
component = Object.new | ||
Test::Container.register "component", component | ||
|
||
depending_obj = Class.new do | ||
include Test::Deps["component"] | ||
end.new | ||
|
||
expect(depending_obj.component).to be component | ||
end | ||
end | ||
|
||
describe "name given" do | ||
it "creates a mixin with the given name in the container's parent module" do | ||
module Test | ||
class Container < Dry::System::Container | ||
use :injector_mixin, name: "Inject" | ||
configured! | ||
end | ||
end | ||
|
||
component = Object.new | ||
Test::Container.register "component", component | ||
|
||
depending_obj = Class.new do | ||
include Test::Inject["component"] | ||
end.new | ||
|
||
expect(depending_obj.component).to be component | ||
end | ||
end | ||
|
||
describe "nested name given" do | ||
it "creates a mixin with the given name in the container's parent module" do | ||
module Test | ||
class Container < Dry::System::Container | ||
use :injector_mixin, name: "Inject::These::Pls" | ||
configured! | ||
end | ||
end | ||
|
||
component = Object.new | ||
Test::Container.register "component", component | ||
|
||
depending_obj = Class.new do | ||
include Test::Inject::These::Pls["component"] | ||
end.new | ||
|
||
expect(depending_obj.component).to be component | ||
end | ||
end | ||
|
||
describe "top-level name given" do | ||
it "creates a mixin with the given name in the top-level module" do | ||
module Test | ||
class Container < Dry::System::Container | ||
use :injector_mixin, name: "::Deps" | ||
configured! | ||
end | ||
end | ||
|
||
component = Object.new | ||
Test::Container.register "component", component | ||
|
||
depending_obj = Class.new do | ||
include ::Deps["component"] | ||
end.new | ||
|
||
expect(depending_obj.component).to be component | ||
end | ||
end | ||
end |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Rubocop is saying:
Do we really think we need to do this here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have a feeling we should always delegate to
super
. Who knows where this class can end up being used later? If it doesn't properly delegate, this would break expectations.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My question is more about whether it's really necessary when inheriting from
Module
. We already don't do it across the other instances ofModule
inheritance in this codebase, and it's not something that's appeared necessary to me when using this technique elsewhere.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@timriley I doubt it plays a role in this particular case. It's not a problem to have
super
calls to quell rubocop, thoughThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it's beneficial to have a habit of calling super, even if it's a noop. It may happen that you really need to call super, but you don't have this habit and you end up debugging why things don't work just realize you forgot to call super. Been there done that 🙂