File Coverage

blib/lib/MooseX/IOC.pm
Criterion Covered Total %
statement 1 3 33.3
branch n/a
condition n/a
subroutine 1 1 100.0
pod n/a
total 2 4 50.0


line stmt bran cond sub pod time code
1              
2             package MooseX::IOC;
3 2     2   89540 use Moose;
  0            
  0            
4              
5             use MooseX::IOC::Meta::Attribute;
6              
7             our $VERSION = '0.03';
8              
9             1;
10              
11             __END__
12              
13             =pod
14              
15             =head1 NAME
16              
17             MooseX::IOC - Moose attributes with IOC integration
18              
19             =head1 SYNOPSIS
20              
21             # in a startup script somewhere ...
22              
23             use IOC;
24             use IOC::Service::Parameterized;
25             use IOC::Registry;
26             use MooseX::IOC;
27              
28             {
29             my $container = IOC::Container->new('MyProject');
30             $container->register(IOC::Service::Literal->new('log_file' => "logfile.log"));
31             $container->register(IOC::Service->new('FileLogger' => sub {
32             my $c = shift;
33             return FileLogger->new($c->get('log_file'));
34             }));
35              
36             my $reg = IOC::Registry->new;
37             $reg->registerContainer($container);
38             }
39              
40             # in a .pm file somewhere ...
41              
42             package MyApplication;
43             use Moose;
44              
45             has 'logger' => (
46             metaclass => 'IOC',
47             is => 'ro',
48             isa => 'FileLogger',
49             service => '/MyProject/FileLogger',
50             );
51              
52             # in a script file somewhere ...
53              
54             my $app = MyApplication->new;
55             $app->logger; # automatically gotten from IOC
56              
57             =head1 DESCRIPTION
58              
59             This module provides a bridge between IOC registries and Moose objects through a
60             custom attribute metaclass. It compliments the C<default> option with a C<service>
61             option which contains a L<IOC::Registry> path (and optional parameters).
62              
63             The C<service> option can be in one of the following formats:
64              
65             =over 4
66              
67             =item I<IOC::Registry path string>
68              
69             This is the simplest version available, it is simply a path string which
70             can be passed to L<IOC::Registry>'s C<locateService> method.
71              
72             =item I<IOC::Registry path string and parameters>
73              
74             This version is for use with L<IOC::Service::Parameterized> services, and
75             allows you to pass additional parameters to the C<locateService> method.
76             It looks like this:
77              
78             has 'logger' => (
79             metaclass => 'IOC',
80             is => 'ro',
81             isa => 'FileLogger',
82             service => [ '/MyProject/FileLogger' => (log_file => 'foo.log') ],
83             );
84              
85             =item I<CODE reference>
86              
87             The last version is the most flexible, it is CODE reference which is
88             expected to return an ARRAY ref similar to the above version.
89              
90             has 'logger' => (
91             metaclass => 'IOC',
92             is => 'ro',
93             isa => 'FileLogger',
94             lazy => 1,
95             service => sub {
96             my $self = shift;
97             [ '/MyProject/FileLogger' => (
98             log_file => $self->log_file
99             ) ]
100             },
101             );
102              
103             =back
104              
105             If the C<service> is not found and a C<default> option has been set, then
106             it will return the value in C<default>. This can be useful for writing
107             code which can potentially be run both under IOC and not under IOC.
108              
109             =head1 METHODS
110              
111             =over 4
112              
113             =item B<meta>
114              
115             =back
116              
117             =head1 SEE ALSO
118              
119             =over 4
120              
121             =item L<IOC>
122              
123             =item L<IOC::Registry>
124              
125             =item L<Moose>
126              
127             =back
128              
129             =head1 BUGS
130              
131             All complex software has bugs lurking in it, and this module is no
132             exception. If you find a bug please either email me, or add the bug
133             to cpan-RT.
134              
135             =head1 AUTHOR
136              
137             Stevan Little E<lt>stevan@iinteractive.comE<gt>
138              
139             =head1 COPYRIGHT AND LICENSE
140              
141             Copyright 2007-2009 by Infinity Interactive, Inc.
142              
143             L<http://www.iinteractive.com>
144              
145             This library is free software; you can redistribute it and/or modify
146             it under the same terms as Perl itself.
147              
148             =cut