lib/dependor/instantiator.rb in dependor-0.0.6 vs lib/dependor/instantiator.rb in dependor-1.0.0
- old
+ new
@@ -1,14 +1,24 @@
module Dependor
class Instantiator
def initialize(injector)
@injector = injector
+ @constructor_params = {}
end
def instantiate(klass)
- params = klass.instance_method(:initialize).parameters
- dependency_names = params.select{|type, name| type == :req}.map{|type, name| name}
- dependencies = dependency_names.map{|name| @injector.get(name)}
+ dependencies = dependecy_names_for(klass).map{|name| @injector.get(name)}
return klass.new(*dependencies)
+ end
+
+ private
+
+ def dependecy_names_for(klass)
+ @constructor_params[klass] ||= get_constructor_params(klass)
+ end
+
+ def get_constructor_params(klass)
+ params = klass.instance_method(:initialize).parameters
+ params.select{|type, name| type == :req}.map{|type, name| name}
end
end
end