File Coverage

blib/lib/ProgressMonitor/Stringify/Fields/AbstractDynamicField.pm
Criterion Covered Total %
statement 33 43 76.7
branch 2 4 50.0
condition 0 3 0.0
subroutine 10 11 90.9
pod 1 1 100.0
total 46 62 74.1


line stmt bran cond sub pod time code
1             package ProgressMonitor::Stringify::Fields::AbstractDynamicField;
2            
3 2     2   3196 use warnings;
  2         6  
  2         70  
4 2     2   46 use strict;
  2         6  
  2         109  
5            
6             require ProgressMonitor::Stringify::Fields::AbstractField if 0;
7            
8             use classes
9 2         17 extends => 'ProgressMonitor::Stringify::Fields::AbstractField',
10             methods => {grabExtraWidth => 'grabExtraWidth', widthChange => 'ABSTRACT'},
11 2     2   11 ;
  2         4  
12            
13             sub isDynamic
14             {
15 1     1 1 3 my $self = shift;
16            
17             # we're only dynamic if the stated maxWidth is greater than our current width
18             #
19 1         3 return $self->get_width < $self->_get_cfg->get_maxWidth;
20             }
21            
22             sub grabExtraWidth
23             {
24 0     0   0 my $self = shift;
25 0         0 my $extraWidth = shift;
26            
27 0         0 my $cfg = $self->_get_cfg;
28            
29             # take as much width we can from the given one, but respect the max
30             #
31 0         0 my $width = $self->get_width;
32 0         0 my $maxWidth = $cfg->get_maxWidth;
33 0   0     0 while ($extraWidth && $width < $maxWidth)
34             {
35 0         0 $width++;
36 0         0 $extraWidth--;
37             }
38            
39 0         0 $self->_set_width($width);
40            
41 0         0 return $extraWidth;
42             }
43            
44             ### PROTECTED
45            
46             # override the width setter, and notify that the width has been changed
47             #
48             sub _set_width
49             {
50 1     1   3 my $self = shift;
51            
52 1         10 $self->SUPER::_set_width(@_);
53            
54 1         5 $self->widthChange;
55            
56 1         3 return;
57             }
58            
59             ###
60            
61             package ProgressMonitor::Stringify::Fields::AbstractDynamicFieldConfiguration;
62            
63 2     2   677 use strict;
  2         4  
  2         49  
64 2     2   8 use warnings;
  2         5  
  2         92  
65            
66             # Attributes:
67             # minWidth
68             # The minimum width the field should use. This may however be too small
69             # for the fields 'minimum computed width' which can depend on many other
70             # settings and thus this value is raised as needed.
71             # maxWidth
72             # The maximum width the field should use, regardless if it is given more.
73             # Defaults to a very large value
74             #
75             use classes
76 2         8 extends => 'ProgressMonitor::Stringify::Fields::AbstractFieldConfiguration',
77             attrs => ['minWidth', 'maxWidth',],
78 2     2   9 ;
  2         3  
79            
80             sub defaultAttributeValues
81             {
82 1     1   3 my $self = shift;
83            
84             return {
85 1         2 %{$self->SUPER::defaultAttributeValues()},
  1         12  
86             minWidth => 0,
87             maxWidth => 1 << 31,
88             };
89             }
90            
91             sub checkAttributeValues
92             {
93 1     1   3 my $self = shift;
94            
95 1         19 $self->SUPER::checkAttributeValues;
96            
97 1         11 my $minWidth = $self->get_minWidth;
98 1 50       9 X::Usage->throw("minWidth must be positive") if $minWidth < 0;
99 1 50       9 X::Usage->throw("maxWidth must be greater or equal to minWidth") if $self->get_maxWidth < $minWidth;
100            
101 1         9 return;
102             }
103            
104             ############################
105            
106             =head1 NAME
107            
108             ProgressMonitor::Stringify::Fields::AbstractDynamicField - A reusable/abstract
109             dynamic field implementation for stringify progress.
110            
111             =head1 DESCRIPTION
112            
113             Inherits from ProgressMonitor::Stringify::Fields::AbstractField. See that for more
114             information; this class signals participation in the dynamic width negotiation
115             protocol as utilized by ProgressMonitor::Stringify::AbstractMonitor and subclasses.
116            
117             Inherit from this class if you wish to dynamically adjust your field width to grab
118             as much as possible.
119            
120             =head1 METHODS
121            
122             =over 2
123            
124             =item grabExtraWidth( $extraWidth )
125            
126             Called with extra width available. Consume all or part of this by updating the
127             inherited width attribute and return the width not used.
128            
129             This method may be called multiple times in order to fairly distribute extra width
130             across several dynamic fields
131            
132             =item isDynamic
133            
134             Returns true as long as the current width is less than maxWidth.
135            
136             =item widthChange
137            
138             Notification that the width has changed, thus giving the field a chance to recompute
139             some of its attributes as needed.
140            
141             =back
142            
143             =head1 PROTECTED METHODS
144            
145             =over 2
146            
147             =item _new( $hashRef, $package )
148            
149             The constructor, needs to be called by subclasses.
150            
151             Configuration data:
152            
153             =over 2
154            
155             =item minWidth (default => 0)
156            
157             The minimum width this field should use.
158            
159             =item maxWidth
160            
161             The maximum width this field should use.
162            
163             =back
164            
165             =item _set_width( $newWidth )
166            
167             Calls SUPER and then widthChange.
168            
169             =back
170            
171             =head1 AUTHOR
172            
173             Kenneth Olwing, C<< >>
174            
175             =head1 BUGS
176            
177             I wouldn't be surprised! If you can come up with a minimal test that shows the
178             problem I might be able to take a look. Even better, send me a patch.
179            
180             Please report any bugs or feature requests to
181             C, or through the web interface at
182             L.
183             I will be notified, and then you'll automatically be notified of progress on
184             your bug as I make changes.
185            
186             =head1 SUPPORT
187            
188             You can find general documentation for this module with the perldoc command:
189            
190             perldoc ProgressMonitor
191            
192             =head1 ACKNOWLEDGEMENTS
193            
194             Thanks to my family. I'm deeply grateful for you!
195            
196             =head1 COPYRIGHT & LICENSE
197            
198             Copyright 2006,2007 Kenneth Olwing, all rights reserved.
199            
200             This program is free software; you can redistribute it and/or modify it
201             under the same terms as Perl itself.
202            
203             =cut
204            
205             1; # End of ProgressMonitor::Stringify::Fields::AbstractDynamicField