File Coverage

lib/DataFlow/ProcWrapper.pm
Criterion Covered Total %
statement 7 9 77.7
branch n/a
condition n/a
subroutine 3 3 100.0
pod n/a
total 10 12 83.3


line stmt bran cond sub pod time code
1             package DataFlow::ProcWrapper;
2              
3 1     1   2076 use strict;
  1         11  
  1         49  
4 1     1   7 use warnings;
  1         2  
  1         60  
5              
6             # ABSTRACT: Wrapper around a processor
7              
8             our $VERSION = '1.121830'; # VERSION
9              
10 1     1   468 use Moose;
  0            
  0            
11             with 'DataFlow::Role::Processor';
12              
13             use Moose::Autobox;
14             use namespace::autoclean;
15              
16             use DataFlow::Item;
17             use DataFlow::Types qw(Processor);
18              
19             has 'input_chan' => (
20             is => 'ro',
21             isa => 'Str',
22             lazy => 1,
23             default => 'default',
24             );
25              
26             has 'output_chan' => (
27             is => 'ro',
28             isa => 'Str',
29             lazy => 1,
30             default => sub { shift->input_chan },
31             );
32              
33             has 'on_proc' => (
34             is => 'ro',
35             isa => 'Processor',
36             required => 1,
37             init_arg => 'wraps',
38             coerce => 1,
39             );
40              
41             sub _itemize_response {
42             my ( $self, $input_item, @response ) = @_;
43             return ($input_item) unless @response;
44             return @{
45             @response->map(
46             sub { $input_item->clone->set_data( $self->output_chan, $_ ) }
47             )
48             };
49             }
50              
51             sub process {
52             my ( $self, $item ) = @_;
53              
54             return unless defined $item;
55             if ( ref($item) eq 'DataFlow::Item' ) {
56             my $data = $item->get_data( $self->input_chan );
57             return ($item) unless $data;
58             return $self->_itemize_response( $item,
59             $self->on_proc->process($data) );
60             }
61             else {
62             my $data = $item;
63             my $empty_item = DataFlow::Item->new();
64             return ($empty_item) unless $data;
65             return $self->_itemize_response( $empty_item,
66             $self->on_proc->process($data) );
67             }
68             }
69              
70             __PACKAGE__->meta->make_immutable;
71              
72             1;
73              
74              
75              
76             =pod
77              
78             =encoding utf-8
79              
80             =head1 NAME
81              
82             DataFlow::ProcWrapper - Wrapper around a processor
83              
84             =head1 VERSION
85              
86             version 1.121830
87              
88             =head1 SYNOPSIS
89              
90             use DataFlow::ProcWrapper;
91             use DataFlow::Item;
92              
93             my $wrapper = DataFlow::ProcWrapper->new( wraps => sub { lc } );
94             my $item = DataFlow::Item->itemize( 'default', 'WAKAWAKAWAKA' );
95             my @result = $wrapper->process($item);
96             # $result[0]->get_data('default') equals to 'wakawakawaka'
97              
98             =head1 DESCRIPTION
99              
100             This class C<DataFlow::ProcWrapper> consumes the L<DataFlow::Role::Processor>
101             role, but this is not a "common" processor and it should not be used as such.
102             Actually, it is supposed to be used internally by DataFlow alone, so in theory,
103             if not in practice, we should be able to ignore its existence.
104              
105             C<ProcWrapper> will, as the name suggests, wraps around a processor (read
106             a Proc, a DataFlow, a naked sub or a named processor), and provides a layer
107             of control on the input and output channels.
108              
109             =head1 ATTRIBUTES
110              
111             =head2 input_chan
112              
113             Name of the input channel. The L<DataFlow::Item> may carry data in distinct
114             "channels", and here we can select which channel we will take the data from.
115             If not specified, it will default to the literal string C<< 'default' >>.
116              
117             =head2 output_chan
118              
119             Similarly, the output channel's name. If not specified, it will default to
120             the same channel used for input.
121              
122             =head1 METHODS
123              
124             =head2 process
125              
126             This works like the regular C<process()> method in a processor, except that
127             it expects to receive an object of the type L<DataFlow::Item>.
128              
129             Additionaly, one can pass a random scalar as argument, and add a
130             second argument that evaluates to a true value, and the scalar argument will
131             be automagically "boxed" into a C<DataFlow::Item> object.
132              
133             Once the data is within a C<DataFlow::Item>, data will be pulled from the
134             specified channel, will call the wrapped processor's C<process()> method.
135              
136             It will always return an array with one or more elements, all of them of the
137             C<DataFlow::Item> type.
138              
139             =head1 SEE ALSO
140              
141             Please see those modules/websites for more information related to this module.
142              
143             =over 4
144              
145             =item *
146              
147             L<DataFlow|DataFlow>
148              
149             =back
150              
151             =head1 AUTHOR
152              
153             Alexei Znamensky <russoz@cpan.org>
154              
155             =head1 COPYRIGHT AND LICENSE
156              
157             This software is copyright (c) 2011 by Alexei Znamensky.
158              
159             This is free software; you can redistribute it and/or modify it under
160             the same terms as the Perl 5 programming language system itself.
161              
162             =head1 BUGS AND LIMITATIONS
163              
164             You can make new bug reports, and view existing ones, through the
165             web interface at L<http://rt.cpan.org>.
166              
167             =head1 DISCLAIMER OF WARRANTY
168              
169             BECAUSE THIS SOFTWARE IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
170             FOR THE SOFTWARE, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT
171             WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER
172             PARTIES PROVIDE THE SOFTWARE "AS IS" WITHOUT WARRANTY OF ANY KIND,
173             EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE
174             IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
175             PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE
176             SOFTWARE IS WITH YOU. SHOULD THE SOFTWARE PROVE DEFECTIVE, YOU ASSUME
177             THE COST OF ALL NECESSARY SERVICING, REPAIR, OR CORRECTION.
178              
179             IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
180             WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
181             REDISTRIBUTE THE SOFTWARE AS PERMITTED BY THE ABOVE LICENCE, BE LIABLE
182             TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL, OR
183             CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE
184             SOFTWARE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING
185             RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A
186             FAILURE OF THE SOFTWARE TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF
187             SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
188             DAMAGES.
189              
190             =cut
191              
192              
193             __END__
194              
195