f3e949903ca0d7c65ccb66232b4802faabdea165
[catagits/Catalyst-Runtime.git] / lib / Catalyst / Service / WithContext.pm
1 package Catalyst::Service::WithContext;
2 use Moose::Role;
3
4 # Why is the needed to be loaded?
5 use Bread::Board::Types;
6
7 with 'Bread::Board::Service';
8
9 has accept_context_sub => (
10     is => 'ro',
11     isa => 'Str',
12     default => 'ACCEPT_CONTEXT',
13 );
14
15 around 'get' => sub {
16     my ( $orig, $self, %params ) = @_;
17
18     my $context = delete $params{context};
19
20     my $instance = $self->$orig(%params);
21     my $ac_sub   = $self->accept_context_sub;
22
23     if ( $instance->can($ac_sub) ) {
24         return $instance->$ac_sub( @$context );
25     }
26
27     return $instance;
28 };
29
30 no Moose::Role;
31 1;
32
33 __END__
34
35 =pod
36
37 =head1 NAME
38
39 Catalyst::Service::WithContext
40
41 =head1 DESCRIPTION
42
43 =head1 METHODS
44
45 =over
46
47 =item B<accept_context_sub>
48
49 =item B<get>
50
51 =back
52
53 =head1 AUTHORS
54
55 Catalyst Contributors, see Catalyst.pm
56
57 =head1 COPYRIGHT
58
59 This library is free software. You can redistribute it and/or modify it under
60 the same terms as Perl itself.
61
62 =cut