| 1 | package Box::Calc::Row; | ||||||||||
| 2 | 12 | $Box::Calc::Row::VERSION = '1.0201'; | |||||||||
| 3 | 12 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 12 | ▶ | use Moose; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 12 | ▶ | use Box::Calc::Item; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 12 | ▶ | use List::Util qw/sum/; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | 12 | ▶ | 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.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 | 12 | 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 | 12 | 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 | 12 | 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 | 12 | 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 | 12 | 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 | 3580 | ▶ | default => sub { [] }, | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 109 | 12 | 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 | ▶ | my $self = shift; | ||||||||
|
Subroutine
calculate_weight: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 123 | 3937 | 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 | ▶ | my ($self, $item) = @_; | ||||||||
|
Subroutine
pack_item: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||
| 144 | 20859 | ▶ | if ($item->x > $self->max_x - $self->fill_x) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 145 | 3560 | $log->info('No room in row for '.$item->{name}.', requesting new row.'); | |||||||||
| 146 | 3560 | return 0; | |||||||||
| 147 | } | ||||||||||
| 148 | 17299 | push @{$self->items}, $item; | |||||||||
| 149 | 17299 | $self->fill_weight($self->fill_weight + $item->weight); | |||||||||
| 150 | 17299 | $self->fill_x($self->fill_x + $item->x); | |||||||||
| 151 | 17299 | ▶ | $self->fill_y($item->y) if $item->y > $self->fill_y; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 152 | 17299 | ▶ | $self->fill_z($item->z) if $item->z > $self->fill_z; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 153 | 17299 | 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 | ▶ | my ($self, $weight, $list) = @_; | ||||||||
|
Subroutine
packing_list: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 165 | 1949 | foreach my $item (@{$self->items}) { | |||||||||
| 166 | 10418 | ${$weight} += $item->weight; | |||||||||
| 167 | 10418 | $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 | ▶ | my $self = shift; | ||||||||
|
Subroutine
packing_instructions: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 186 | return { | ||||||||||
| 187 | 1964 | 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 | 1960 | ▶ | my $self = shift; | ||||||||
|
Subroutine
used_volume: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 208 | 1960 | 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 | 12 | ▶ | no Moose; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 216 | 12 | __PACKAGE__->meta->make_immutable; | |||||||||