Calc/Role/Dimensional.pm

filter: Statementstmt 100% 40 / 40 Branchbran 100% 6 / 6 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 7 / 7 Podpod 100% 3 / 3 totaltotal 100% 56 / 56 CC 4 SCAR 7.9
CC
4
Cov
100%
__ANON__
4.0
BEGIN
1.0
BEGIN
1.0
CRAP 2.2
« Calc/Box.pm ↑ Summary Calc/Row.pm »
1 package Box::Calc::Role::Dimensional;
2 15 $Box::Calc::Role::Dimensional::VERSION = '1.0205';
3 15 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 15 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 15 ▶use Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
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.0205
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 15 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 15 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 15 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 15 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 20988 ▶ my ($self) = @_;
Subroutine volume: called CC=1 SCAR=0.0
Pod documented
92 20988 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 20971 ▶ my ($self) = @_;
Subroutine dimensions: called CC=1 SCAR=0.0
Pod documented
103 20971 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 ▶ my ($self) = @_;
Subroutine extent: called CC=1 SCAR=0.0
Pod documented
114 110 return join ',', $self->x, $self->y, $self->z;
115 }
116  
117 around BUILDARGS => sub {
118 132 ▶ my $orig = shift;
Subroutine __ANON__: called CC=4 SCAR=13.9
119 132 my $className = shift;
120 132 my $args;
121 132 ▶ if (ref $_[0] eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0] eq 'HASH') { } 82 50 132
122 82 $args = shift;
123 }
124 else {
125 50 $args = { @_ };
126 }
127  
128 # sort large to small
129 132 my ( $x, $y, $z );
130
131 132 ▶ if ( $args->{no_sort} ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($args->{'no_sort'}) { } 6 126 132
elsif ($args->{'swap_xy'}) { } 44 82 126
132 6 ( $x, $y, $z ) = ( $args->{x}, $args->{y}, $args->{z} );
133 }
134 elsif ( $args->{swap_xy} ) {
135 44 ( $x, $y, $z ) = sort { $b <=> $a } ( $args->{x}, $args->{y}, $args->{z} );
136 44 ( $x, $y ) = ( $y, $x );
137 }
138 else {
139 82 ( $x, $y, $z ) = sort { $b <=> $a } ( $args->{x}, $args->{y}, $args->{z} );
140 }
141  
142 132 $args->{x} = $x;
143 132 $args->{y} = $y;
144 132 $args->{z} = $z;
145 132 return $className->$orig($args);
146 15 };
147  
148 15 1;
« Calc/Box.pm ↑ Summary Calc/Row.pm »