-
-
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
Implement "allow" feature for injectable object #122
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'dry-auto_inject' | ||
|
||
module Dry | ||
module System | ||
# @api private | ||
module AutoInject | ||
# @api private | ||
ForbiddenInjectionError = Class.new(StandardError) | ||
|
||
# Dry system specific injector builder class | ||
# | ||
# @api private | ||
class Builder < Dry::AutoInject::Builder | ||
def initialize(container, options = {}) | ||
super | ||
@pattern = options.fetch(:pattern) { nil } | ||
end | ||
|
||
# @api private | ||
def allow(pattern) | ||
::Dry::System::AutoInject::Builder.new(@container, pattern: pattern, strategies: @strategies) | ||
end | ||
|
||
# @api private | ||
def [](*dependency_names) | ||
unless match_by_pattern?(dependency_names) | ||
::Kernel.raise AutoInject::ForbiddenInjectionError, | ||
"injecting #{dependency_names} dependencies forbidden for injector pattern #{@pattern}" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Layout/AlignParameters: Align the parameters of a method call if they span more than one line. |
||
end | ||
|
||
super | ||
end | ||
|
||
private | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Layout/AccessModifierIndentation: Indent access modifiers like private. |
||
|
||
def match_by_pattern?(dependency_names) | ||
@pattern.nil? || dependency_names.all? { |dependency| dependency.match(@pattern) } | ||
end | ||
end | ||
end | ||
end | ||
end |
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.
Metrics/LineLength: Line is too long. [103/100]