Role/Handler.pm

filter: Statementstmt 100% 33 / 33 Branchbran 50.0% 1 / 2 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 8 / 8 Podpod 100% 1 / 1 totaltotal 97.7% 43 / 44 CC 2 SCAR 2.0
CC
2
Cov
97%
__ANON__
2.0
BEGIN
1.0
__ANON__
1.0
CRAP 1.2
« Role/Dispatcher.pm ↑ Summary Application.pm »
1 package Patterns::ChainOfResponsibility::Role::Handler;
2  
3 4 ▶use MooseX::Role::Parameterized;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 2 parameter 'handler_method' => (
6 isa => 'Str',
7 required => 1,
8 default => 'handle',
9 );
10  
11 2 parameter 'dispatcher' => (
12 isa => 'Str',
13 required => 1,
14 default => '::Provider',
15 );
16  
17 12 ▶role {
Subroutine __ANON__: called CC=2 SCAR=6.9
18 12 my $dispatcher = $_[0]->dispatcher;
19 12 ▶ if($dispatcher =~m/^::/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dispatcher =~ /^::/) 12 0 12
20 12 $dispatcher = "Patterns::ChainOfResponsibility".$dispatcher;
21 }
22 12 Class::MOP::load_class($dispatcher);
23  
24 12 has 'dispatcher' => (
25 is => 'bare',
26 does => 'Patterns::ChainOfResponsibility::Role::Dispatcher',
27 lazy_build => 1,
28 handles => ['dispatch'],
29 );
30  
31 9 ▶ method '_build_dispatcher', sub {
Subroutine _build_dispatcher: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
32 9 return $dispatcher->new;
33 12 };
34  
35 12 requires 'handle';
36  
37 has 'handlers' => (
38 traits => [qw(Array)],
39 is => 'rw',
40 isa => 'ArrayRef[Object]',
41 handles => {
42 all_handlers => 'elements',
43 no_handlers => 'is_empty',
44 },
45 12 ▶ default => sub {+[]},
Subroutine __ANON__: called CC=1 SCAR=0.0
46 12 );
47  
48 14 ▶ sub next_handlers {
Subroutine next_handlers: called CC=1 SCAR=0.0
Pod documented
49 14 my ($self, @handlers) = @_;
50 14 $self->handlers(\@handlers);
51 14 return $self;
52 }
53  
54 12 my $handler_method = $_[0]->handler_method;
55  
56 9 ▶ method 'process', sub {
Subroutine process: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
57 9 my ($self, @args) = @_;
58 9 my @return = $self->$handler_method(@args);
59 9 return $self->dispatch($self,\@return,\@args);
60  
61 12 };
62 2 };
63  
64 =head1 NAME
65
66 Patterns::ChainOfResponsibility::Role::Handler - A Link in a Chain of Responsibility
67  
68 =head1 SYNOPSIS
69  
70 package Loader::Filesystem;
71  
72 use Moose;
73 with 'MyApp::Loader';
74 with 'Patterns::ChainOfResponsibility::Role::Handler', {dispatcher => '::Provider'};
75  
76 sub handle {
77 my ($self, @args) = @_;
78 if($something_works) {
79 return @returned_info;
80 } else {
81 return;
82 }
83 }
84  
85 ...
86  
87 my $io_handle = Loader::Filesystem
88 ->new(%options)
89 ->next_handlers(@handlers)
90 ->process(@args);
91  
92
93 =head1 DESCRIPTION
94  
95 This is a role to be consumed by any class whose instances are links in a chain
96 of responsibility. Classes must define a L</handle> method.
97  
98 =head1 PARAMETERS
99  
100 This role defines the following parameters
101  
102 =head2 handler_method
103  
104 This is the name of the method in the target class which is handling args sent
105 to L</process>. By default its 'handle' but you can override as follows:
106  
107 with 'Patterns::ChainOfResponsibility::Role::Handler', {
108 handler_method => 'execute',
109 };
110  
111 This method should return processed args or nothing in the case of an error.
112  
113 =head2 dispatcher
114  
115 This is the dispatcher strategy to be used. This must be a class that consumes
116 the role L<Patterns::ChainOfResponsibility::Role::Dispatcher>. This distribution
117 includes three dispatchers which should cover the standard uses, but you can
118 write your own.
119  
120 =over 4
121  
122 =item ::Provider
123  
124 This is probably the most 'classic' dispatcher. Processes each handler until
125 one of them returns true. The first handler to 'handle' the job wins.
126  
127 with 'Patterns::ChainOfResponsibility::Role::Handler', {
128 dispatcher => '::Provider',
129 };
130  
131  
132 See L<Patterns::ChainOfResponsibility::Provider>
133  
134 =item ::Filter
135  
136 Similar to a UNIX commandline pipe. Each hander gets the return of the previous
137 until all have been handled (and the final filtered results are returned) or
138 when one handler returns nothing.
139  
140 with 'Patterns::ChainOfResponsibility::Role::Handler', {
141 dispatcher => '::Filter',
142 };
143  
144 See L<Patterns::ChainOfResponsibility::Filter>
145  
146 =item ::Broadcast
147  
148 Each handler gets the original args. All handers are processed in order until
149 the last handler is finished or one handler returns null. Good for something
150 like a logger where you have multiply and independent loggers
151  
152 with 'Patterns::ChainOfResponsibility::Role::Handler', {
153 dispatcher => '::Broadcast',
154 };
155  
156 See L<Patterns::ChainOfResponsibility::Broadcast>
157  
158 =back
159  
160 If you write a custom dispatcher, you should include the full namespace name in
161 the parameter, as in:
162  
163 with 'Patterns::ChainOfResponsibility::Role::Handler', {
164 dispatcher => 'MyApp::Dispatcher::MyCustomDispatcher',
165 };
166  
167 =head1 METHODS
168  
169 This role defines the following methods
170  
171 =head2 process ( @args)
172
173 The arguments you want one or more handler to process
174  
175 =head2 next_handlers (@handlers)
176  
177 Add more handlers to the end of the chain
178  
179 =head2 handle (@args)
180  
181 Gets @args sent to L</process>. Should return either processed arguments or a null
182 terminated 'return' (see L</SYNOPSIS>.
183  
184 =head1 AUTHOR
185  
186 John Napiorkowski C<< <jjnapiork@cpan.org> >>
187
188 =head1 LICENSE & COPYRIGHT
189
190 Copyright 2011, John Napiorkowski C<< <jjnapiork@cpan.org> >>
191  
192 This program is free software; you can redistribute it and/or modify
193 it under the same terms as Perl itself.
194
195 =cut
196  
197 2 1;
« Role/Dispatcher.pm ↑ Summary Application.pm »