ProgressMonitor/Stringify/ToCallback.pm

filter: Statementstmt 88.4% 46 / 52 Branchbran 61.1% 11 / 18 Conditioncond 50.0% 3 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 83.3% 10 / 12 Podpod 0.0% 0 / 2 totaltotal 74.4% 70 / 94 CC 12 SCAR 15.3
CC
12
Cov
79%
checkAttributeValues
8.4
setErrorMessage
6.0
render
5.0
CRAP 4.6
« ProgressMonitor/AbstractStatefulMonitor.pm ↑ Summary ProgressMonitor/AbstractConfiguration.pm »
1 package ProgressMonitor::Stringify::ToCallback;
2
3 10 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 10 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5
6 10 require ProgressMonitor::Stringify::AbstractMonitor if 0;
7
8 use classes
9 10 ▶ extends => 'ProgressMonitor::Stringify::AbstractMonitor',
Subroutine BEGIN: called CC=1 SCAR=0.0
10 new => 'new',
11 10 ;
12
13 sub new
14 11 ▶{
Subroutine new: called CC=1 SCAR=0.0
15 11 my $class = shift;
16 11 my $cfg = shift;
17
18 11 my $self = $class->SUPER::_new($cfg, $CLASS);
19
20 11 return $self;
21 }
22
23 sub render
24 411 ▶{
Subroutine render: called CC=5 SCAR=16.1
Pod undocumented
25 411 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 my $cfg = $self->_get_cfg;
32 411 my $tcb = $cfg->get_tickCallback;
33 411 my $mcb = $cfg->get_messageCallback;
34 411 ▶ my $cancel = $tcb->($self->_toString($mcb ? 0 : 1));
Branch: 1 branch
BranchTrueFalseTotal
$mcb ? : 189 222 411
35 411 ▶ if ($mcb)
Branch: 1 branch
BranchTrueFalseTotal
if ($mcb) 189 222 411
36 {
37 189 my $msg = $self->_get_message;
38 189 ▶ $mcb->($msg) if $msg;
Branch: 1 branch
BranchTrueFalseTotal
if $msg 60 129 189
39 }
40 411 ▶ $self->setCanceled($cancel) unless $self->isCanceled;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->isCanceled 411 0 411
41
42 411 return;
43 }
44
45 sub setErrorMessage
46 0 ▶{
Subroutine setErrorMessage: not called CC=2 SCAR=17.9
Pod undocumented
47 0 my $self = shift;
48 0 my $msg = $self->SUPER::setErrorMessage(shift());
49
50 0 my $emcb = $self->_get_cfg->get_errorMessageCallback;
51 0 ▶ $emcb->($msg) if $emcb;
Branch: 1 branch
BranchTrueFalseTotal
if $emcb 0 0 0
52 }
53
54 ###
55
56 package ProgressMonitor::Stringify::ToCallbackConfiguration;
57
58 10 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
59 10 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
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 ▶ extends => 'ProgressMonitor::Stringify::AbstractMonitorConfiguration',
Subroutine BEGIN: called CC=1 SCAR=0.0
67 attrs => ['tickCallback', 'messageCallback', 'errorMessageCallback'],
68 10 ;
69
70 sub defaultAttributeValues
71 11 ▶{
Subroutine defaultAttributeValues: called CC=1 SCAR=0.6
72 11 my $self = shift;
73
74 return {
75 11 %{$self->SUPER::defaultAttributeValues()},
76 0 ▶ tickCallback => sub { X::Usage->throw("missing tickCallback"); 1; },
Subroutine __ANON__: not called CC=1 SCAR=6.9
77 11 messageCallback => undef,
78 errorMessageCallback => undef,
79 };
80 }
81
82 sub checkAttributeValues
83 11 ▶{
Subroutine checkAttributeValues: called CC=7 SCAR=21.3
84 11 my $self = shift;
85
86 11 $self->SUPER::checkAttributeValues();
87
88 11 ▶ X::Usage->throw("tickCallback is not a code ref") unless ref($self->get_tickCallback) eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self->get_tickCallback eq "CODE" 0 11 11
89
90 11 my $mcb = $self->get_messageCallback;
91 11 ▶ X::Usage->throw("messageCallback is not a code ref") if ($mcb && ref($mcb) ne 'CODE');
Branch: 1 branch
BranchTrueFalseTotal
if $mcb and ref $mcb ne "CODE" 0 11 11
Condition66%2/3
$mcb and ref $mcb ne "CODE"
!ll&&!rl&&r
830
MC/DC0%0/2
$mcb and ref $mcb ne "CODE"
$mcb ref $mcb ne "CODE"
Truth table66%2/3
A and B
A= $mcbB= ref $mcb ne "CODE"
ABresult
0X0
100
111
92
93 11 my $emcb = $self->get_errorMessageCallback;
94 11 ▶ X::Usage->throw("errorMessageCallback is not a code ref") if ($emcb && ref($emcb) ne 'CODE');
Branch: 1 branch
BranchTrueFalseTotal
if $emcb and ref $emcb ne "CODE" 0 11 11
Condition33%1/3
$emcb and ref $emcb ne "CODE"
!ll&&!rl&&r
1100
MC/DC0%0/2
$emcb and ref $emcb ne "CODE"
$emcb ref $emcb ne "CODE"
Truth table33%1/3
A and B
A= $emcbB= ref $emcb ne "CODE"
ABresult
0X0
100
111
95
96 11 ▶ X::Usage->throw("maxWidth not set") unless $self->get_maxWidth;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->get_maxWidth 0 11 11
97
98 11 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<< <knth at cpan.org> >>
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<bug-progressmonitor at rt.cpan.org>, or through the web interface at
169 L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=ProgressMonitor>.
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 10 1; # End of ProgressMonitor::Stringify::ToCallback
« ProgressMonitor/AbstractStatefulMonitor.pm ↑ Summary ProgressMonitor/AbstractConfiguration.pm »