Calc/Box.pm

filter: Statementstmt 97.1% 101 / 104 Branchbran 92.8% 13 / 14 Conditioncond 100% 2 / 2 MC/DCmcdc 100% 1 / 1 Subroutinesub 100% 24 / 24 Podpod 90.9% 10 / 11 totaltotal 96.7% 151 / 156 CC 13 SCAR 9.7
CC
13
Cov
97%
pack_item
7.1
fill_x
3.0
fill_y
3.0
CRAP 2.6
« Calc/Role/Container.pm ↑ Summary Calc/Role/Dimensional.pm »
1 package Box::Calc::Box;
2 10 $Box::Calc::Box::VERSION = '1.0205';
3 10 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 10 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 10 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 10 ▶use Storable qw(dclone);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 10 with 'Box::Calc::Role::Container';
8 10 with 'Box::Calc::Role::Mailable';
9 10 ▶use Box::Calc::Layer;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 10 ▶use Data::GUID;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 10 ▶use List::Util qw/sum/;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 10 ▶use Log::Any qw($log);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 10 ▶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.0205
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 10 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 65 ▶ my $self = shift;
Subroutine fill_x: called CC=3 SCAR=11.0
Pod documented
85 65 my $value = 0;
86 65 foreach my $layer (@{$self->layers}) {
87 1045 ▶ $value = $layer->fill_x if $layer->fill_x > $value;
Branch: 1 branch
BranchTrueFalseTotal
if $layer->fill_x > $value 68 977 1045
88 }
89 65 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 65 ▶ my $self = shift;
Subroutine fill_y: called CC=3 SCAR=11.0
Pod documented
100 65 my $value = 0;
101 65 foreach my $layer (@{$self->layers}) {
102 1045 ▶ $value = $layer->fill_y if $layer->fill_y > $value;
Branch: 1 branch
BranchTrueFalseTotal
if $layer->fill_y > $value 68 977 1045
103 }
104 65 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 29556 ▶ my $self = shift;
Subroutine fill_z: called CC=2 SCAR=6.9
Pod documented
115 29556 my $value = 0;
116 29556 foreach my $layer (@{$self->layers}) {
117 609472 $value += $layer->fill_z;
118 }
119 29556 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 47 ▶ default => sub { Data::GUID->new->as_string },
Subroutine __ANON__: called CC=1 SCAR=0.0
131 10 );
132  
133 =head2 name()
134  
135 Returns the name of the box.
136  
137 =cut
138  
139 10 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 47 ▶ default => sub { [] },
Subroutine __ANON__: called CC=1 SCAR=0.0
155 10 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 2 ▶ my $self = shift;
Subroutine __ANON__: called CC=1 SCAR=0.0
172 2 return $self->weight * 0.7;
173 }
174 10 );
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 28688 ▶ my $self = shift;
Subroutine calculate_weight: called CC=1 SCAR=0.0
Pod documented
184 28688 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 851 ▶ my $self = shift;
Subroutine create_layer: called CC=1 SCAR=0.0
Pod documented
195 851 push @{$self->layers}, Box::Calc::Layer->new( max_x => $self->x, max_y => $self->y, );
196 }
197  
198 sub BUILD {
199 47 ▶ my $self = shift;
Subroutine BUILD: called CC=1 SCAR=0.0
Pod undocumented
200 47 $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 28653 ▶ my ($self, $item, $count) = @_;
Subroutine pack_item: called CC=7 SCAR=19.6
Pod documented
221 28653 ▶ $count ||= 1;
Condition100%2/2
$count ||= 1
l!l
80427849
MC/DC100%1/1
$count ||= 1
$count
Truth table100%2/2
A
A= $count
Aresult
11
00
222 28653 ▶ if ($count > 5) {
Branch: 1 branch
BranchTrueFalseTotal
if ($count > 5) 0 28653 28653
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 28653 ▶ if ($item->weight + $self->calculate_weight >= $self->max_weight) {
Branch: 1 branch
BranchTrueFalseTotal
if ($item->weight + $self->calculate_weight >= $self->max_weight) 1 28652 28653
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 28652 ▶ 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 28651 28652
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 28651 ▶ if ($self->layers->[-1]->pack_item($item)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->layers->[-1]->pack_item($item)) { } 27814 837 28651
237 27814 $self->fill_weight($self->fill_weight + $item->weight);
238 27814 return 1;
239 }
240 else {
241 837 ▶ if ($item->z > $self->z - $self->fill_z) {
Branch: 1 branch
BranchTrueFalseTotal
if ($item->z > $self->z - $self->fill_z) { } 33 804 837
242 33 $log->info($item->{name}.' is too big to create another layer in this box, requesting another box.');
243 33 return 0;
244 }
245 else {
246 804 $self->create_layer;
247 804 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 27 ▶ my $self = shift;
Subroutine packing_list: called CC=2 SCAR=6.9
Pod documented
260 27 my $weight = $self->weight;
261 27 my $list = {};
262 27 foreach my $layer (@{$self->layers}) {
263 514 $layer->packing_list(\$weight, $list)
264 }
265 27 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 32 ▶ 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 fill_volume => $self->fill_volume,
303 volume => $self->volume,
304 32 layers => [map { $_->packing_instructions } @{ $self->layers }],
305 };
306 }
307  
308 =head2 used_volume
309  
310 Returns the real used volume for this box.
311  
312 =cut
313  
314 sub used_volume {
315 32 ▶ my $self = shift;
Subroutine used_volume: called CC=1 SCAR=0.0
Pod documented
316 32 return sum map { $_->used_volume } @{ $self->layers };
317 }
318  
319 =head2 fill_volume
320  
321 Returns the exact volume needed for this box.
322  
323 =cut
324  
325 sub fill_volume {
326 32 ▶ return $_[0]->fill_x * $_[0]->fill_y * $_[0]->fill_z;
Subroutine fill_volume: called CC=1 SCAR=0.0
Pod documented
327 }
328  
329 10 ▶no Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
330 10 __PACKAGE__->meta->make_immutable;
331  
332 =for Pod::Coverage BUILD
« Calc/Role/Container.pm ↑ Summary Calc/Role/Dimensional.pm »