-
Notifications
You must be signed in to change notification settings - Fork 1
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
29dbccd
commit d5f99e7
Showing
14 changed files
with
208 additions
and
131 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,9 +9,11 @@ Gem::Specification.new do |spec| | |
spec.email = ['[email protected]'] | ||
|
||
spec.summary = 'Dependency injection and resolution support for classes and modules.' | ||
spec.description = 'ActiveDryDeps not modify constructor and support Dependency Injection for modules. | ||
Also you can import method from any object in your container. | ||
Adding extra dependencies is easy and improve readability your code.' | ||
spec.description = <<~DESCRIPTION | ||
ActiveDryDeps does not modify constructor and supports Dependency Injection for modules. | ||
Also you can import method from any object in your container. | ||
Adding extra dependencies is easy and improve readability your code. | ||
DESCRIPTION | ||
spec.homepage = 'https://github.com/corp-gp/active_dry_deps' | ||
spec.license = 'MIT' | ||
spec.required_ruby_version = '>= 3.0.0' | ||
|
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,15 @@ | ||
# frozen_string_literal: true | ||
|
||
module ActiveDryDeps | ||
class Container < Hash | ||
|
||
def resolve(const_name) | ||
unless key?(const_name) | ||
self[const_name] = Object.const_get(const_name) | ||
end | ||
|
||
self[const_name] | ||
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,90 @@ | ||
# frozen_string_literal: true | ||
|
||
module ActiveDryDeps | ||
class Dependency | ||
|
||
VALID_METHOD_NAME = /^[a-zA-Z_0-9]+$/ | ||
VALID_CONST_NAME = /^[[:upper:]][a-zA-Z_0-9\:]*$/ | ||
METHODS_AS_KLASS = %w[perform_later call].freeze | ||
|
||
attr_reader :receiver_method_name, :receiver_method, :const_name, :method_name | ||
|
||
def initialize(resolver, receiver_method_alias: nil) | ||
if resolver.respond_to?(:call) | ||
receiver_method_by_proc(resolver, receiver_method_alias: receiver_method_alias) | ||
else | ||
receiver_method_by_const_name(resolver, receiver_method_alias: receiver_method_alias) | ||
end | ||
end | ||
|
||
def callable? | ||
!@receiver_method.nil? | ||
end | ||
|
||
def receiver_method_string | ||
if @method_name | ||
<<~RUBY | ||
# def create_order(*args, **options, &block) | ||
# ::ActiveDryDeps::Deps::CONTAINER.resolve("OrderService::Create").call(*args, **options, &block) | ||
# end | ||
def #{@receiver_method_name}(*args, **options, &block) | ||
::ActiveDryDeps::Deps::CONTAINER.resolve("#{@const_name}").#{@method_name}(*args, **options, &block) | ||
end | ||
RUBY | ||
else | ||
<<~RUBY | ||
# def create_order_service | ||
# ::ActiveDryDeps::Deps::CONTAINER.resolve("OrderService::Create") | ||
# end | ||
def #{@receiver_method_name} | ||
::ActiveDryDeps::Deps::CONTAINER.resolve("#{@const_name}") | ||
end | ||
RUBY | ||
end | ||
end | ||
|
||
private def receiver_method_by_proc(resolver, receiver_method_alias: nil) | ||
unless receiver_method_alias | ||
raise MissingAlias, "Alias is required while injecting with Proc" | ||
end | ||
|
||
@receiver_method_name = receiver_method_alias | ||
|
||
unless VALID_METHOD_NAME.match?(@receiver_method_name.to_s) | ||
raise DependencyNameInvalid, "name +#{@receiver_method_name}+ is not a valid Ruby identifier" | ||
end | ||
|
||
@receiver_method = resolver | ||
end | ||
|
||
private def receiver_method_by_const_name(resolver, receiver_method_alias: nil) | ||
@const_name, @method_name = resolver.to_s.split('.', 2) | ||
|
||
unless VALID_CONST_NAME.match?(@const_name) | ||
raise DependencyNameInvalid, "+#{resolver}+ must contains valid constant name" | ||
end | ||
|
||
if @method_name && !VALID_METHOD_NAME.match?(@method_name) | ||
raise DependencyNameInvalid, "name +#{@method_name}+ is not a valid Ruby identifier" | ||
end | ||
|
||
@receiver_method_name = | ||
if receiver_method_alias | ||
receiver_method_alias | ||
elsif @method_name && METHODS_AS_KLASS.exclude?(@method_name) | ||
@method_name | ||
elsif @const_name | ||
@const_name.split('::').last | ||
else | ||
resolver | ||
end | ||
|
||
unless VALID_METHOD_NAME.match?(@receiver_method_name.to_s) | ||
raise DependencyNameInvalid, "name +#{@receiver_method_name}+ is not a valid Ruby identifier" | ||
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
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
Oops, something went wrong.