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

Raise exception when injecting into modules #62

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
5 changes: 2 additions & 3 deletions lib/dry/auto_inject/dependency_map.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
# frozen_string_literal: true

require 'dry/auto_inject/errors'

module Dry
module AutoInject
DuplicateDependencyError = Class.new(StandardError)
DependencyNameInvalid = Class.new(StandardError)

VALID_NAME = /([a-z_][a-zA-Z_0-9]*)$/

class DependencyMap
Expand Down
9 changes: 9 additions & 0 deletions lib/dry/auto_inject/errors.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# frozen_string_literal: true

module Dry
module AutoInject
Error = Class.new(StandardError)
DuplicateDependencyError = Class.new(Error)
DependencyNameInvalid = Class.new(Error)
end
end
3 changes: 3 additions & 0 deletions lib/dry/auto_inject/strategies/constructor.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# frozen_string_literal: true

require 'dry/auto_inject/dependency_map'
require 'dry/auto_inject/errors'

module Dry
module AutoInject
Expand All @@ -23,6 +24,8 @@ def initialize(container, *dependency_names)

# @api private
def included(klass)
fail Error, "cannot inject dependencies into a module" unless klass.respond_to?(:new)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


define_readers

define_new
Expand Down
28 changes: 28 additions & 0 deletions spec/dry/auto_inject_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ def initialize(*args)
Class.new(child_class)
end

let(:mod) do
Module.new
end

context 'with positioned args' do
let(:parent_class) do
Class.new do
Expand Down Expand Up @@ -167,6 +171,14 @@ def initialize(first, *middle, last)
end
end
end

context 'autoinject in a module' do
it 'raises exception' do
expect {
mod.include Test::AutoInject.args[:one, :two]
}.to raise_error(Dry::AutoInject::Error, /cannot inject/)
end
end
end

context 'with hash arg' do
Expand Down Expand Up @@ -312,6 +324,14 @@ def self.included(klass)
end
end
end

context 'autoinject in a module' do
it 'raises exception' do
expect {
mod.include Test::AutoInject.hash[:one, :two]
}.to raise_error(Dry::AutoInject::Error, /cannot inject/)
end
end
end

context 'with keyword args' do
Expand Down Expand Up @@ -558,5 +578,13 @@ def initialize(other)
end
end
end

context 'autoinject in a module' do
it 'raises exception' do
expect {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mod.include Test::AutoInject.kwargs[:one, :two]
}.to raise_error(Dry::AutoInject::Error, /cannot inject/)
end
end
end
end