Calc/Row.pm

filter: Statementstmt 98.1% 54 / 55 Branchbran 100% 6 / 6 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 92.3% 12 / 13 Podpod 100% 6 / 6 totaltotal 97.5% 78 / 80 CC 5 SCAR 6.3
CC
5
Cov
98%
pack_item
4.0
packing_list
2.0
volume
2.0
CRAP 1.9
« Calc/Role/Dimensional.pm ↑ Summary Calc/Role/Mailable.pm »
1 package Box::Calc::Row;
2 13 $Box::Calc::Row::VERSION = '1.0205';
3 13 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 13 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 13 ▶use Box::Calc::Item;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 13 ▶use List::Util qw/sum/;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 13 ▶use Log::Any qw($log);
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 =head1 NAME
10  
11 Box::Calc::Row - The smallest organizational unit in a box.
12  
13 =head1 VERSION
14  
15 version 1.0205
16  
17 =head1 SYNOPSIS
18  
19 my $row = Box::Calc::Row->new(max_x => 6);
20
21 =head1 METHODS
22  
23 =head2 new(params)
24  
25 Constructor.
26  
27 =over
28  
29 =item max_x
30  
31 The maximimum width of the row. This is equivalent to the C<x> or longest dimension of the containing box.
32  
33 =back
34  
35 =head2 fill_weight()
36  
37 Returns the weight of the items in this row.
38  
39 =cut
40  
41 13 has fill_weight => (
42 is => 'rw',
43 default => 0,
44 isa => 'Num',
45 );
46  
47 =head2 fill_x()
48  
49 Returns how full the row is in the C<x> dimension.
50  
51 =cut
52  
53 13 has fill_x => (
54 is => 'rw',
55 default => 0,
56 isa => 'Num',
57 );
58  
59 =head2 fill_y()
60  
61 Returns how full the row is in the C<y> dimension.
62  
63 =cut
64  
65 13 has fill_y => (
66 is => 'rw',
67 default => 0,
68 isa => 'Num',
69 );
70  
71 =head2 fill_z()
72  
73 Returns how full the row is in the C<z> dimension.
74  
75 =cut
76  
77 13 has fill_z => (
78 is => 'rw',
79 default => 0,
80 isa => 'Num',
81 );
82  
83 =head2 max_x()
84  
85 Returns the maximum C<x> dimension of this row. See C<new> for details.
86  
87 =cut
88  
89 13 has max_x => (
90 is => 'ro',
91 required => 1,
92 isa => 'Num',
93 );
94  
95 =head2 items()
96  
97 Returns an array reference of items contained in this row.
98  
99 =head2 count_items()
100  
101 Returns the number of items contained in this row.
102  
103 =cut
104  
105 has items => (
106 is => 'rw',
107 isa => 'ArrayRef[Box::Calc::Item]',
108 5415 ▶ default => sub { [] },
Subroutine __ANON__: called CC=1 SCAR=0.0
109 13 traits => ['Array'],
110 handles => {
111 count_items => 'count',
112 }
113 );
114  
115 =head2 calculate_weight()
116  
117 Calculates the weight of all the items in this row, and returns that value.
118  
119 =cut
120  
121 sub calculate_weight {
122 7787 ▶ my $self = shift;
Subroutine calculate_weight: called CC=1 SCAR=0.0
Pod documented
123 7787 return $self->fill_weight;
124 }
125  
126 =head2 pack_item(item)
127  
128 Places an item into the row, and updates all the relevant statistics about the row.
129  
130 Returns 1 on success or 0 on failure.
131  
132 =over
133  
134 =item item
135  
136 A L<Box::Calc::Item> instance.
137  
138 =back
139  
140 =cut
141  
142 sub pack_item {
143 33221 ▶ my ($self, $item) = @_;
Subroutine pack_item: called CC=4 SCAR=13.9
Pod documented
144 33221 ▶ if ($item->x > $self->max_x - $self->fill_x) {
Branch: 1 branch
BranchTrueFalseTotal
if ($item->x > $self->max_x - $self->fill_x) 5395 27826 33221
145 5395 $log->info('No room in row for '.$item->{name}.', requesting new row.');
146 5395 return 0;
147 }
148 27826 push @{$self->items}, $item;
149 27826 $self->fill_weight($self->fill_weight + $item->weight);
150 27826 $self->fill_x($self->fill_x + $item->x);
151 27826 ▶ $self->fill_y($item->y) if $item->y > $self->fill_y;
Branch: 1 branch
BranchTrueFalseTotal
if $item->y > $self->fill_y 5417 22409 27826
152 27826 ▶ $self->fill_z($item->z) if $item->z > $self->fill_z;
Branch: 1 branch
BranchTrueFalseTotal
if $item->z > $self->fill_z 5417 22409 27826
153 27826 return 1;
154 }
155  
156  
157 =head2 packing_list(weight, list)
158  
159 Updates a scalar reference with the weight of the row and a hash reference of all the items in this row.
160  
161 =cut
162  
163 sub packing_list {
164 3874 ▶ my ($self, $weight, $list) = @_;
Subroutine packing_list: called CC=2 SCAR=6.9
Pod documented
165 3874 foreach my $item (@{$self->items}) {
166 20788 ${$weight} += $item->weight;
167 20788 $list->{$item->name}++;
168 }
169 }
170  
171 =head2 packing_instructions()
172  
173 Returns a hash reference of items contained in this row via the C<describe> method, and other important info about the row. Example:
174  
175 {
176 items => [ ... ],
177 x => 3,
178 y => 2,
179 z => 2
180 }
181  
182 =cut
183  
184 sub packing_instructions {
185 3889 ▶ my $self = shift;
Subroutine packing_instructions: called CC=1 SCAR=0.0
Pod documented
186 return {
187 3889 items => [map { $_->describe } @{ $self->items }],
188 fill_x => $self->fill_x,
189 fill_y => $self->fill_y,
190 fill_z => $self->fill_z,
191 calculated_weight => $self->calculate_weight,
192 };
193 }
194  
195  
196 =head2 used_volume
197  
198 Returns the real used volume for this row.
199  
200 =head2 volume
201  
202 Returns the exact volume needed for this row.
203  
204 =cut
205  
206 sub used_volume {
207 3885 ▶ my $self = shift;
Subroutine used_volume: called CC=1 SCAR=0.0
Pod documented
208 3885 return sum map { $_->volume } @{ $self->items };
209 }
210  
211 sub volume {
212 0 ▶ return $_[0]->fill_x * $_[0]->fill_y * $_[0]->fill_z;
Subroutine volume: not called CC=1 SCAR=6.9
Pod documented
213 }
214  
215 13 ▶no Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
216 13 __PACKAGE__->meta->make_immutable;
« Calc/Role/Dimensional.pm ↑ Summary Calc/Role/Mailable.pm »