sinject 0.2.0 → 0.2.1
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/README.md +10 -8
- data/lib/sinject.rb +37 -7
- 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: 84ae556b8e2fd2b90c6a6098c655a72133e3190c
|
4
|
+
data.tar.gz: d6994c60f60a5dad2d1cb7179c166c1255301585
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: bc23325620a024329a2acb4c49fe97fbed19e647353e943da931cb7c9ed900175ccfcd4dc2ced889a1366a35237f696933e3c401247693b841435be9c491ea7b
|
7
|
+
data.tar.gz: 1e8ba21b96980fbd99b62a18f59cb28b6c9f0549301a8882b09255262ec0ccdbc89a1b30e8d652748fd8c659883dd49cdd89dbb6789403ca99c4df056118bcb5
|
data/README.md
CHANGED
@@ -43,9 +43,8 @@ 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
|
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:
|
47
47
|
|
48
|
-
#register your dependencies
|
49
48
|
container.register(:cache_store, RedisCacheStore, true) do
|
50
49
|
instance = RedisCacheStore.new
|
51
50
|
instance.host = 'http://localhost'
|
@@ -53,17 +52,16 @@ Dependencies that require custom initialization can be registered with an initia
|
|
53
52
|
instance
|
54
53
|
end
|
55
54
|
|
56
|
-
Dependencies with a custom initialization block must return an object of the registered dependency class
|
55
|
+
Dependencies with a custom initialization block must return an object of the registered dependency class type, if an unexpected instance is returned then Sinject will raise a `DependencyInitializeException`.
|
57
56
|
|
58
57
|
**Dependency Contracts**
|
59
58
|
|
60
|
-
Dependency contracts can be defined
|
59
|
+
Dependency contracts can be defined to validate registered dependencies are valid for the task they are being registered for.
|
61
60
|
|
62
|
-
To create a dependency contract you
|
61
|
+
To create a dependency contract you need to create a new class with empty methods for each of the methods that the dependency needs to respond to in order to fulfill it's role:
|
63
62
|
|
64
|
-
#initialize the container
|
65
63
|
class LoggerContract
|
66
|
-
def write
|
64
|
+
def write(message)
|
67
65
|
end
|
68
66
|
end
|
69
67
|
|
@@ -72,7 +70,11 @@ Then when registering a dependency for the role the contract is written for, you
|
|
72
70
|
#register the dependency
|
73
71
|
container.register(:logger, FileLogger, false, LoggerContract)
|
74
72
|
|
75
|
-
Sinject will then validate that the registered dependency meets the requirements specified within the contract. If a dependency does not meet the contract requirements then
|
73
|
+
Sinject will then validate that the registered dependency meets the requirements specified within the contract. If a dependency does not meet the contract requirements then 1 of the following exceptions will be raised:
|
74
|
+
|
75
|
+
- `DependencyContractMissingMethodsException` is raised when 1 or more methods from the contract could not be found on the dependency.
|
76
|
+
- `DependencyContractInvalidParametersException` is raised when the parameters of a contract method do not match the parameters found on a dependency method.
|
77
|
+
|
76
78
|
|
77
79
|
**Assigning dependencies**
|
78
80
|
|
data/lib/sinject.rb
CHANGED
@@ -41,11 +41,8 @@ class SinjectContainer
|
|
41
41
|
|
42
42
|
#check if a contract has been specified
|
43
43
|
if contract_class_name != nil
|
44
|
-
#
|
45
|
-
|
46
|
-
if !missing_methods.empty?
|
47
|
-
raise DependencyContractException.new(missing_methods)
|
48
|
-
end
|
44
|
+
#validate the dependency class against the contract
|
45
|
+
validate_contract(dependency_class_name, contract_class_name)
|
49
46
|
end
|
50
47
|
|
51
48
|
item = ContainerItem.new
|
@@ -91,12 +88,33 @@ class SinjectContainer
|
|
91
88
|
private
|
92
89
|
|
93
90
|
def validate_contract(dependency_class, contract_class)
|
91
|
+
|
94
92
|
#get the methods defined for the contract
|
95
93
|
contract_methods = (contract_class.instance_methods - Object.instance_methods)
|
96
94
|
#get the methods defined for the dependency
|
97
95
|
dependency_methods = (dependency_class.instance_methods - Object.instance_methods)
|
98
96
|
#calculate any methods specified in the contract that are not specified in the dependency
|
99
|
-
contract_methods - dependency_methods
|
97
|
+
missing_methods = contract_methods - dependency_methods
|
98
|
+
|
99
|
+
if !missing_methods.empty?
|
100
|
+
raise DependencyContractMissingMethodsException.new(missing_methods)
|
101
|
+
end
|
102
|
+
|
103
|
+
#loop through each contract method
|
104
|
+
contract_methods.each do |method|
|
105
|
+
|
106
|
+
#get the contract method parameters
|
107
|
+
cmp = contract_class.instance_method(method).parameters
|
108
|
+
#get teh dependency method parameters
|
109
|
+
dmp = dependency_class.instance_method(method).parameters
|
110
|
+
|
111
|
+
#check if the parameters match for both methods
|
112
|
+
if cmp != dmp
|
113
|
+
raise DependencyContractInvalidParametersException.new(method, cmp)
|
114
|
+
end
|
115
|
+
|
116
|
+
end
|
117
|
+
|
100
118
|
end
|
101
119
|
|
102
120
|
def create_instance(item)
|
@@ -153,7 +171,7 @@ class Class
|
|
153
171
|
end
|
154
172
|
end
|
155
173
|
|
156
|
-
class
|
174
|
+
class DependencyContractMissingMethodsException < StandardError
|
157
175
|
def initialize(methods)
|
158
176
|
@methods = methods
|
159
177
|
end
|
@@ -164,6 +182,18 @@ class DependencyContractException < StandardError
|
|
164
182
|
end
|
165
183
|
end
|
166
184
|
|
185
|
+
class DependencyContractInvalidParametersException < StandardError
|
186
|
+
def initialize(method, parameters)
|
187
|
+
@method = method
|
188
|
+
@parameters = parameters
|
189
|
+
end
|
190
|
+
|
191
|
+
def to_s
|
192
|
+
parameter_names = @parameters.join(', ')
|
193
|
+
"The method signature of method: '#{@method}' does not match the contract parameters: '#{parameter_names}'"
|
194
|
+
end
|
195
|
+
end
|
196
|
+
|
167
197
|
class DependencyInitializeException < StandardError
|
168
198
|
|
169
199
|
def initialize(expected_type)
|
data/lib/sinject/version.rb
CHANGED