File Coverage

blib/lib/Box/Calc/Row.pm
Criterion Covered Total %
statement 45 46 97.8
branch 6 6 100.0
condition n/a
subroutine 11 12 91.6
pod 6 6 100.0
total 68 70 97.1


line stmt bran cond sub pod time code
1             package Box::Calc::Row;
2             $Box::Calc::Row::VERSION = '1.0201';
3 12     12   1000 use strict;
  12         24  
  12         340  
4 12     12   55 use Moose;
  12         23  
  12         63  
5 12     12   73579 use Box::Calc::Item;
  12         45230  
  12         396  
6 12     12   76 use List::Util qw/sum/;
  12         25  
  12         913  
7 12     12   5527 use Log::Any qw($log);
  12         91011  
  12         62  
8              
9             =head1 NAME
10              
11             Box::Calc::Row - The smallest organizational unit in a box.
12              
13             =head1 VERSION
14              
15             version 1.0201
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             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             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             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             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             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             default => sub { [] },
109             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 3937     3937 1 5464 my $self = shift;
123 3937         88250 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 20859     20859 1 45606 my ($self, $item) = @_;
144 20859 100       452673 if ($item->x > $self->max_x - $self->fill_x) {
145 3560         19767 $log->info('No room in row for '.$item->{name}.', requesting new row.');
146 3560         19617 return 0;
147             }
148 17299         26704 push @{$self->items}, $item;
  17299         360223  
149 17299         372246 $self->fill_weight($self->fill_weight + $item->weight);
150 17299         361284 $self->fill_x($self->fill_x + $item->x);
151 17299 100       380374 $self->fill_y($item->y) if $item->y > $self->fill_y;
152 17299 100       373818 $self->fill_z($item->z) if $item->z > $self->fill_z;
153 17299         51693 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 1949     1949 1 4286 my ($self, $weight, $list) = @_;
165 1949         2293 foreach my $item (@{$self->items}) {
  1949         42179  
166 10418         13220 ${$weight} += $item->weight;
  10418         246638  
167 10418         223603 $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 1964     1964 1 2795 my $self = shift;
186             return {
187 1964         2582 items => [map { $_->describe } @{ $self->items }],
  10448         23594  
  1964         44839  
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 1960     1960 1 2713 my $self = shift;
208 1960         2347 return sum map { $_->volume } @{ $self->items };
  10442         243740  
  1960         42365  
209             }
210              
211             sub volume {
212 0     0 1   return $_[0]->fill_x * $_[0]->fill_y * $_[0]->fill_z;
213             }
214              
215 12     12   28991 no Moose;
  12         24  
  12         105  
216             __PACKAGE__->meta->make_immutable;