dry-auto_inject 0.0.1 → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +6 -1
- data/README.md +3 -4
- data/lib/dry/auto_inject.rb +101 -49
- data/lib/dry/auto_inject/version.rb +2 -2
- metadata +3 -3
- data/lib/dry/auto_inject/injection.rb +0 -71
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 811a2b285fae7c657e214c57d682488c2b1d3ed8
|
4
|
+
data.tar.gz: 4d6bff629910fc0400e2725b401c2f270fe135bc
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0220dcb80424b49015821e64bbe51245a0a8c5d0cba462fdf1150e37e00e4f92116edbada4b7ec4a18a8f015cd3bcf20c1c586f6f7c36d0f8843fb9b4d30b12e
|
7
|
+
data.tar.gz: 966a0d328c57c4d72791f30311418892ae69def872d683718bbceae688d52f3afcc76556b60741430fc5dda086659d93bf679eb0ffbf870b48a1846ba2ba0571
|
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
@@ -47,12 +47,11 @@ my_container.register(:data_store, -> { DataStore.new })
|
|
47
47
|
my_container.register(:user_repository, -> { container[:data_store][:users] })
|
48
48
|
my_container.register(:persist_user, -> { PersistUser.new })
|
49
49
|
|
50
|
-
# set up your auto-injection
|
51
|
-
|
52
|
-
AutoInject = Dry::AutoInject.new { container(my_container) }
|
50
|
+
# set up your auto-injection function
|
51
|
+
AutoInject = Dry::AutoInject(my_container)
|
53
52
|
|
54
53
|
# then simply include it in your class providing which dependencies should be
|
55
|
-
# injected automatically from the
|
54
|
+
# injected automatically from the configured container
|
56
55
|
class PersistUser
|
57
56
|
include AutoInject[:user_repository]
|
58
57
|
|
data/lib/dry/auto_inject.rb
CHANGED
@@ -1,63 +1,115 @@
|
|
1
1
|
require 'dry/auto_inject/version'
|
2
|
-
require 'dry/auto_inject/injection'
|
3
2
|
|
4
3
|
module Dry
|
5
|
-
#
|
4
|
+
# Configure an auto-injection module
|
5
|
+
#
|
6
|
+
# @example
|
7
|
+
# module MyApp
|
8
|
+
# # set up your container
|
9
|
+
# container = Dry::Container.new
|
10
|
+
#
|
11
|
+
# container.register(:data_store, -> { DataStore.new })
|
12
|
+
# container.register(:user_repository, -> { container[:data_store][:users] })
|
13
|
+
# container.register(:persist_user, -> { PersistUser.new })
|
14
|
+
#
|
15
|
+
# # set up your auto-injection function
|
16
|
+
# AutoInject = Dry::AutoInject(container)
|
17
|
+
#
|
18
|
+
# # define your injection function
|
19
|
+
# def self.Inject(*keys)
|
20
|
+
# AutoInject[*keys]
|
21
|
+
# end
|
22
|
+
# end
|
23
|
+
#
|
24
|
+
# # then simply include it in your class providing which dependencies should be
|
25
|
+
# # injected automatically from the configured container
|
26
|
+
# class PersistUser
|
27
|
+
# include MyApp::Inject(:user_repository)
|
28
|
+
#
|
29
|
+
# def call(user)
|
30
|
+
# user_repository << user
|
31
|
+
# end
|
32
|
+
# end
|
33
|
+
#
|
34
|
+
# persist_user = container[:persist_user]
|
35
|
+
#
|
36
|
+
# persist_user.call(name: 'Jane')
|
37
|
+
#
|
38
|
+
# @return [Proc] calling the returned proc builds an auto-injection module
|
6
39
|
#
|
7
40
|
# @api public
|
8
|
-
|
9
|
-
|
10
|
-
|
41
|
+
def self.AutoInject(container)
|
42
|
+
-> *names { AutoInject.new(names, container) }
|
43
|
+
end
|
44
|
+
|
45
|
+
# @api private
|
46
|
+
class AutoInject < Module
|
47
|
+
attr_reader :names
|
48
|
+
|
49
|
+
attr_reader :container
|
50
|
+
|
51
|
+
attr_reader :instance_mod
|
52
|
+
|
53
|
+
attr_reader :ivars
|
54
|
+
|
11
55
|
# @api private
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
# my_container = Dry::Container.new
|
19
|
-
#
|
20
|
-
# my_container.register(:data_store, -> { DataStore.new })
|
21
|
-
# my_container.register(:user_repository, -> { container[:data_store][:users] })
|
22
|
-
# my_container.register(:persist_user, -> { PersistUser.new })
|
23
|
-
#
|
24
|
-
# # set up your auto-injection module
|
25
|
-
#
|
26
|
-
# AutoInject = Dry::AutoInject.new { container(my_container) }
|
27
|
-
#
|
28
|
-
# # then simply include it in your class providing which dependencies should be
|
29
|
-
# # injected automatically from the configure container
|
30
|
-
# class PersistUser
|
31
|
-
# include AutoInject[:user_repository]
|
32
|
-
#
|
33
|
-
# def call(user)
|
34
|
-
# user_repository << user
|
35
|
-
# end
|
36
|
-
# end
|
37
|
-
#
|
38
|
-
# persist_user = my_container[:persist_user]
|
39
|
-
#
|
40
|
-
# persist_user.call(name: 'Jane')
|
41
|
-
#
|
42
|
-
# @return [Dry::AutoInject::Injection]
|
43
|
-
#
|
44
|
-
# @api public
|
45
|
-
def self.new(&block)
|
46
|
-
dsl = super(&block)
|
47
|
-
dsl.injection
|
56
|
+
def initialize(names, container)
|
57
|
+
@names = names
|
58
|
+
@container = container
|
59
|
+
@ivars = names.map(&:to_s).map { |s| s.split('.').last }.map(&:to_sym)
|
60
|
+
@instance_mod = Module.new
|
61
|
+
define_constructor
|
48
62
|
end
|
49
63
|
|
50
64
|
# @api private
|
51
|
-
def
|
52
|
-
|
53
|
-
|
65
|
+
def included(klass)
|
66
|
+
define_new_method(klass)
|
67
|
+
define_container(klass)
|
68
|
+
|
69
|
+
klass.send(:include, instance_mod)
|
70
|
+
|
71
|
+
super
|
54
72
|
end
|
55
73
|
|
56
|
-
|
57
|
-
|
58
|
-
# @api
|
59
|
-
def
|
60
|
-
@container
|
74
|
+
private
|
75
|
+
|
76
|
+
# @api private
|
77
|
+
def define_container(klass)
|
78
|
+
klass.instance_variable_set('@container', container)
|
79
|
+
|
80
|
+
klass.class_eval do
|
81
|
+
def self.container
|
82
|
+
if superclass.respond_to?(:container)
|
83
|
+
superclass.container
|
84
|
+
else
|
85
|
+
@container
|
86
|
+
end
|
87
|
+
end
|
88
|
+
end
|
89
|
+
end
|
90
|
+
|
91
|
+
# @api private
|
92
|
+
def define_new_method(klass)
|
93
|
+
klass.class_eval <<-RUBY, __FILE__, __LINE__ + 1
|
94
|
+
def self.new(*args)
|
95
|
+
names = [#{names.map(&:inspect).join(', ')}]
|
96
|
+
deps = names.map.with_index { |_, i| args[i] || container[names[i]] }
|
97
|
+
super(*deps)
|
98
|
+
end
|
99
|
+
RUBY
|
100
|
+
end
|
101
|
+
|
102
|
+
# @api private
|
103
|
+
def define_constructor
|
104
|
+
instance_mod.class_eval <<-RUBY, __FILE__, __LINE__ + 1
|
105
|
+
attr_reader #{ivars.map { |name| ":#{name}" }.join(', ')}
|
106
|
+
|
107
|
+
def initialize(*args)
|
108
|
+
super()
|
109
|
+
#{ivars.map.with_index { |name, i| "@#{name} = args[#{i}]" }.join("\n")}
|
110
|
+
end
|
111
|
+
RUBY
|
112
|
+
self
|
61
113
|
end
|
62
114
|
end
|
63
115
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: dry-auto_inject
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Piotr Solnica
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-
|
11
|
+
date: 2015-11-12 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -73,7 +73,6 @@ files:
|
|
73
73
|
- dry-auto_inject.gemspec
|
74
74
|
- lib/dry-auto_inject.rb
|
75
75
|
- lib/dry/auto_inject.rb
|
76
|
-
- lib/dry/auto_inject/injection.rb
|
77
76
|
- lib/dry/auto_inject/version.rb
|
78
77
|
- rakelib/rubocop.rake
|
79
78
|
homepage: https://github.com/dryrb/dry-auto_inject
|
@@ -100,3 +99,4 @@ signing_key:
|
|
100
99
|
specification_version: 4
|
101
100
|
summary: Container-agnostic automatic constructor injection
|
102
101
|
test_files: []
|
102
|
+
has_rdoc:
|
@@ -1,71 +0,0 @@
|
|
1
|
-
module Dry
|
2
|
-
# @api private
|
3
|
-
class Injection < Module
|
4
|
-
attr_reader :names
|
5
|
-
|
6
|
-
attr_reader :container
|
7
|
-
|
8
|
-
attr_reader :instance_mod
|
9
|
-
|
10
|
-
attr_reader :ivars
|
11
|
-
|
12
|
-
# @api private
|
13
|
-
def initialize(names, container)
|
14
|
-
@names = names
|
15
|
-
@container = container
|
16
|
-
@ivars = names.map(&:to_s).map { |s| s.split('.').last }.map(&:to_sym)
|
17
|
-
@instance_mod = Module.new
|
18
|
-
define_constructor
|
19
|
-
end
|
20
|
-
|
21
|
-
# @api private
|
22
|
-
def included(klass)
|
23
|
-
define_new_method(klass)
|
24
|
-
define_container(klass)
|
25
|
-
|
26
|
-
klass.send(:include, instance_mod)
|
27
|
-
|
28
|
-
super
|
29
|
-
end
|
30
|
-
|
31
|
-
private
|
32
|
-
|
33
|
-
# @api private
|
34
|
-
def define_container(klass)
|
35
|
-
klass.instance_variable_set('@container', container)
|
36
|
-
|
37
|
-
klass.class_eval do
|
38
|
-
def self.container
|
39
|
-
if superclass.respond_to?(:container)
|
40
|
-
superclass.container
|
41
|
-
else
|
42
|
-
@container
|
43
|
-
end
|
44
|
-
end
|
45
|
-
end
|
46
|
-
end
|
47
|
-
|
48
|
-
# @api private
|
49
|
-
def define_new_method(klass)
|
50
|
-
klass.class_eval <<-RUBY, __FILE__, __LINE__ + 1
|
51
|
-
def self.new(*args)
|
52
|
-
names = [#{names.map(&:inspect).join(', ')}]
|
53
|
-
deps = names.map.with_index { |_, i| args[i] || container[names[i]] }
|
54
|
-
super(*deps)
|
55
|
-
end
|
56
|
-
RUBY
|
57
|
-
end
|
58
|
-
|
59
|
-
# @api private
|
60
|
-
def define_constructor
|
61
|
-
instance_mod.class_eval <<-RUBY, __FILE__, __LINE__ + 1
|
62
|
-
attr_reader #{ivars.map { |name| ":#{name}" }.join(', ')}
|
63
|
-
|
64
|
-
def initialize(*args)
|
65
|
-
#{ivars.map.with_index { |name, i| "@#{name} = args[#{i}]" }.join("\n")}
|
66
|
-
end
|
67
|
-
RUBY
|
68
|
-
self
|
69
|
-
end
|
70
|
-
end
|
71
|
-
end
|