injectable_dependencies 1.0.0 → 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +1 -0
- data/lib/injectable_dependencies/version.rb +1 -1
- data/lib/injectable_dependencies.rb +17 -2
- data/spec/injectable_dependencies_spec.rb +18 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 061ab1c3ffd232aff0541defb77b8491d670421b
|
4
|
+
data.tar.gz: 1be76a0ab83cf9a50690543c85780e9309d30591
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: cc5a0a1b6aa5eaf381d56dfd50e6fb540ff65e5a407fe5209f652db692ecaf73d00fe3dc8fb2159da1066ef23d76a705a3d31fb7df15ab92040d06cff5448b80
|
7
|
+
data.tar.gz: 680bc81279cd67075c31de0beb357d2ec802a7430442fee92ade32f92ddb525d4eabdad7c6cc76e75458221097d6bb08e70a60626e2915af3e3708159e8f1990
|
data/.gitignore
CHANGED
@@ -17,12 +17,18 @@ require 'active_support/concern'
|
|
17
17
|
#
|
18
18
|
module InjectableDependencies
|
19
19
|
UndeclaredDependencyError = Class.new(StandardError)
|
20
|
+
MissingImplementationError = Class.new(StandardError)
|
21
|
+
|
20
22
|
extend ActiveSupport::Concern
|
21
23
|
|
22
24
|
module ClassMethods
|
23
|
-
def dependency(name, &
|
25
|
+
def dependency(name, &block)
|
24
26
|
dependencies << name
|
25
|
-
|
27
|
+
if block
|
28
|
+
define_method(name, &block) # available to all instances
|
29
|
+
else
|
30
|
+
no_default_dependencies << name
|
31
|
+
end
|
26
32
|
end
|
27
33
|
|
28
34
|
protected
|
@@ -30,10 +36,19 @@ module InjectableDependencies
|
|
30
36
|
def dependencies
|
31
37
|
@dependencies ||= []
|
32
38
|
end
|
39
|
+
|
40
|
+
def no_default_dependencies
|
41
|
+
@no_default_dependencies ||= []
|
42
|
+
end
|
33
43
|
end
|
34
44
|
|
35
45
|
def initialize_dependencies(overrides = {})
|
36
46
|
return unless overrides
|
47
|
+
self.class.send(:no_default_dependencies).each do |name|
|
48
|
+
unless overrides.has_key?(name)
|
49
|
+
raise MissingImplementationError.new("No implementation was provided for dependency ##{name}")
|
50
|
+
end
|
51
|
+
end
|
37
52
|
overrides.each do |k,v|
|
38
53
|
unless self.class.send(:dependencies).include?(k)
|
39
54
|
raise UndeclaredDependencyError, "Cannot override undeclared dependency '#{k}'."
|
@@ -46,4 +46,22 @@ describe InjectableDependencies do
|
|
46
46
|
.to raise_error(InjectableDependencies::UndeclaredDependencyError,
|
47
47
|
"Cannot override undeclared dependency 'undeclared_thing'.")
|
48
48
|
end
|
49
|
+
|
50
|
+
context 'when a dependency is defined without a default implementation' do
|
51
|
+
let(:dependent_class) {
|
52
|
+
Class.new do
|
53
|
+
include InjectableDependencies
|
54
|
+
dependency(:baz)
|
55
|
+
def initialize(dependencies = {})
|
56
|
+
initialize_dependencies(dependencies)
|
57
|
+
end
|
58
|
+
end
|
59
|
+
}
|
60
|
+
|
61
|
+
it 'raises an error if an implementation is not provided during dependency initialization' do
|
62
|
+
expect{ dependent_class.new }.to \
|
63
|
+
raise_error(InjectableDependencies::MissingImplementationError,
|
64
|
+
'No implementation was provided for dependency #baz')
|
65
|
+
end
|
66
|
+
end
|
49
67
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: injectable_dependencies
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- John Wilger
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2014-03-08 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|