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 a yaml configurator to register services
- Loading branch information
Kevin
committed
Aug 5, 2013
1 parent
988235d
commit e54e4fd
Showing
7 changed files
with
166 additions
and
2 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 |
---|---|---|
@@ -1,5 +1,6 @@ | ||
require 'rake/testtask' | ||
|
||
Rake::TestTask.new do |test| | ||
test.libs << 'test' | ||
test.pattern = 'test/**/test_*.rb' | ||
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,11 @@ | ||
parameters: | ||
mailer.transport: sendmail | ||
services: | ||
my.mailer: | ||
class: Mailer | ||
calls: | ||
- ['transport=', '%mailer.transport%'] | ||
newsletter: | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
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__)), 'services.yml')) | ||
|
||
class NewsletterManager | ||
def initialize(mailer) | ||
@mailer = mailer | ||
end | ||
|
||
def send_mail(message) | ||
puts 'newletter' | ||
@mailer.send_mail(message) | ||
end | ||
end | ||
|
||
class Mailer | ||
attr_accessor :transport | ||
|
||
def send_mail(message) | ||
puts "mail sent via #{self.transport}: #{message}" | ||
end | ||
end | ||
|
||
c.get('newsletter').send_mail('Hello World') | ||
# => newletter | ||
# mail sent via sendmail: 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,35 @@ | ||
require 'yaml' | ||
|
||
module DependencyInjection | ||
module Loaders | ||
class Yaml | ||
def initialize(container) | ||
@container = container | ||
end | ||
|
||
def load(filename) | ||
file = YAML::load_file(filename) | ||
add_parameters(file['parameters']) if file['parameters'] | ||
add_services(file['services']) if file['services'] | ||
end | ||
|
||
private | ||
|
||
def add_parameters(parameters) | ||
parameters.each { |name, value| @container.add_parameter(name, value) } | ||
end | ||
|
||
def add_services(services) | ||
services.each { |name, parameters| add_service(name, parameters) } | ||
end | ||
|
||
def add_service(name, parameters) | ||
definition = @container.register(name, parameters['class']) | ||
definition.add_arguments(*parameters['arguments']) if parameters['arguments'] | ||
if parameters['calls'] | ||
parameters['calls'].each { |method_name, arguments| definition.add_method_call(method_name, *arguments) } | ||
end | ||
end | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
require 'test_helper' | ||
require 'dependency_injection/loaders/yaml' | ||
|
||
class TestYaml < Minitest::Test | ||
def setup | ||
@container = mock | ||
@yaml_loader = DependencyInjection::Loaders::Yaml.new(@container) | ||
end | ||
|
||
def test_loading_file_without_parameters | ||
YAML.stubs(:load_file).with('services.yml').returns({ 'services' => [] }) | ||
@yaml_loader.expects(:add_parameters).never | ||
|
||
@yaml_loader.load('services.yml') | ||
end | ||
|
||
def test_loading_file_with_parameters | ||
YAML.stubs(:load_file).with('services.yml').returns({'parameters' => { 'key_1' => 'value_1' }, 'services' => [] }) | ||
@yaml_loader.expects(:add_parameters).with({ 'key_1' => 'value_1' }) | ||
|
||
@yaml_loader.load('services.yml') | ||
end | ||
|
||
def test_loading_file_without_services | ||
YAML.stubs(:load_file).with('services.yml').returns({ 'parameters' => [] }) | ||
@yaml_loader.expects(:add_services).never | ||
|
||
@yaml_loader.load('services.yml') | ||
end | ||
|
||
def test_loading_file_with_services | ||
YAML.stubs(:load_file).with('services.yml').returns({ 'parameters' => {}, 'services' => { 'service_1' => { 'class' => 'MyKlass' }}}) | ||
@yaml_loader.expects(:add_services).with({ 'service_1' => { 'class' => 'MyKlass' }}) | ||
|
||
@yaml_loader.load('services.yml') | ||
end | ||
|
||
def test_adding_parameters | ||
@container.expects(:add_parameter).with('key_1', 'value_1') | ||
@container.expects(:add_parameter).with('key_2', 'value_2') | ||
|
||
@yaml_loader.send(:add_parameters, { 'key_1' => 'value_1', 'key_2' => 'value_2' }) | ||
end | ||
|
||
def test_adding_services | ||
@yaml_loader.expects(:add_service).with('key_1', { 'param_1' => 'value_1', 'param_2' => 'value_2' }) | ||
@yaml_loader.expects(:add_service).with('key_2', { 'param_1' => 'value_1' }) | ||
|
||
@yaml_loader.send(:add_services, { 'key_1' => { 'param_1' => 'value_1', 'param_2' => 'value_2' }, | ||
'key_2' => { 'param_1' => 'value_1' }}) | ||
end | ||
|
||
def test_adding_service_without_parameters | ||
definition = mock | ||
@container.stubs(:register).with('key_1', 'MyKlass').returns(definition) | ||
definition.expects(:add_arguments).never | ||
|
||
@yaml_loader.send(:add_service, 'key_1', { 'class' => 'MyKlass' }) | ||
end | ||
|
||
def test_adding_service_with_parameters | ||
definition = mock | ||
@container.stubs(:register).with('key_1', 'MyKlass').returns(definition) | ||
definition.expects(:add_arguments).with('arg_1', 'arg_2') | ||
|
||
@yaml_loader.send(:add_service, 'key_1', { 'class' => 'MyKlass', 'arguments' => ['arg_1', 'arg_2'] }) | ||
end | ||
|
||
def test_adding_service_without_method_calls | ||
definition = mock | ||
@container.stubs(:register).with('key_1', 'MyKlass').returns(definition) | ||
definition.expects(:add_method_call).never | ||
|
||
@yaml_loader.send(:add_service, 'key_1', { 'class' => 'MyKlass' }) | ||
end | ||
|
||
def test_adding_service_with_method_calls | ||
definition = mock | ||
@container.stubs(:register).with('key_1', 'MyKlass').returns(definition) | ||
definition.expects(:add_method_call).with('method_1', 'arg_1') | ||
definition.expects(:add_method_call).with('method_2', 'arg_1', 'arg_2') | ||
definition.expects(:add_method_call).with('method_3', %w(arg_1 arg_2)) | ||
|
||
@yaml_loader.send(:add_service, 'key_1', { 'class' => 'MyKlass', 'calls' => { 'method_1' => ['arg_1'], | ||
'method_2' => ['arg_1', 'arg_2'], | ||
'method_3' => [['arg_1', 'arg_2']] }}) | ||
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