MooseX::IOC - Moose attributes with IOC integration
# in a startup script somewhere ... use IOC; use IOC::Service::Parameterized; use IOC::Registry; use MooseX::IOC; { my $container = IOC::Container->new('MyProject'); $container->register(IOC::Service::Literal->new('log_file' => "logfile.log")); $container->register(IOC::Service->new('FileLogger' => sub { my $c = shift; return FileLogger->new($c->get('log_file')); })); my $reg = IOC::Registry->new; $reg->registerContainer($container); } # in a .pm file somewhere ... package MyApplication; use Moose; has 'logger' => ( metaclass => 'IOC', is => 'ro', isa => 'FileLogger', service => '/MyProject/FileLogger', ); # in a script file somewhere ... my $app = MyApplication->new; $app->logger; # automatically gotten from IOC
This module provides a bridge between IOC registries and Moose objects through a custom attribute metaclass. It compliments the default option with a service option which contains a IOC::Registry path (and optional parameters).
default
service
The service option can be in one of the following formats:
This is the simplest version available, it is simply a path string which can be passed to IOC::Registry's locateService method.
locateService
This version is for use with IOC::Service::Parameterized services, and allows you to pass additional parameters to the locateService method. It looks like this:
has 'logger' => ( metaclass => 'IOC', is => 'ro', isa => 'FileLogger', service => [ '/MyProject/FileLogger' => (log_file => 'foo.log') ], );
The last version is the most flexible, it is CODE reference which is expected to return an ARRAY ref similar to the above version.
has 'logger' => ( metaclass => 'IOC', is => 'ro', isa => 'FileLogger', lazy => 1, service => sub { my $self = shift; [ '/MyProject/FileLogger' => ( log_file => $self->log_file ) ] }, );
If the service is not found and a default option has been set, then it will return the value in default. This can be useful for writing code which can potentially be run both under IOC and not under IOC.
All complex software has bugs lurking in it, and this module is no exception. If you find a bug please either email me, or add the bug to cpan-RT.
Stevan Little <stevan@iinteractive.com>
Copyright 2007-2009 by Infinity Interactive, Inc.
http://www.iinteractive.com
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
To install MooseX::IOC, copy and paste the appropriate command in to your terminal.
cpanm
cpanm MooseX::IOC
CPAN shell
perl -MCPAN -e shell install MooseX::IOC
For more information on module installation, please visit the detailed CPAN module installation guide.