make_single_default sub
[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 );
13
14 sub get_component {
15     my ( $self, $name, @args ) = @_;
16
17     return $self->resolve(
18         service    => $name,
19         parameters => { accept_context_args => \@args },
20     );
21 }
22
23 sub get_component_regexp {
24     my ( $self, $query, $c, @args ) = @_;
25
26     my @result = map {
27         $self->get_component( $_, $c, @args )
28     } grep { m/$query/ } $self->get_service_list;
29
30     return @result;
31 }
32
33 # FIXME: not the best name for a sub
34 sub make_single_default {
35     my ( $self ) = @_;
36
37     my @complist = $self->get_service_list;
38
39     $self->default_component( shift @complist )
40         if !$self->default_component && scalar @complist == 1;
41 }
42
43 1;
44
45 __END__
46
47 =pod
48
49 =head1 NAME
50
51 Catalyst::IOC::SubContainer - Container for models, controllers and views
52
53 =head1 METHODS
54
55 =head2 get_component
56
57 =head2 get_component_regexp
58
59 =head2 make_single_default
60
61 =head1 AUTHORS
62
63 Catalyst Contributors, see Catalyst.pm
64
65 =head1 COPYRIGHT
66
67 This library is free software. You can redistribute it and/or modify it under
68 the same terms as Perl itself.
69
70 =cut