File Coverage

blib/lib/Box/Calc/Role/Container.pm
Criterion Covered Total %
statement 11 15 73.3
branch n/a
condition n/a
subroutine 4 6 66.6
pod 3 3 100.0
total 18 24 75.0


line stmt bran cond sub pod time code
1             package Box::Calc::Role::Container;
2             $Box::Calc::Role::Container::VERSION = '1.0206';
3 12     12   8112 use strict;
  12         26  
  12         347  
4 12     12   66 use warnings;
  12         21  
  12         330  
5 12     12   3404 use Moose::Role;
  12         34951  
  12         210  
6             with 'Box::Calc::Role::Dimensional';
7              
8             =head1 NAME
9              
10             Box::Calc::Role::Container - Extends the L<Box::Calc::Role::Dimensional> role to include outer dimensions.
11              
12              
13             =head1 VERSION
14              
15             version 1.0206
16              
17             =head1 METHODS
18              
19             This role installs these methods:
20              
21             =head2 outer_x
22              
23             Returns the outside dimension of largest side of an object.
24              
25             =cut
26              
27             has outer_x => (
28             is => 'rw',
29             isa => 'Num',
30             required => 1,
31             );
32              
33             =head2 outer_y
34              
35             Returns the outside dimension of the middle side of an object.
36              
37             =cut
38              
39             has outer_y => (
40             is => 'rw',
41             isa => 'Num',
42             required => 1,
43             );
44              
45             =head2 outer_z
46              
47             Returns the outside dimension of the shortest side of an object.
48              
49             =cut
50              
51             has outer_z => (
52             is => 'rw',
53             isa => 'Num',
54             required => 1,
55             );
56              
57             =head2 outer_volume
58              
59             Returns the result of multiplying outer_x, outer_y, and outer_z.
60              
61             =cut
62              
63              
64             sub outer_volume {
65 0     0 1 0 my ($self) = @_;
66 0         0 return $self->outer_x * $self->outer_y * $self->outer_z;
67             }
68              
69             =head2 outer_dimensions
70              
71             Returns an array reference containing outer_x, outer_y, and outer_z.
72              
73             =cut
74              
75             sub outer_dimensions {
76 2     2 1 10 my ($self) = @_;
77 2         56 return [ $self->outer_x, $self->outer_y, $self->outer_z, ];
78             }
79              
80             =head2 outer_extent
81              
82             Returns a string of C<outer_x,outer_y,outer_z>. Good for comparing whether two items are dimensionally similar.
83              
84             =cut
85              
86              
87             sub outer_extent {
88 0     0 1   my ($self) = @_;
89 0           return join ',', $self->outer_x, $self->outer_y, $self->outer_z;
90             }
91              
92             =head2 max_weight
93              
94             The max weight of the items including the container. Defaults to 1000.
95              
96             =cut
97              
98             has max_weight => (
99             is => 'ro',
100             isa => 'Num',
101             default => 1000,
102             );
103              
104             around BUILDARGS => sub {
105             my $orig = shift;
106             my $className = shift;
107             my $args;
108             if (ref $_[0] eq 'HASH') {
109             $args = shift;
110             }
111             else {
112             $args = { @_ };
113             }
114              
115             $args->{outer_x} ||= $args->{x};
116             $args->{outer_y} ||= $args->{y};
117             $args->{outer_z} ||= $args->{z};
118              
119             # sort large to small
120             my ( $x, $y, $z ) = sort { $b <=> $a } ( $args->{outer_x}, $args->{outer_y}, $args->{outer_z} );
121            
122             $args->{outer_x} = $x;
123             $args->{outer_y} = $y;
124             $args->{outer_z} = $z;
125             return $className->$orig($args);
126             };
127              
128             1;