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 declare a service as an alias for another
- Loading branch information
Kevin
committed
Aug 5, 2013
1 parent
5f9dea9
commit dd1989c
Showing
8 changed files
with
124 additions
and
15 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,19 @@ | ||
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__)), 'alias_definitions.yml')) | ||
|
||
class Mailer | ||
attr_accessor :transport | ||
|
||
def send_mail(message) | ||
puts "mail sent via #{self.transport}: #{message}" | ||
end | ||
end | ||
|
||
puts c.get('my.mailer').class | ||
# => Mailer | ||
puts c.get('aliased.mailer').class | ||
# => 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
parameters: | ||
mailer.transport: sendmail | ||
services: | ||
my.mailer: | ||
class: Mailer | ||
method_call: ['transport=', '%mailer.transport%'] | ||
aliased.mailer: | ||
alias: '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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
module DependencyInjection | ||
class AliasDefinition | ||
attr_accessor :alias_definition_name | ||
|
||
def initialize(alias_definition_name, container) | ||
@container = container | ||
self.alias_definition_name = alias_definition_name | ||
end | ||
|
||
def object | ||
@container.get(self.alias_definition_name) | ||
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
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/alias_definition' | ||
|
||
class TestAliasDefinition < Minitest::Test | ||
def setup | ||
@container = mock | ||
@alias_definition = DependencyInjection::AliasDefinition.new('my_definition', @container) | ||
end | ||
|
||
def test_getting_object_to_container | ||
@container.expects(:get).with('my_definition') | ||
|
||
@alias_definition.object | ||
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