This repository has been archived by the owner on Apr 1, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add the ability to lazy load a service #4
- Loading branch information
Kevin
committed
Aug 5, 2013
1 parent
2325ac0
commit fc0dc88
Showing
10 changed files
with
128 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
require 'dependency_injection/container' | ||
require 'dependency_injection/loaders/yaml' | ||
|
||
c = DependencyInjection::Container.new | ||
loader = DependencyInjection::Loaders::Yaml.new(c) | ||
loader.load(File.join(File.dirname(File.expand_path(__FILE__)), 'lazy_load_object.yml')) | ||
|
||
class NewsletterManager | ||
def initialize(mailer) | ||
@mailer = mailer | ||
end | ||
|
||
def send_mail(message) | ||
puts 'newletter' | ||
@mailer.send_mail(message) | ||
end | ||
end | ||
|
||
class Mailer | ||
def initialize | ||
puts 'mailer loaded' | ||
end | ||
|
||
def send_mail(message) | ||
puts "mail sent via: #{message}" | ||
end | ||
end | ||
|
||
puts c.get('my.mailer').class | ||
# => ProxyObject | ||
puts c.get('my.newsletter_manager').class | ||
# => NewsletterManager | ||
|
||
c.get('my.newsletter_manager').send_mail('Hello World') | ||
# => newsletter | ||
# mailer loaded | ||
# mail sent : Hello World |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
services: | ||
my.mailer: | ||
class: Mailer | ||
lazy: true | ||
my.newsletter_manager: | ||
class: NewsletterManager | ||
arguments: | ||
- '@my.mailer' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
require 'dependency_injection/definition' | ||
require 'dependency_injection/proxy_object' | ||
|
||
module DependencyInjection | ||
class LazyDefinition < Definition | ||
def object | ||
return @proxy_object if @proxy_object | ||
|
||
@proxy_object = ProxyObject.new { super } | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
module DependencyInjection | ||
class ProxyObject | ||
def initialize(&block) | ||
@object = block | ||
end | ||
|
||
def method_missing(method_name, *args) | ||
@object.call.send(method_name, *args) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
require 'test_helper' | ||
require 'dependency_injection/lazy_definition' | ||
|
||
class TestLazyDefinition < Minitest::Test | ||
def setup | ||
@container = mock | ||
@lazy_definition = DependencyInjection::LazyDefinition.new('MyKlass', @container) | ||
end | ||
|
||
def test_getting_object_returns_a_proxy_object | ||
assert_equal(DependencyInjection::ProxyObject, @lazy_definition.object.class) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
require 'test_helper' | ||
require 'dependency_injection/proxy_object' | ||
|
||
class TestProxyObject < Minitest::Test | ||
def setup | ||
@object = mock | ||
@proxy_object = DependencyInjection::ProxyObject.new { @object } | ||
end | ||
|
||
def test_calling_a_method_existing_on_object | ||
@object.expects(:existing_method).with('arg_1', 'arg_2') | ||
|
||
@proxy_object.existing_method('arg_1', 'arg_2') | ||
end | ||
end |
Just noticed that there's a missing white space here.
And don't you need to align these two assignations?