sinject 1.0.2 → 1.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
- SHA1:
3
- metadata.gz: 2ff53f60728f9e82cae343665133a1afb5fd25a4
4
- data.tar.gz: 484be6ecc45857889ad818ac5f9cb5d4d5af86cd
2
+ SHA256:
3
+ metadata.gz: 811bf8beccc5b35e04e735fefc564aebcd551cd8f7514cdb4766b8a57776b7ba
4
+ data.tar.gz: 2b5c71e629c158779a657a533d247081c340c01cb1b748491ce5194dc8c4e14e
5
5
  SHA512:
6
- metadata.gz: 13c4fca49e08c1da9e8936b4890c087377bfc08fdf0e234b122fdd5279e5d428451c2763e04ceecbdd9a4bff290e58057fdd3db1e13e4a7a5f45b892b9d290c9
7
- data.tar.gz: 670cda2067966140a1f3e1577fc24480e7b38a38aaa74366937a06de8731e997d13558ec93bceb17b45acb9c54395f803d6694bd3eb77a2f7c94917c49b0bcc7
6
+ metadata.gz: 48e343a9ca5e54224f5b248e47600169c3212587aacd9f1fecadbf2d1f59f4a7343ab51adba893da1200e1349d1d54cad34030f3c7c445554fdf05e94e0acfdb
7
+ data.tar.gz: 44da113fef31cc4eeea74ae590592871bfecb733ec6ba96edd9b3ea934cd4ec0b9f8dc536ee790596ee1934c6244024c8c3908fad8f364f5464bb05e86905089
@@ -7,22 +7,25 @@ module Sinject
7
7
  end
8
8
 
9
9
  def initialize(singleton=true)
10
- @store = []
11
- if singleton
12
- Sinject::Container.instance = self
13
- end
10
+ @store = {}
11
+ Sinject::Container.instance = self if singleton
14
12
  end
15
13
 
16
14
  # Check if an object has been registered with the container.
17
15
  #
18
16
  # Example:
19
- # >> Sinject::Container.instance.is_registered? :object_key
17
+ # >> Sinject::Container.instance.registered? :object_key
20
18
  # => true
21
19
  #
22
20
  # Arguments:
23
21
  # key: (Symbol)
22
+ def registered?(key)
23
+ @store.has_key?(key)
24
+ end
25
+ # @deprecated: Use registered? method instead
24
26
  def is_registered?(key)
25
- !@store.select { |i| i.key == key}.empty?
27
+ puts "[#{self.class}] - #is_registered? method is deprecated please use #registered? instead."
28
+ registered?(key)
26
29
  end
27
30
 
28
31
  # Register an object with the container.
@@ -38,22 +41,15 @@ module Sinject
38
41
  # class_name: (ClassName)
39
42
  # single_instance: (Boolean)
40
43
  def register(options = {}, &initialize_block)
44
+ raise Sinject::DependencyRegistrationKeyNotSpecifiedException.new unless options.has_key?(:key)
41
45
 
42
- if(!options.has_key?(:key))
43
- raise Sinject::DependencyRegistrationKeyNotSpecifiedException.new
44
- end
45
-
46
- if(!options.has_key?(:class))
47
- raise Sinject::DependencyRegistrationClassNotSpecifiedException.new
48
- end
46
+ raise Sinject::DependencyRegistrationClassNotSpecifiedException.new unless options.has_key?(:class)
49
47
 
50
48
  key = options[:key]
51
49
  dependency_class_name = options[:class]
52
50
 
53
- #check if a dependency has already been registered for this key.
54
- if is_registered?(key)
55
- raise Sinject::DependencyRegistrationException.new(key)
56
- end
51
+ # check if a dependency has already been registered for this key.
52
+ raise Sinject::DependencyRegistrationException.new(key) if registered?(key)
57
53
 
58
54
  single_instance = false
59
55
  contract_class_name = nil
@@ -66,11 +62,8 @@ module Sinject
66
62
  contract_class_name = options[:contract]
67
63
  end
68
64
 
69
- #check if a contract has been specified
70
- if contract_class_name != nil
71
- #validate the dependency class against the contract
72
- validate_contract(dependency_class_name, contract_class_name)
73
- end
65
+ # Validate the dependency class against the contract if a contract has been specified
66
+ validate_contract(dependency_class_name, contract_class_name) unless contract_class_name.nil?
74
67
 
75
68
  item = Sinject::ContainerItem.new
76
69
  item.key = key
@@ -78,7 +71,8 @@ module Sinject
78
71
  item.class_name = dependency_class_name
79
72
  item.initialize_block = initialize_block
80
73
 
81
- @store.push(item)
74
+ @store[item.key] = item
75
+ true
82
76
  end
83
77
 
84
78
  # Get an object from the container.
@@ -90,25 +84,24 @@ module Sinject
90
84
  # Arguments:
91
85
  # key: (Symbol)
92
86
  def get(key)
93
- #get the dependency from the container store for the specified key
94
- items = @store.select { |i| i.key == key}
95
- if !items.empty?
96
- item = items.first
97
-
98
- #check if the item has been registered as a single instance item.
87
+ # get the dependency from the container store for the specified key
88
+ item = @store[key]
89
+ if !item.nil?
90
+ # check if the item has been registered as a single instance item.
99
91
  if item.single_instance == true
100
- #check if the instance needs to be created
101
- if item.instance == nil
102
- item.instance = create_instance(item)
103
- end
92
+ # check if the instance needs to be created
93
+ item.instance = create_instance(item) if item.instance.nil?
94
+
104
95
  return item.instance
105
96
  else
106
97
  return create_instance(item)
107
98
  end
108
99
  else
