Fix various issues and add improved test for dependency resolution
This commit is contained in:
@@ -8,9 +8,16 @@ module Laradi
|
|||||||
|
|
||||||
module Mixin
|
module Mixin
|
||||||
def dependencies(*deps)
|
def dependencies(*deps)
|
||||||
|
result = {}
|
||||||
deps.each { |dep|
|
deps.each { |dep|
|
||||||
include $laradi_injector[dep]
|
instance = $laradi_injector[dep]
|
||||||
|
if instance.nil?
|
||||||
|
raise NoDependencyError, "No dependency found for #{dep}"
|
||||||
|
end
|
||||||
|
result[dep] = instance
|
||||||
}
|
}
|
||||||
|
|
||||||
|
result
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
9
lib/laradi/exceptions.rb
Normal file
9
lib/laradi/exceptions.rb
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
module Laradi
|
||||||
|
|
||||||
|
module Exceptions
|
||||||
|
class NoDependencyError < Exception
|
||||||
|
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
||||||
@@ -2,7 +2,7 @@ class LogicUnitReceiver
|
|||||||
include Laradi::Mixin
|
include Laradi::Mixin
|
||||||
|
|
||||||
def get_unit
|
def get_unit
|
||||||
dependencies :logic_unit
|
dependencies(:logic_unit) => {logic_unit:}
|
||||||
|
|
||||||
logic_unit
|
logic_unit
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -14,7 +14,11 @@ class LaradiTest < ActiveSupport::TestCase
|
|||||||
test "It can inject dependencies" do
|
test "It can inject dependencies" do
|
||||||
lur = LogicUnitReceiver.new
|
lur = LogicUnitReceiver.new
|
||||||
|
|
||||||
assert_not_nil lur.get_unit
|
result = lur.get_unit
|
||||||
|
|
||||||
|
assert_not_nil result
|
||||||
|
|
||||||
|
assert_kind_of(LogicUnit, result)
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user