| 1 | package Box::Calc::Role::Dimensional; | ||||||||||||||
| 2 | 14 | $Box::Calc::Role::Dimensional::VERSION = '1.0201'; | |||||||||||||
| 3 | 14 | ▶ | use strict; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 4 | 14 | ▶ | use warnings; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 5 | 14 | ▶ | 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.0201 | ||||||||||||||
| 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 | 14 | 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 | 14 | 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 | 14 | 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 | 14 | 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 | 14 | has volume => ( | |||||||||||||
| 91 | is => 'ro', | ||||||||||||||
| 92 | isa => 'Num', | ||||||||||||||
| 93 | required => 1, | ||||||||||||||
| 94 | ); | ||||||||||||||
| 95 | |||||||||||||||
| 96 | =head2 dimensions | ||||||||||||||
| 97 | |||||||||||||||
| 98 | Returns an array reference containing x, y, and z. | ||||||||||||||
| 99 | |||||||||||||||
| 100 | =cut | ||||||||||||||
| 101 | |||||||||||||||
| 102 | 14 | has dimensions => ( | |||||||||||||
| 103 | is => 'ro', | ||||||||||||||
| 104 | isa => 'ArrayRef', | ||||||||||||||
| 105 | required => 1, | ||||||||||||||
| 106 | ); | ||||||||||||||
| 107 | |||||||||||||||
| 108 | =head2 extent | ||||||||||||||
| 109 | |||||||||||||||
| 110 | Returns a string of C<x,y,z>. Good for comparing whether two items are dimensionally similar. | ||||||||||||||
| 111 | |||||||||||||||
| 112 | =cut | ||||||||||||||
| 113 | |||||||||||||||
| 114 | 14 | has extent => ( | |||||||||||||
| 115 | is => 'ro', | ||||||||||||||
| 116 | isa => 'Str', | ||||||||||||||
| 117 | required => 1, | ||||||||||||||
| 118 | ); | ||||||||||||||
| 119 | |||||||||||||||
| 120 | around BUILDARGS => sub { | ||||||||||||||
| 121 | 117 | ▶ | my $orig = shift; | ||||||||||||
|
Subroutine
__ANON__: called CC=4 SCAR=13.9
| |||||||||||||||
| 122 | 117 | my $className = shift; | |||||||||||||
| 123 | 117 | my $args; | |||||||||||||
| 124 | 117 | ▶ | if (ref $_[0] eq 'HASH') { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 125 | 68 | $args = shift; | |||||||||||||
| 126 | } | ||||||||||||||
| 127 | else { | ||||||||||||||
| 128 | 49 | $args = { @_ }; | |||||||||||||
| 129 | } | ||||||||||||||
| 130 | |||||||||||||||
| 131 | # sort large to small | ||||||||||||||
| 132 | 117 | my ( $x, $y, $z ); | |||||||||||||
| 133 | |||||||||||||||
| 134 | 117 | ▶ | if ( $args->{no_sort} ) { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 135 | 6 | ( $x, $y, $z ) = ( $args->{x}, $args->{y}, $args->{z} ); | |||||||||||||
| 136 | } | ||||||||||||||
| 137 | elsif ( $args->{swap_xy} ) { | ||||||||||||||
| 138 | 31 | ( $x, $y, $z ) = sort { $b <=> $a } ( $args->{x}, $args->{y}, $args->{z} ); | |||||||||||||
| 139 | 31 | ( $x, $y ) = ( $y, $x ); | |||||||||||||
| 140 | } | ||||||||||||||
| 141 | else { | ||||||||||||||
| 142 | 80 | ( $x, $y, $z ) = sort { $b <=> $a } ( $args->{x}, $args->{y}, $args->{z} ); | |||||||||||||
| 143 | } | ||||||||||||||
| 144 | |||||||||||||||
| 145 | 117 | $args->{x} = $x; | |||||||||||||
| 146 | 117 | $args->{y} = $y; | |||||||||||||
| 147 | 117 | $args->{z} = $z; | |||||||||||||
| 148 | 117 | $args->{volume} = $x * $y * $z; | |||||||||||||
| 149 | 117 | $args->{dimensions} = [$x, $y, $z]; | |||||||||||||
| 150 | 117 | $args->{extent} = join(',', $x, $y, $z); | |||||||||||||
| 151 | 117 | return $className->$orig($args); | |||||||||||||
| 152 | 14 | }; | |||||||||||||
| 153 | |||||||||||||||
| 154 | 14 | 1; | |||||||||||||