lib/dependor/auto_injector.rb in dependor-0.0.6 vs lib/dependor/auto_injector.rb in dependor-1.0.0
- old
+ new
@@ -6,39 +6,35 @@
@instantiator = Instantiator.new(self)
@class_name_resolver = ClassNameResolver.new(search_modules)
end
def get(name)
- ensure_resolvable!(name)
+ return @injector.send(name)
+ rescue ArgumentError
+ return @injector.method(name).to_proc
+ rescue NameError
+ get_by_class_name(name)
+ end
- if has_method?(name)
- return @injector.send(name) if no_arguments?(name)
- return @injector.method(name).to_proc
- end
-
+ def get_by_class_name(name)
+ ensure_class_exists!(name)
klass = @class_name_resolver.for_name(name)
- @instantiator.instantiate(klass)
+ instantiator = @instantiator
+ @injector.define_singleton_method(name) do
+ instantiator.instantiate(klass)
+ end
+ @injector.send(name)
end
- def resolvable?(name)
- has_method?(name) || !!@class_name_resolver.for_name(name)
+ def class_exists?(name)
+ !!@class_name_resolver.for_name(name)
end
private
- def ensure_resolvable!(name)
- unless resolvable?(name)
+ def ensure_class_exists!(name)
+ unless class_exists?(name)
raise UnknownObject.new("Injector does not know how to create object: #{name}")
end
end
-
- def has_method?(name)
- @injector.methods.include?(name)
- end
-
- def no_arguments?(name)
- @injector.method(name).arity == 0
- end
-
end
-
end