File Coverage

blib/lib/ProgressMonitor/Stringify/ToCallback.pm
Criterion Covered Total %
statement 44 50 88.0
branch 11 18 61.1
condition 3 6 50.0
subroutine 10 12 83.3
pod 0 2 0.0
total 68 88 77.2


line stmt bran cond sub pod time code
1             package ProgressMonitor::Stringify::ToCallback;
2            
3 10     10   1566876 use warnings;
  10         26  
  10         500  
4 10     10   50 use strict;
  10         20  
  10         602  
5            
6             require ProgressMonitor::Stringify::AbstractMonitor if 0;
7            
8             use classes
9 10         74 extends => 'ProgressMonitor::Stringify::AbstractMonitor',
10             new => 'new',
11 10     10   11699 ;
  10         25994717  
12            
13             sub new
14             {
15 11     11   6120 my $class = shift;
16 11         24 my $cfg = shift;
17            
18 11         129 my $self = $class->SUPER::_new($cfg, $CLASS);
19            
20 11         43 return $self;
21             }
22            
23             sub render
24             {
25 411     411 0 596 my $self = shift;
26            
27             # call the tick callback with the normal rendering
28             # unless the message callback is set, then don't render message
29             # and pass that separately to the msg callback
30             #
31 411         1194 my $cfg = $self->_get_cfg;
32 411         1501 my $tcb = $cfg->get_tickCallback;
33 411         2100 my $mcb = $cfg->get_messageCallback;
34 411 100       2960 my $cancel = $tcb->($self->_toString($mcb ? 0 : 1));
35 411 100       3691499 if ($mcb)
36             {
37 189         596 my $msg = $self->_get_message;
38 189 100       478 $mcb->($msg) if $msg;
39             }
40 411 50       23683 $self->setCanceled($cancel) unless $self->isCanceled;
41            
42 411         940 return;
43             }
44            
45             sub setErrorMessage
46             {
47 0     0 0 0 my $self = shift;
48 0         0 my $msg = $self->SUPER::setErrorMessage(shift());
49            
50 0         0 my $emcb = $self->_get_cfg->get_errorMessageCallback;
51 0 0       0 $emcb->($msg) if $emcb;
52             }
53            
54             ###
55            
56             package ProgressMonitor::Stringify::ToCallbackConfiguration;
57            
58 10     10   5890 use strict;
  10         23  
  10         332  
59 10     10   54 use warnings;
  10         19  
  10         549  
60            
61             # Attributes:
62             # callback (code ref)
63             # The callback will be called with the rendered string and should return a
64             # boolean, which will be used to set the cancellation status with.
65             use classes
66 10         60 extends => 'ProgressMonitor::Stringify::AbstractMonitorConfiguration',
67             attrs => ['tickCallback', 'messageCallback', 'errorMessageCallback'],
68 10     10   55 ;
  10         21  
69            
70             sub defaultAttributeValues
71             {
72 11     11   22 my $self = shift;
73            
74             return {
75 11         120 %{$self->SUPER::defaultAttributeValues()},
76 0     0   0 tickCallback => sub { X::Usage->throw("missing tickCallback"); 1; },
  0         0  
77 11         25 messageCallback => undef,
78             errorMessageCallback => undef,
79             };
80             }
81            
82             sub checkAttributeValues
83             {
84 11     11   28 my $self = shift;
85            
86 11         89 $self->SUPER::checkAttributeValues();
87            
88 11 50       55 X::Usage->throw("tickCallback is not a code ref") unless ref($self->get_tickCallback) eq 'CODE';
89            
90 11         119 my $mcb = $self->get_messageCallback;
91 11 50 66     95 X::Usage->throw("messageCallback is not a code ref") if ($mcb && ref($mcb) ne 'CODE');
92            
93 11         47 my $emcb = $self->get_errorMessageCallback;
94 11 50 33     116 X::Usage->throw("errorMessageCallback is not a code ref") if ($emcb && ref($emcb) ne 'CODE');
95            
96 11 50       37 X::Usage->throw("maxWidth not set") unless $self->get_maxWidth;
97            
98 11         76 return;
99             }
100            
101             ############################
102            
103             =head1 NAME
104            
105             ProgressMonitor::Stringify::ToCallback - a monitor implementation that provides
106             stringified feedback to a callback.
107            
108             =head1 SYNOPSIS
109            
110             ...
111             # call someTask and give it a monitor to call us back
112             # on callback, just do something unimaginative (print it...:-) and return 0 (don't cancel)
113             #
114             someTask(ProgressMonitor::Stringify::ToCallback->new({fields => [ ... ], tickCallback => sub { print "GOT: ", shift(), "\n"; 0; });
115            
116             =head1 DESCRIPTION
117            
118             This is a concrete implementation of a ProgressMonitor. It will send the stringified
119             feedback to a callback (code ref) supplied by the user.
120            
121             Inherits from ProgressMonitor::Stringify::AbstractMonitor.
122            
123             =head1 METHODS
124            
125             =over 2
126            
127             =item new( $hashRef )
128            
129             Note that the maxWidth must be set explicitly.
130            
131             Configuration data:
132            
133             =over 2
134            
135             =item tickCallback
136            
137             A code reference to an anonymous sub. For each rendering tick, it will be called
138             with the rendered string as the argument. The return value will be used to
139             set the cancellation status.
140            
141             =item messageCallback
142            
143             A code reference that will be called specifically with the current message.
144             Note that setting this changes the behavior of tickCallback; normally,
145             tickCallback will receive the rendered string including any message.
146             However, by setting messageCallback, the message will be skipped during
147             rendition of the ordinary fields. Also, if this is set, the strategy used
148             is of no importance.
149            
150             =item errorMessageCallback
151            
152             A code reference that will be called with the current error message.
153            
154             =back
155            
156             =back
157            
158             =head1 AUTHOR
159            
160             Kenneth Olwing, C<< >>
161            
162             =head1 BUGS
163            
164             I wouldn't be surprised! If you can come up with a minimal test that shows the
165             problem I might be able to take a look. Even better, send me a patch.
166            
167             Please report any bugs or feature requests to
168             C, or through the web interface at
169             L.
170             I will be notified, and then you'll automatically be notified of progress on
171             your bug as I make changes.
172            
173             =head1 SUPPORT
174            
175             You can find general documentation for this module with the perldoc command:
176            
177             perldoc ProgressMonitor
178            
179             =head1 ACKNOWLEDGEMENTS
180            
181             Thanks to my family. I'm deeply grateful for you!
182            
183             =head1 COPYRIGHT & LICENSE
184            
185             Copyright 2006,2007 Kenneth Olwing, all rights reserved.
186            
187             This program is free software; you can redistribute it and/or modify it
188             under the same terms as Perl itself.
189            
190             =cut
191            
192             1; # End of ProgressMonitor::Stringify::ToCallback