File Coverage

blib/lib/Pipeline/Dispatch.pm
Criterion Covered Total %
statement 72 75 96.0
branch 8 8 100.0
condition 10 12 83.3
subroutine 15 16 93.7
pod 8 11 72.7
total 113 122 92.6


line stmt bran cond sub pod time code
1             package Pipeline::Dispatch;
2              
3 11     11   358321 use strict;
  11         20  
  11         489  
4 11     11   60 use warnings;
  11         19  
  11         370  
5              
6 11     11   1083 use Pipeline;
  11         21  
  11         363  
7 11     11   66 use Pipeline::Base;
  11         27  
  11         258  
8 11     11   58 use base qw( Pipeline::Base );
  11         18  
  11         8752  
9              
10             our $VERSION = "3.12";
11              
12             sub segments {
13 135     135 1 164 my $self = shift;
14 135         152 my $list = shift;
15 135 100       786 if (defined( $list )) {
16 43         133 $self->{ segments } = $list;
17 43         140 return $self;
18             } else {
19 92   100     259 $self->{ segments } ||= [];
20 92         368 return $self->{ segments };
21             }
22             }
23              
24             sub dispatched_segments {
25 57     57 1 160 my $self = shift;
26 57         65 my $list = shift;
27 57 100       121 if (defined( $list )) {
28 17         37 $self->{ dispatched_segments } = $list;
29 17         45 return $self;
30             } else {
31 40   100     184 $self->{ dispatched_segments } ||= [];
32 40         133 return $self->{ dispatched_segments };
33             }
34             }
35              
36             sub get {
37 0     0 0 0 my $self = shift;
38 0         0 my $idx = shift;
39 0         0 return $self->segments->[ $idx ];
40             }
41              
42             sub add {
43 28     28 1 45 my $self = shift;
44              
45 28         53 return $self if push(
46 33         346 @{$self->segments},
47 28 100       39 grep { $_->isa('Pipeline::Segment') } @_
48             ) == @_;
49             }
50              
51             sub delete {
52 2     2 1 10 my $self = shift;
53 2         3 my $idx = shift;
54 2         4 splice(@{$self->segments},$idx,1);
  2         6  
55 2         7 $self;
56             }
57              
58             sub get_next_segment {
59 23     23 0 34 my $self = shift;
60 23         28 my $pipe = shift;
61 23         29 my $segment = shift @{$self->segments};
  23         47  
62 23         48 return $segment;
63             }
64              
65             sub dispatch_a_segment {
66 23     23 0 33 my $self = shift;
67 23         30 my $seg = shift;
68 23   66     7146 my $meth = $seg->dispatch_method || $self->dispatch_method;
69              
70 23         236 $self->emit("dispatching to " . ref($seg));
71              
72 23         83 $seg->parent->start_dispatch();
73              
74 23         174 my @results = $seg->$meth( $seg->parent );
75              
76 23         5946 $seg->parent->end_dispatch();
77              
78 23         105 return @results;
79             }
80              
81             sub next {
82 23     23 1 36 my $self = shift;
83 23   66     82 my $pipe = shift || Pipeline->new();
84              
85 23         74 my $segment = $self->get_next_segment( $pipe );
86              
87 23         349 $segment->prepare_dispatch( $pipe );
88 23         80 my @results = $self->dispatch_a_segment( $segment );
89 23         174 $segment->cleanup_dispatch( $pipe );
90              
91 23         28 push @{$self->dispatched_segments}, $segment;
  23         63  
92              
93 23         128 return @results;
94             }
95              
96             sub dispatch_method {
97 24     24 1 165 my $self = shift;
98 24         36 my $text = shift;
99 24 100       98 if (defined( $text )) {
100 1         3 $self->{ dispatch_method } = $text;
101 1         5 return $self;
102             } else {
103 23   100     169 $self->{ dispatch_method } ||= 'dispatch';
104 23         97 return $self->{ dispatch_method };
105             }
106             }
107              
108             sub segment_available {
109 33     33 1 60 my $self = shift;
110 33         79 !!$self->segments->[0]
111             }
112              
113             sub reset {
114 17     17 1 37 my $self = shift;
115 17         73 $self->segments( $self->dispatched_segments );
116 17         48 $self->dispatched_segments( [] );
117             }
118              
119              
120             1;
121              
122             =head1 NAME
123              
124             Pipeline::Dispatch - dispatcher for pipeline segments
125              
126             =head1 SYNOPSIS
127              
128             use Pipeline::Dispatch;
129             my $dispatcher = Pipeline::Dispatch->new();
130             $dispatcher->segments();
131             $dispatcher->add( Pipeline::Segment->new() );
132             $dispatcher->delete( 0 );
133             $dispatcher->segment_available && $dispatcher->next()
134             my $method = $dispatcher->dispatch_method();
135              
136             =head1 DESCRIPTION
137              
138             C simply accepts pipeline segments and does very little
139             with them. It can dispatch segments in order, one by one. It is also capable
140             of altering the way in which it dispatches to each segment, both on a pipeline
141             basis, and on a segment-by-segment basis.
142              
143             =head1 CONSTRUCTOR
144              
145             =over 4
146              
147             =item new()
148              
149             The C constructor simply returns a new dispatcher object.
150              
151             =back
152              
153             =head1 METHODS
154              
155             =over 4
156              
157             =item segments( [ARRAYREF] )
158              
159             The C method returns the dispatchers list of remaining segments as an
160             array reference. Optionally the ARRAYREF argument can be given to the C
161             method, which will set the list.
162              
163             =item add( LIST )
164              
165             The C method adds one or more segments to the dispatchers segment list.
166              
167             =item delete( INTEGER )
168              
169             The C method removes the segment at index INTEGER from the list of
170             segments.
171              
172             =item segment_available()
173              
174             The C method returns true or false, depending on whether or
175             not there is a segment available to dispatch to.
176              
177             =item next( [ Pipeline ] )
178              
179             The C method dispatches the next segment in the segment list. It optionally
180             takes a Pipeline object that is handed down to the segment.
181              
182             =item dispatch_method( [STRING] )
183              
184             The C method gets and sets the method name to call globally on
185             each segment for dispatch. Individual segments can override this if they set
186             dispatch_method themselves.
187              
188             =item dispatched_segments( [ARRAYREF] )
189              
190             The C method gets and sets the list of segments that
191             have already been dispatched. Used by the C method, and probably
192             should not be called by the user..
193              
194             =item reset()
195              
196             puts the dispatcher back into an undispatched state - all the segments
197             are available for dispatch again.
198              
199             =back
200              
201             =head1 SEE ALSO
202              
203             Pipeline::Segment Pipeline
204              
205             =head1 AUTHOR
206              
207             James A. Duncan
208              
209             =head1 COPYRIGHT
210              
211             Copyright 2003 Fotango Ltd. All Rights Reserved.
212              
213             This software is released under the same terms as Perl itself.
214              
215             http://opensource.fotango.com
216              
217             =cut
218              
219              
220              
221              
222              
223             =cut