dry-auto_inject 0.0.1 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: f2aa9cdc617e2bfdf4a1c13a3cada9ff2df3a402
4
- data.tar.gz: 4884e6893aa3c5e41de78fcff1548e1cb63bde5d
3
+ metadata.gz: 811a2b285fae7c657e214c57d682488c2b1d3ed8
4
+ data.tar.gz: 4d6bff629910fc0400e2725b401c2f270fe135bc
5
5
  SHA512:
6
- metadata.gz: a05899761a453cd74c5d1fef9ad82a13e7cf5c64c4251314ee0140fedf3ef088cb3714d07aae53598a6a5e23843953c9c22ac020964f40416bcb68bfd417ae9b
7
- data.tar.gz: dc5c7e8e5b0d4112ef032d5b7ea9b0a8fc1fe6223e29e3c741c13a0260ec312686a1a21b6fdab45f199fe52c7c56c8b99182f4d34f54cf1866c5f3fec6f85041
6
+ metadata.gz: 0220dcb80424b49015821e64bbe51245a0a8c5d0cba462fdf1150e37e00e4f92116edbada4b7ec4a18a8f015cd3bcf20c1c586f6f7c36d0f8843fb9b4d30b12e
7
+ data.tar.gz: 966a0d328c57c4d72791f30311418892ae69def872d683718bbceae688d52f3afcc76556b60741430fc5dda086659d93bf679eb0ffbf870b48a1846ba2ba0571
data/CHANGELOG.md CHANGED
@@ -1,3 +1,8 @@
1
- v0.0.1 2015-08-20
1
+ # v0.1.0 2015-11-12
2
+
3
+ Changed interface from `Dry::AutoInject.new { container(some_container) }` to
4
+ `Dry::AutoInject(some_container)`.
5
+
6
+ # v0.0.1 2015-08-20
2
7
 
3
8
  First public release \o/
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 module
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 configure container
54
+ # injected automatically from the configured container
56
55
  class PersistUser
57
56
  include AutoInject[:user_repository]
58
57
 
@@ -1,63 +1,115 @@
1
1
  require 'dry/auto_inject/version'
2
- require 'dry/auto_inject/injection'
3
2
 
4
3
  module Dry
5
- # Main DSL class
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
- class AutoInject
9
- # @attr_reader [Injection] injection The configured injection module
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
- attr_reader :injection
13
-
14
- # Configure an auto-injection module
15
- #
16
- # @example
17
- # # set up your container
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 initialize(&block)
52
- instance_exec(&block)
53
- @injection = -> *names { Injection.new(names, @container) }
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
- # Set up the container for the injection module
57
- #
58
- # @api public
59
- def container(container)
60
- @container = 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
@@ -1,5 +1,5 @@
1
1
  module Dry
2
- class AutoInject
3
- VERSION = '0.0.1'.freeze
2
+ class AutoInject < Module
3
+ VERSION = '0.1.0'.freeze
4
4
  end
5
5
  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.1
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-08-20 00:00:00.000000000 Z
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