109
- #no dependency has been registered for the specified key, attempt to convert the key into a class name and initialize it.
100
+ # no dependency has been registered for the specified key,
101
+ # attempt to convert the key into a class name and initialize it.
110
102
  class_name = "#{key}".split('_').collect(&:capitalize).join
111
- puts "[Sinject] - WARNING: No registered dependency could be found for key: #{key}. Attempting to load class: #{class_name}."
103
+ puts "[#{self.class}] - WARNING: No registered dependency could be found for key: #{key}. " \
104
+ "Attempting to load class: #{class_name}."
112
105
  Object.const_get(class_name).new
113
106
  end
114
107
  end
@@ -116,7 +109,7 @@ module Sinject
116
109
  def load_groups
117
110
  Sinject::DependencyGroup.descendants.each do |g|
118
111
  group = g.new
119
- if group.is_valid?
112
+ if (group.respond_to?(:valid?) && group.valid?) || (group.respond_to?(:is_valid?) && group.is_valid?)
120
113
  group.register(self)
121
114
  end
122
115
  end
@@ -125,46 +118,69 @@ module Sinject
125
118
  private
126
119
 
127
120
  def validate_contract(dependency_class, contract_class)
128
-
129
- #get the methods defined for the contract
121
+ # get the methods defined for the contract
130
122
  contract_methods = (contract_class.instance_methods - Object.instance_methods)
131
- #get the methods defined for the dependency
123
+ # get the methods defined for the dependency
132
124
  dependency_methods = (dependency_class.instance_methods - Object.instance_methods)
133
- #calculate any methods specified in the contract that are not specified in the dependency
125
+ # calculate any methods specified in the contract that are not specified in the dependency
134
126
  missing_methods = contract_methods - dependency_methods
135
127
 
136
128
  if !missing_methods.empty?
137
129
  raise Sinject::DependencyContractMissingMethodsException.new(missing_methods)
138
130
  end
139
131
 
140
- #loop through each contract method
132
+ # loop through each contract method
141
133
  contract_methods.each do |method|
134
+ # get the contract method parameters
135
+ contract_params = contract_class.instance_method(method).parameters.map{ |p| { type: p[0], name: p[1] } }
142
136
 
143
- #get the contract method parameters
144
- cmp = contract_class.instance_method(method).parameters
145
- #get teh dependency method parameters
146
- dmp = dependency_class.instance_method(method).parameters
137
+ # get the dependency method parameters
138
+ dependency_params = dependency_class.instance_method(method).parameters.map{ |p| { type: p[0], name: p[1] } }
147
139
 
148
- #check if the parameters match for both methods
149
- if cmp != dmp
150
- raise Sinject::DependencyContractInvalidParametersException.new(method, cmp)
140
+ errors = []
141
+
142
+ contract_params.each do |cp|
143
+ dp = dependency_params.detect { |p| p[:name] == cp[:name] }
144
+ if dp.nil? || !match?(cp, dp)
145
+ errors << cp[:name]
146
+ end
147
+ end
148
+
149
+ dependency_params.each do |dp|
150
+ cp = contract_params.detect { |p| p[:name] == dp[:name] }
151
+ if cp.nil?
152
+ errors << dp[:name]
153
+ end
151
154
  end
152
155
 
156
+ # check if any parameter errors
157
+ if errors.length > 0
158
+ raise Sinject::DependencyContractInvalidParametersException.new(method, errors)
159
+ end
153
160
  end
161
+ end
154
162
 
163
+ def match?(contract, dependency)
164
+ return true if contract[:type] == dependency[:type]
165
+ return true if contract[:type] == :req && dependency[:type] == :opt
166
+ return true if contract[:type] == :keyreq && dependency[:type] == :key
167
+ return false
155
168
  end
156
169
 
157
- def create_instance(item)
170
+ # this method is called to get a standard param type for comparison purposes
171
+ def param_type(type)
172
+ return :arg if type == :opt || type == :req
173
+ return :key if type == :keyreq || type == :key
174
+ end
158
175
 
159
- #check if a custom initializer block has been specified
176
+ def create_instance(item)
177
+ # check if a custom initializer block has been specified
160
178
  if item.initialize_block != nil
161
- #call the block to create the dependency instance
179
+ # call the block to create the dependency instance
162
180
  instance = item.initialize_block.call
163
181
 
164
- #verify the block created the expected dependency type
165
- if !instance.is_a?(item.class_name)
166
- raise Sinject::DependencyInitializeException.new(item.class_name)
167
- end
182
+ # verify the block created the expected dependency type
183
+ raise Sinject::DependencyInitializeException.new(item.class_name) unless instance.is_a?(item.class_name)
168
184
  else
169
185
  instance = item.class_name.new
170
186
  end
@@ -172,4 +188,4 @@ module Sinject
172
188
  instance
173
189
  end
174
190
  end
175
- end
191
+ end
@@ -1,17 +1,10 @@
1
1
  module Sinject
2
2
  class DependencyGroup
3
-
4
3
  def register(container)
5
-
6
- end
7
-
8
- def is_valid?
9
- return true
10
4
  end
11
5
 
12
6
  def self.descendants
13
7
  ObjectSpace.each_object(Class).select { |klass| klass < self }
14
8
  end
15
-
16
9
  end
17
- end
10
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sinject
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.2
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sage One
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-11-30 00:00:00.000000000 Z
11
+ date: 2018-03-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -87,7 +87,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
87
87
  version: '0'
88
88
  requirements: []
89
89
  rubyforge_project:
90
- rubygems_version: 2.4.5
90
+ rubygems_version: 2.7.6
91
91
  signing_key:
92
92
  specification_version: 4
93
93
  summary: Simple Dependency Injection.