created default_component writer
[catagits/Catalyst-Runtime.git] / lib / Catalyst / IOC / SubContainer.pm
1 package Catalyst::IOC::SubContainer;
2 use Bread::Board;
3 use Moose;
4 use Catalyst::IOC::BlockInjection;
5
6 extends 'Bread::Board::Container';
7
8 has default_component => (
9     isa => 'Str|Undef',
10     is  => 'ro',
11     required => 0,
12     writer   => '_set_default_component',
13 );
14
15 sub get_component {
16     my ( $self, $name, @args ) = @_;
17
18     return $self->resolve(
19         service    => $name,
20         parameters => { accept_context_args => \@args },
21     );
22 }
23
24 sub get_component_regexp {
25     my ( $self, $query, $c, @args ) = @_;
26
27     my @result = map {
28         $self->get_component( $_, $c, @args )
29     } grep { m/$query/ } $self->get_service_list;
30
31     return @result;
32 }
33
34 # FIXME: not the best name for a sub
35 sub make_single_default {
36     my ( $self ) = @_;
37
38     my @complist = $self->get_service_list;
39
40     $self->_set_default_component( shift @complist )
41         if !$self->default_component && scalar @complist == 1;
42 }
43
44 1;
45
46 __END__
47
48 =pod
49
50 =head1 NAME
51
52 Catalyst::IOC::SubContainer - Container for models, controllers and views
53
54 =head1 METHODS
55
56 =head2 get_component
57
58 =head2 get_component_regexp
59
60 =head2 make_single_default
61
62 =head1 AUTHORS
63
64 Catalyst Contributors, see Catalyst.pm
65
66 =head1 COPYRIGHT
67
68 This library is free software. You can redistribute it and/or modify it under
69 the same terms as Perl itself.
70
71 =cut