sinject 0.2.2 → 0.2.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +7 -5
- data/lib/sinject.rb +17 -0
- data/lib/sinject/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b4b501138ed0b8872f405861b14cde245114c751
|
4
|
+
data.tar.gz: d88eebbbfd2396a0ac99b666bedd68ac2ff0697a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 91617b66be38aa50184ce4632e0599245f51b6aa8a53f235bb7f3d53d051f3b8b7a57eafec890fa65cd86848166dd846c78e9d613ed54d8fda5d80b0d27487df
|
7
|
+
data.tar.gz: d4faa88400fc584775400549212b7bb2b7f868999d831891fe7900f08d3875303389308216cd3efa05b8692a14e617d35a0858db414c216f7b2bf4b418457a8e
|
data/README.md
CHANGED
@@ -27,7 +27,7 @@ If you're using rails then after you've installed the gem you need to create a *
|
|
27
27
|
|
28
28
|
**Registering dependencies**
|
29
29
|
|
30
|
-
Dependency objects need to be registered with the container before use, to do so you need to configure the SinjectContainer
|
30
|
+
Dependency objects need to be registered with the container before use, to do so you need to configure the SinjectContainer: [If you're using rails this will need to be done within the *'dependencies.rb'* file]
|
31
31
|
|
32
32
|
#initialize the container
|
33
33
|
container = SinjectContainer.new
|
@@ -43,7 +43,7 @@ Dependencies can be registered with the container in 2 modes:
|
|
43
43
|
|
44
44
|
The registration mode can be set by specifying **true** or **false** to the *'single_instance'* argument of the containers register method.
|
45
45
|
|
46
|
-
Dependencies that require custom initialization can be registered with an initialization block to handle creation of the dependency, this allows you more control over how the dependency is created:
|
46
|
+
Dependencies that require custom initialization can be registered with an initialization block to handle the creation of the dependency, this allows you more control over how the dependency is created if required:
|
47
47
|
|
48
48
|
container.register(:cache_store, RedisCacheStore, true) do
|
49
49
|
instance = RedisCacheStore.new
|
@@ -77,9 +77,9 @@ Sinject will then validate that the registered dependency meets the requirements
|
|
77
77
|
|
78
78
|
**Dependency Groups**
|
79
79
|
|
80
|
-
Dependency
|
80
|
+
Dependency registration groups can be created to allow groups of dependencies to be set without the need for manual registration *(e.g. to include with a gem for auto registration)*, or to allow different dependency groups to be loaded in different circumstances *(e.g. per environment)*.
|
81
81
|
|
82
|
-
To create a dependency group create a class that inherits from the DependencyGroup base class and implement the
|
82
|
+
To create a dependency group, create a class that inherits from the `DependencyGroup` base class and implement the `register` & `is_valid?` methods.
|
83
83
|
|
84
84
|
For example:
|
85
85
|
|
@@ -95,8 +95,10 @@ For example:
|
|
95
95
|
end
|
96
96
|
end
|
97
97
|
|
98
|
-
|
98
|
+
To load valid dependency groups the following method needs to be called from the container:
|
99
99
|
|
100
|
+
container.load_groups
|
101
|
+
|
100
102
|
**Assigning dependencies**
|
101
103
|
|
102
104
|
To assign a dependency to an object you need to add the dependency attribute to the class and specify the symbol key that was used to register the dependency with the SinjectContainer:
|
data/lib/sinject.rb
CHANGED
@@ -39,6 +39,11 @@ class SinjectContainer
|
|
39
39
|
# single_instance: (Boolean)
|
40
40
|
def register(key, dependency_class_name, single_instance = false, contract_class_name = nil, &initialize_block)
|
41
41
|
|
42
|
+
#check if a dependency has already been registered for this key.
|
43
|
+
if is_registered?(key)
|
44
|
+
raise DependencyRegistrationException.new(key)
|
45
|
+
end
|
46
|
+
|
42
47
|
#check if a contract has been specified
|
43
48
|
if contract_class_name != nil
|
44
49
|
#validate the dependency class against the contract
|
@@ -215,6 +220,18 @@ class DependencyInitializeException < StandardError
|
|
215
220
|
|
216
221
|
end
|
217
222
|
|
223
|
+
class DependencyRegistrationException < StandardError
|
224
|
+
|
225
|
+
def initialize(key)
|
226
|
+
@key = key
|
227
|
+
end
|
228
|
+
|
229
|
+
def to_s
|
230
|
+
"A Dependency has already been registered for the key: '#{key}'"
|
231
|
+
end
|
232
|
+
|
233
|
+
end
|
234
|
+
|
218
235
|
class DependencyGroup
|
219
236
|
|
220
237
|
def register(container)
|
data/lib/sinject/version.rb
CHANGED