-
-
Notifications
You must be signed in to change notification settings - Fork 29
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
53b150d
commit 29a9d10
Showing
5 changed files
with
121 additions
and
19 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
# frozen_string_literal: true | ||
|
||
module Dry | ||
module AutoInject | ||
class Strategies | ||
# @api private | ||
class Base < Module | ||
ClassMethods = Class.new(Module) | ||
InstanceMethods = Class.new(Module) | ||
|
||
attr_reader :container | ||
attr_reader :dependency_map | ||
attr_reader :instance_mod | ||
attr_reader :class_mod | ||
|
||
def initialize(container, *dependency_names) | ||
@container = container | ||
@dependency_map = DependencyMap.new(*dependency_names) | ||
@instance_mod = InstanceMethods.new | ||
@class_mod = ClassMethods.new | ||
end | ||
|
||
# @api private | ||
def included(klass) | ||
klass.send(:include, instance_mod) | ||
klass.extend(class_mod) | ||
|
||
super | ||
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,59 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'dry/auto_inject/strategies/base' | ||
require 'dry/auto_inject/method_parameters' | ||
|
||
module Dry | ||
module AutoInject | ||
class Strategies | ||
# @api private | ||
class Methods < Base | ||
# @api private | ||
def included(klass) | ||
define_class_methods | ||
define_instance_methods | ||
|
||
super | ||
end | ||
|
||
private | ||
|
||
def define_class_methods | ||
class_mod.class_exec(container, dependency_map) do |container, dependency_map| | ||
dependency_map.to_h.each do |name, identifier| | ||
define_method name do | ||
container[identifier] | ||
end | ||
end | ||
|
||
def with_deps(**deps) | ||
Class.new(self) do |klass| | ||
deps.each do |name, value| | ||
singleton_class.define_method name do | ||
value | ||
end | ||
end | ||
end | ||
end | ||
end | ||
|
||
self | ||
end | ||
|
||
def define_instance_methods | ||
instance_mod.class_exec(container, dependency_map) do |container, dependency_map| | ||
dependency_map.to_h.each do |name, identifier| | ||
define_method name do | ||
self.class.send(name) | ||
end | ||
end | ||
end | ||
|
||
self | ||
end | ||
end | ||
|
||
register :methods, Methods | ||
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,26 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'pry' | ||
|
||
RSpec.describe 'methods' do | ||
it 'defines methods returning dependencies' do | ||
module Test | ||
AutoInject = Dry::AutoInject(one: 'dep 1', two: 'dep 2').methods | ||
end | ||
|
||
obj = Class.new do | ||
include Test::AutoInject[:one, :two] | ||
end | ||
|
||
expect(obj.one).to eq 'dep 1' | ||
expect(obj.new.one).to eq 'dep 1' | ||
expect(obj.two).to eq 'dep 2' | ||
expect(obj.new.two).to eq 'dep 2' | ||
|
||
other_obj = obj.with_deps(one: 'other dep 1') | ||
expect(other_obj.one).to eq 'other dep 1' | ||
expect(other_obj.new.one).to eq 'other dep 1' | ||
expect(other_obj.two).to eq 'dep 2' | ||
expect(other_obj.new.two).to eq 'dep 2' | ||
end | ||
end |