Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

make injection within a namespace smarter

  • Loading branch information...
commit 5bbc515dd13f23c380f9e9fae406b2cc12128ff4 1 parent d59cfcb
James Golick jamesgolick authored
25 lib/objectify/injector.rb
View
@@ -14,20 +14,21 @@ def initialize(config)
def call(object, method)
payload = {:object => object, :method => method}
instrument("inject.objectify", payload) do |payload|
+ namespace = object.respond_to?(:name) && object.name ? object.name.underscore.split("/")[0...-1].join("/") : nil
method_obj = method_object(object, method)
injectables = method_obj.parameters.map do |reqd, name|
- @decoration_context[name] || @config.get(name) if reqd == :req
+ @decoration_context[[namespace, name].join("/").to_sym] || @decoration_context[name] || @config.get(name) if reqd == :req
end.compact
arguments = injectables.map do |type, value|
if type == :unknown
- type, value = unknown_value_to_injectable(value)
+ type, value = unknown_value_to_injectable(namespace, value)
end
if type == :resolver
- resolver_klass = [value, :resolver].join("_").classify.constantize
+ resolver_klass = [value, :resolver].join("_").camelize.constantize
call(call(resolver_klass, :new), :call)
elsif type == :implementation
- implementation_klass = value.to_s.classify.constantize
+ implementation_klass = value.to_s.camelize.constantize
call(implementation_klass, :new)
elsif type == :value
value
@@ -45,7 +46,7 @@ def call(object, method)
base_name = object.name.underscore.to_sym
add_decoration_context(base_name, result)
result = @config.decorators(base_name).inject(result) do |memo, decorator|
- call(decorator.to_s.classify.constantize, :new).tap do |decorated|
+ call(decorator.to_s.camelize.constantize, :new).tap do |decorated|
add_decoration_context(base_name, decorated)
end
end
@@ -65,12 +66,14 @@ def method_object(object, method)
end
end
- def unknown_value_to_injectable(value)
- [nil, :resolver].each do |suffix|
- begin
- [value, suffix].compact.join("_").classify.constantize
- return [suffix.nil? ? :implementation : suffix, value]
- rescue NameError => e
+ def unknown_value_to_injectable(namespace, value)
+ [namespace, nil].uniq.each do |ns|
+ [nil, :resolver].each do |suffix|
+ begin
+ [ns, [value, suffix].compact.join("_")].compact.join("/").camelize.constantize
+ return [suffix.nil? ? :implementation : suffix, [ns, value].compact.join("/")]
+ rescue NameError => e
+ end
end
end
2  objectify.gemspec
View
@@ -25,6 +25,6 @@ Gem::Specification.new do |s|
s.add_development_dependency "bourne", "1.0"
s.add_development_dependency "mocha", "0.9.8"
- s.add_runtime_dependency "rails", ">=3.0.0"
+ s.add_runtime_dependency "rails", ">=3.2.5"
s.add_runtime_dependency "i18n"
end
51 spec/injector_spec.rb
View
@@ -135,4 +135,55 @@ def initialize(to_be_decorated)
@injector.call(ToBeDecorated, :new).should be_instance_of(ToBeDecorated)
end
end
+
+ module An
+ class Service
+ attr_reader :canada
+
+ def initialize(canada)
+ @canada = canada
+ end
+ end
+
+ class ServiceWithStuff
+ attr_reader :service
+
+ def initialize(service)
+ @service = service
+ end
+ end
+
+ class ServiceWithOtherStuff
+ def initialize(service, my_injected_class)
+ @service = service
+ @my_injected_class = my_injected_class
+ end
+ end
+
+ class Canada
+ end
+ end
+
+ context "within a namespace" do
+ before do
+ @config.stubs(:get).with(:canada).returns([:unknown, :canada])
+ @result = @injector.call(An::Service, :new)
+ end
+
+ it "first searches within the namespace to fulfill the dependency" do
+ @result.canada.should be_instance_of(An::Canada)
+ end
+
+ it "can decorate" do
+ @config.stubs(:decorators).with(:"an/service").returns([:"an/service_with_stuff"])
+ @injector.call(An::Service, :new).should be_instance_of(An::ServiceWithStuff)
+ end
+
+ it "can still look up the module chain for decorators" do
+ @config.stubs(:decorators).with(:"an/service").returns([:"an/service_with_other_stuff"])
+ @config.stubs(:get).with(:my_injected_class).returns([:unknown, :my_injected_class])
+ @config.stubs(:get).with(:some_dependency).returns([:value, :asdf])
+ @injector.call(An::Service, :new).should be_instance_of(An::ServiceWithOtherStuff)
+ end
+ end
end
Please sign in to comment.
Something went wrong with that request. Please try again.