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