dependor 1.0.0 → 1.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile.lock +1 -1
- data/lib/dependor.rb +14 -13
- data/lib/dependor/auto_inject.rb +6 -2
- data/lib/dependor/auto_injector.rb +2 -2
- data/lib/dependor/dependency_names_cache.rb +18 -0
- data/lib/dependor/instantiator.rb +5 -14
- data/lib/dependor/isolate.rb +2 -1
- data/lib/dependor/version.rb +1 -1
- data/spec/dependor/instantiator_spec.rb +2 -1
- metadata +4 -3
data/Gemfile.lock
CHANGED
data/lib/dependor.rb
CHANGED
@@ -1,17 +1,18 @@
|
|
1
1
|
require 'dependor/constructor'
|
2
2
|
|
3
3
|
module Dependor
|
4
|
-
autoload :AutoInject,
|
5
|
-
autoload :AutoInjector,
|
6
|
-
autoload :ClassNameResolver,
|
7
|
-
autoload :CustomizedInjector,
|
8
|
-
autoload :
|
9
|
-
autoload :
|
10
|
-
autoload :
|
11
|
-
autoload :
|
12
|
-
autoload :
|
13
|
-
autoload :
|
14
|
-
autoload :
|
15
|
-
autoload :
|
16
|
-
autoload :
|
4
|
+
autoload :AutoInject, 'dependor/auto_inject'
|
5
|
+
autoload :AutoInjector, 'dependor/auto_injector'
|
6
|
+
autoload :ClassNameResolver, 'dependor/class_name_resolver'
|
7
|
+
autoload :CustomizedInjector, 'dependor/customized_injector'
|
8
|
+
autoload :DependencyNamesCache, 'dependor/dependency_names_cache'
|
9
|
+
autoload :EvaluatingInjector, 'dependor/evaluating_injector'
|
10
|
+
autoload :Injectable, 'dependor/injectable'
|
11
|
+
autoload :Instantiator, 'dependor/instantiator'
|
12
|
+
autoload :Isolate, 'dependor/isolate'
|
13
|
+
autoload :Let, 'dependor/let'
|
14
|
+
autoload :SendingInjector, 'dependor/sending_injector'
|
15
|
+
autoload :Shorty, 'dependor/shorty'
|
16
|
+
autoload :UnknownObject, 'dependor/exceptions'
|
17
|
+
autoload :VERSION, 'dependor/version'
|
17
18
|
end
|
data/lib/dependor/auto_inject.rb
CHANGED
@@ -24,14 +24,18 @@ module Dependor
|
|
24
24
|
|
25
25
|
def inject(klass, overrides = {})
|
26
26
|
injector = Dependor::CustomizedInjector.new(auto_injector, overrides)
|
27
|
-
instantiator = Dependor::Instantiator.new(injector)
|
27
|
+
instantiator = Dependor::Instantiator.new(injector, dependency_names)
|
28
28
|
instantiator.instantiate(klass)
|
29
29
|
end
|
30
30
|
|
31
31
|
private
|
32
32
|
|
33
33
|
def auto_injector
|
34
|
-
@auto_injector ||= Dependor::AutoInjector.new(self, self.class.search_modules)
|
34
|
+
@auto_injector ||= Dependor::AutoInjector.new(self, dependency_names, self.class.search_modules)
|
35
|
+
end
|
36
|
+
|
37
|
+
def dependency_names
|
38
|
+
@dependency_names ||= Dependor::DependencyNamesCache.new
|
35
39
|
end
|
36
40
|
|
37
41
|
end
|
@@ -1,9 +1,9 @@
|
|
1
1
|
module Dependor
|
2
2
|
class AutoInjector
|
3
3
|
|
4
|
-
def initialize(injector, search_modules)
|
4
|
+
def initialize(injector, dependency_names, search_modules)
|
5
5
|
@injector = injector
|
6
|
-
@instantiator = Instantiator.new(self)
|
6
|
+
@instantiator = Instantiator.new(self, dependency_names)
|
7
7
|
@class_name_resolver = ClassNameResolver.new(search_modules)
|
8
8
|
end
|
9
9
|
|
@@ -0,0 +1,18 @@
|
|
1
|
+
module Dependor
|
2
|
+
class DependencyNamesCache
|
3
|
+
def initialize
|
4
|
+
@constructor_params = {}
|
5
|
+
end
|
6
|
+
|
7
|
+
def for_class(klass)
|
8
|
+
@constructor_params[klass] ||= get_constructor_params(klass)
|
9
|
+
end
|
10
|
+
|
11
|
+
private
|
12
|
+
|
13
|
+
def get_constructor_params(klass)
|
14
|
+
params = klass.instance_method(:initialize).parameters
|
15
|
+
params.select{|type, name| type == :req}.map{|type, name| name}
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
@@ -1,24 +1,15 @@
|
|
1
1
|
module Dependor
|
2
2
|
class Instantiator
|
3
|
-
|
3
|
+
attr_reader :dependency_names
|
4
|
+
|
5
|
+
def initialize(injector, dependency_names)
|
4
6
|
@injector = injector
|
5
|
-
@
|
7
|
+
@dependency_names = dependency_names
|
6
8
|
end
|
7
9
|
|
8
10
|
def instantiate(klass)
|
9
|
-
dependencies =
|
11
|
+
dependencies = dependency_names.for_class(klass).map{|name| @injector.get(name)}
|
10
12
|
return klass.new(*dependencies)
|
11
13
|
end
|
12
|
-
|
13
|
-
private
|
14
|
-
|
15
|
-
def dependecy_names_for(klass)
|
16
|
-
@constructor_params[klass] ||= get_constructor_params(klass)
|
17
|
-
end
|
18
|
-
|
19
|
-
def get_constructor_params(klass)
|
20
|
-
params = klass.instance_method(:initialize).parameters
|
21
|
-
params.select{|type, name| type == :req}.map{|type, name| name}
|
22
|
-
end
|
23
14
|
end
|
24
15
|
end
|
data/lib/dependor/isolate.rb
CHANGED
@@ -9,7 +9,8 @@ module Dependor
|
|
9
9
|
sending_injector = SendingInjector.new(self)
|
10
10
|
injector = CustomizedInjector.new(sending_injector, overrides)
|
11
11
|
end
|
12
|
-
|
12
|
+
dependecy_names = DependencyNamesCache.new
|
13
|
+
instantiator = Instantiator.new(injector, dependecy_names)
|
13
14
|
instantiator.instantiate(klass)
|
14
15
|
end
|
15
16
|
end
|
data/lib/dependor/version.rb
CHANGED
@@ -2,7 +2,8 @@ require 'spec_helper'
|
|
2
2
|
|
3
3
|
describe Dependor::Instantiator do
|
4
4
|
let(:injector) { double(:injector) }
|
5
|
-
let(:
|
5
|
+
let(:dependency_names) { Dependor::DependencyNamesCache.new }
|
6
|
+
let(:instantiator) { Dependor::Instantiator.new(injector, dependency_names) }
|
6
7
|
|
7
8
|
it "instantiates objects with no-arg constructors" do
|
8
9
|
klass = Class.new do
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: dependor
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.1
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -148,6 +148,7 @@ files:
|
|
148
148
|
- lib/dependor/class_name_resolver.rb
|
149
149
|
- lib/dependor/constructor.rb
|
150
150
|
- lib/dependor/customized_injector.rb
|
151
|
+
- lib/dependor/dependency_names_cache.rb
|
151
152
|
- lib/dependor/evaluating_injector.rb
|
152
153
|
- lib/dependor/exceptions.rb
|
153
154
|
- lib/dependor/injectable.rb
|
@@ -184,7 +185,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
184
185
|
version: '0'
|
185
186
|
segments:
|
186
187
|
- 0
|
187
|
-
hash: -
|
188
|
+
hash: -3867103565292776709
|
188
189
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
189
190
|
none: false
|
190
191
|
requirements:
|
@@ -193,7 +194,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
193
194
|
version: '0'
|
194
195
|
segments:
|
195
196
|
- 0
|
196
|
-
hash: -
|
197
|
+
hash: -3867103565292776709
|
197
198
|
requirements: []
|
198
199
|
rubyforge_project: dependor
|
199
200
|
rubygems_version: 1.8.25
|