Calc/Box.pm

filter: Statementstmt 97.0% 100 / 103 Branchbran 92.8% 13 / 14 Conditioncond 100% 2 / 2 MC/DCmcdc 100% 1 / 1 Subroutinesub 100% 23 / 23 Podpod 90.0% 9 / 10 totaltotal 96.7% 148 / 153 SCAR 25.7
CC
13
Cov
97%
pack_item
7.1
fill_x
3.0
fill_y
3.0
CRAP 13.0
« Calc/Layer.pm ↑ Summary Calc/Role/Container.pm »
1 package Box::Calc::Box;
2 9 $Box::Calc::Box::VERSION = '1.0201';
3 9 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 9 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 9 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 9 ▶use Storable qw(dclone);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 9 with 'Box::Calc::Role::Container';
8 9 with 'Box::Calc::Role::Mailable';
9 9 ▶use Box::Calc::Layer;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 9 ▶use Data::GUID;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 9 ▶use List::Util qw/sum/;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 9 ▶use Log::Any qw($log);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 9 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 =head1 NAME
16  
17 Box::Calc::Box - The container in which we pack items.
18  
19 =head1 VERSION
20  
21 version 1.0201
22  
23 =head1 SYNOPSIS
24  
25 my $box = Box::Calc::Box->new(name => 'Big Box', x => 12, y => 12, z => 18, weight => 20);
26  
27 =head1 METHODS
28  
29 =head2 new(params)
30  
31 Constructor.
32  
33 B<NOTE:> All boxes automatically have one empty L<Box::Calc::Layer> added to them.
34  
35 =over
36  
37 =item params
38  
39 =over
40  
41 =item name
42  
43 An identifying name for your box.
44  
45 =item x
46  
47 The interior width of your box.
48  
49 =item y
50  
51 The interior length of your box.
52  
53 =item z
54  
55 The interior thickness of your box.
56  
57 =item weight
58  
59 The weight of your box.
60  
61 =back
62  
63 =back
64  
65 =head2 fill_weight()
66  
67 Returns the weight of the items in this box.
68  
69 =cut
70  
71 9 has fill_weight => (
72 is => 'rw',
73 default => 0,
74 isa => 'Num',
75 );
76  
77 =head2 fill_x()
78  
79 Returns how full the box is in the C<x> dimension.
80  
81 =cut
82  
83 sub fill_x {
84 21 ▶ my $self = shift;
Subroutine fill_x: called CC=3 SCAR=11.0
Pod documented
85 21 my $value = 0;
86 21 foreach my $layer (@{$self->layers}) {
87 273 ▶ $value = $layer->fill_x if $layer->fill_x > $value;
Branch: 1 branch
BranchTrueFalseTotal
if $layer->fill_x > $value 22 251 273
88 }
89 21 return sprintf ("%.4f", $value);
90 }
91  
92 =head2 fill_y()
93  
94 Returns how full the box is in the C<y> dimension.
95  
96 =cut
97  
98 sub fill_y {
99 21 ▶ my $self = shift;
Subroutine fill_y: called CC=3 SCAR=11.0
Pod documented
100 21 my $value = 0;
101 21 foreach my $layer (@{$self->layers}) {
102 273 ▶ $value = $layer->fill_y if $layer->fill_y > $value;
Branch: 1 branch
BranchTrueFalseTotal
if $layer->fill_y > $value 22 251 273
103 }
104 21 return sprintf ("%.4f", $value);
105 }
106  
107 =head2 fill_z()
108  
109 Returns how full the box is in the C<z> dimension.
110  
111 =cut
112  
113 sub fill_z {
114 18373 ▶ my $self = shift;
Subroutine fill_z: called CC=2 SCAR=6.9
Pod documented
115 18373 my $value = 0;
116 18373 foreach my $layer (@{$self->layers}) {
117 343897 $value += $layer->fill_z;
118 }
119 18373 return sprintf ("%.4f", $value);
120 }
121  
122 =head2 id()
123  
124 Returns a generated unique id for this box.
125  
126 =cut
127  
128 has id => (
129 is => 'ro',
130 34 ▶ default => sub { Data::GUID->new->as_string },
Subroutine __ANON__: called CC=1 SCAR=0.0
131 9 );
132  
133 =head2 name()
134  
135 Returns the name of the box.
136  
137 =cut
138  
139 9 has name => (
140 is => 'ro',
141 isa => 'Str',
142 required => 1,
143 );
144  
145 =head2 layers()
146  
147 Returns an array reference of the L<Box::Calc::Layer>s in this box.
148  
149 =cut
150  
151 has layers => (
152 is => 'rw',
153 isa => 'ArrayRef[Box::Calc::Layer]',
154 34 ▶ default => sub { [] },
Subroutine __ANON__: called CC=1 SCAR=0.0
155 9 traits => ['Array'],
156 handles => {
157 count_layers => 'count',
158 }
159 );
160  
161 =head2 void_weight()
162  
163 Returns the weight assigned to the void space left in the box due to void space filler such as packing peanuts. Defaults to 70% of the box weight.
164  
165 =cut
166  
167 has void_weight => (
168 is => 'rw',
169 lazy => 1,
170 default => sub {
171 33 ▶ my $self = shift;
Subroutine __ANON__: called CC=1 SCAR=0.0
172 33 return $self->weight * 0.7;
173 }
174 9 );
175  
176 =head2 calculate_weight()
177  
178 Calculates and returns the weight of all the layers in this box, including the weight of this box and any packing filler (see L<void_weight>).
179  
180 =cut
181  
182 sub calculate_weight {
183 17842 ▶ my $self = shift;
Subroutine calculate_weight: called CC=1 SCAR=0.0
Pod documented
184 17842 return $self->weight + $self->void_weight + $self->fill_weight;
185 }
186  
187 =head2 create_layer()
188  
189 Adds a new L<Box::Calc::Layer> to this box.
190  
191 =cut
192  
193 sub create_layer {
194 544 ▶ my $self = shift;
Subroutine create_layer: called CC=1 SCAR=0.0
Pod documented
195 544 push @{$self->layers}, Box::Calc::Layer->new( max_x => $self->x, max_y => $self->y, );
196 }
197  
198 sub BUILD {
199 34 ▶ my $self = shift;
Subroutine BUILD: called CC=1 SCAR=0.0
Pod undocumented
200 34 $self->create_layer;
201 }
202  
203 =head2 pack_item(item)
204  
205 Add a L<Box::Calc::Item> to this box.
206  
207 Returns 1 on success or 0 on failure.
208  
209 =over
210  
211 =item item
212  
213 The L<Box::Calc::Item> instance you want to add to this box.
214  
215 =back
216  
217 =cut
218  
219 sub pack_item {
220 17820 ▶ my ($self, $item, $count) = @_;
Subroutine pack_item: called CC=7 SCAR=19.6
Pod documented
221 17820 ▶ $count ||= 1;
Condition100%2/2
$count ||= 1
l!l
51017310
MC/DC100%1/1
$count ||= 1
$count
Truth table100%2/2
A
A= $count
Aresult
11
00
222 17820 ▶ if ($count > 99) {
Branch: 1 branch
BranchTrueFalseTotal
if ($count > 99) 0 17820 17820
223 0 $log->warn($item->{name}.' is causing infinite recursion in Box::Calc');
224 0 $log->debug(Dumper($item));
225 0 return 0;
226 }
227 17820 ▶ if ($item->weight + $self->calculate_weight >= $self->max_weight) {
Branch: 1 branch
BranchTrueFalseTotal
if ($item->weight + $self->calculate_weight >= $self->max_weight) 1 17819 17820
228 1 $log->info($item->{name}.' would make this box weigh too much, requesting new box.');
229 1 return 0;
230 }
231 # item height > ( box height - box fill + the height of the current layer )
232 17819 ▶ if ($item->z > $self->z - $self->fill_z + $self->layers->[-1]->fill_z) {
Branch: 1 branch
BranchTrueFalseTotal
if ($item->z > $self->z - $self->fill_z + $self->layers->[-1]->fill_z) 1 17818 17819
233 1 $log->info($item->{name}.' would make the layer too tall to fit in the box, requesting new box.');
234 1 return 0;
235 }
236 17818 ▶ if ($self->layers->[-1]->pack_item($item)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->layers->[-1]->pack_item($item)) { } 17287 531 17818
237 17287 $self->fill_weight($self->fill_weight + $item->weight);
238 17287 return 1;
239 }
240 else {
241 531 ▶ if ($item->z > $self->z - $self->fill_z) {
Branch: 1 branch
BranchTrueFalseTotal
if ($item->z > $self->z - $self->fill_z) { } 21 510 531
242 21 $log->info($item->{name}.' is too big to create another layer in this box, requesting another box.');
243 21 return 0;
244 }
245 else {
246 510 $self->create_layer;
247 510 return $self->pack_item($item, $count + 1);
248 }
249 }
250 }
251  
252 =head2 packing_list()
253  
254 Returns a scalar with the weight of the box and a hash reference of all the items in this box.
255  
256 =cut
257  
258 sub packing_list {
259 15 ▶ my $self = shift;
Subroutine packing_list: called CC=2 SCAR=6.9
Pod documented
260 15 my $weight = $self->weight;
261 15 my $list = {};
262 15 foreach my $layer (@{$self->layers}) {
263 264 $layer->packing_list(\$weight, $list)
264 }
265 15 return ($weight, $list);
266 }
267  
268 =head2 packing_instructions()
269  
270 Returns a description of the box. Example:
271  
272 {
273 x => 5,
274 y => 6,
275 z => 3,
276 fill_x => 4,
277 fill_y => '5.1',
278 fill_z => 2,
279 name => 'The Big Box',
280 layers => [ ... ],
281 id => 'xxx',
282 weight => '6',
283 calculated_weight => '12.35',
284 }
285  
286 =cut
287  
288 sub packing_instructions {
289 20 ▶ my $self = shift;
Subroutine packing_instructions: called CC=1 SCAR=0.0
Pod documented
290 return {
291 x => $self->x,
292 y => $self->y,
293 z => $self->z,
294 fill_x => $self->fill_x,
295 fill_y => $self->fill_y,
296 fill_z => $self->fill_z,
297 name => $self->name,
298 id => $self->id,
299 weight => $self->weight,
300 calculated_weight => $self->calculate_weight,
301 used_volume => $self->used_volume,
302 volume => $self->volume,
303 20 layers => [map { $_->packing_instructions } @{ $self->layers }],
304 };
305 }
306  
307 =head2 used_volume
308  
309 Returns the real used volume for this box.
310  
311 =cut
312  
313 sub used_volume {
314 20 ▶ my $self = shift;
Subroutine used_volume: called CC=1 SCAR=0.0
Pod documented
315 20 return sum map { $_->used_volume } @{ $self->layers };
316 }
317  
318 =head2 volume
319  
320 Returns the exact volume needed for this box.
321  
322 =cut
323  
324 sub volume {
325 return $_[0]->fill_x * $_[0]->fill_y * $_[0]->fill_z;
326 }
327  
328 9 ▶no Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
329 9 __PACKAGE__->meta->make_immutable;
330  
331 =for Pod::Coverage BUILD
« Calc/Layer.pm ↑ Summary Calc/Role/Container.pm »