File Coverage

blib/lib/Box/Calc/Role/Dimensional.pm
Criterion Covered Total %
statement 15 15 100.0
branch n/a
condition n/a
subroutine 6 6 100.0
pod 3 3 100.0
total 24 24 100.0


line stmt bran cond sub pod time code
1             package Box::Calc::Role::Dimensional;
2             $Box::Calc::Role::Dimensional::VERSION = '1.0206';
3 16     16   11107 use strict;
  16         37  
  16         552  
4 16     16   83 use warnings;
  16         40  
  16         446  
5 16     16   3596 use Moose::Role;
  16         36919  
  16         89  
6              
7             =head1 NAME
8              
9             Box::Calc::Role::Dimensional - Role to add standard dimensions to objects.
10              
11              
12             =head1 VERSION
13              
14             version 1.0206
15              
16             =head2 SYNOPSIS
17              
18             The x, y, and z attributes are first sorted from largest to smallest before creating the object. So you can insert them in any order. x=3, y=9, z=1 would become x=r9, y=3, z=1.
19              
20             #----------#
21             | |
22             | |
23             | Y |
24             | |
25             | |
26             | X |
27             #----------#
28              
29             Z is from bottom up
30              
31              
32             =head1 METHODS
33              
34             This role installs these methods:
35              
36             =head2 x
37              
38             Returns the largest side of an object.
39              
40             =cut
41              
42             has x => (
43             is => 'rw',
44             required => 1,
45             isa => 'Num',
46             );
47              
48             =head2 y
49              
50             Returns the middle side of an object.
51              
52             =cut
53              
54             has y => (
55             is => 'rw',
56             required => 1,
57             isa => 'Num',
58             );
59              
60             =head2 z
61              
62             Returns the shortest side of an object.
63              
64             =cut
65              
66             has z => (
67             is => 'rw',
68             required => 1,
69             isa => 'Num',
70             );
71              
72             =head2 weight
73              
74             Returns the weight of an object.
75              
76             =cut
77              
78             has weight => (
79             is => 'ro',
80             isa => 'Num',
81             required => 1,
82             );
83              
84             =head2 volume
85              
86             Returns the result of multiplying x, y, and z.
87              
88             =cut
89              
90             sub volume {
91 31396     31396 1 40688 my ($self) = @_;
92 31396         598576 return $self->x * $self->y * $self->z;
93             }
94              
95             =head2 dimensions
96              
97             Returns an array reference containing x, y, and z.
98              
99             =cut
100              
101             sub dimensions {
102 31348     31348 1 38259 my ($self) = @_;
103 31348         600360 return [ $self->x, $self->y, $self->z, ];
104             }
105              
106             =head2 extent
107              
108             Returns a string of C<x,y,z>. Good for comparing whether two items are dimensionally similar.
109              
110             =cut
111              
112             sub extent {
113 110     110 1 148 my ($self) = @_;
114 110         2333 return join ',', $self->x, $self->y, $self->z;
115             }
116              
117             around BUILDARGS => sub {
118             my $orig = shift;
119             my $className = shift;
120             my $args;
121             if (ref $_[0] eq 'HASH') {
122             $args = shift;
123             }
124             else {
125             $args = { @_ };
126             }
127              
128             # sort large to small
129             my ( $x, $y, $z );
130            
131             if ( $args->{no_sort} ) {
132             ( $x, $y, $z ) = ( $args->{x}, $args->{y}, $args->{z} );
133             }
134             elsif ( $args->{swap_xy} ) {
135             ( $x, $y, $z ) = sort { $b <=> $a } ( $args->{x}, $args->{y}, $args->{z} );
136             ( $x, $y ) = ( $y, $x );
137             }
138             else {
139             ( $x, $y, $z ) = sort { $b <=> $a } ( $args->{x}, $args->{y}, $args->{z} );
140             }
141              
142             $args->{x} = $x;
143             $args->{y} = $y;
144             $args->{z} = $z;
145             return $className->$orig($args);
146             };
147              
148             1;