File Coverage

lib/SVG/Estimate/Role/ArgsWithUnits.pm
Criterion Covered Total %
statement 9 9 100.0
branch n/a
condition n/a
subroutine 3 3 100.0
pod n/a
total 12 12 100.0


line stmt bran cond sub pod time code
1             package SVG::Estimate::Role::ArgsWithUnits;
2             $SVG::Estimate::Role::ArgsWithUnits::VERSION = '1.0113';
3 15     15   6193 use strict;
  15         28  
  15         371  
4 15     15   59 use Moo::Role;
  15         25  
  15         76  
5 15     15   7003 use Ouch;
  15         8512  
  15         125  
6              
7             =head1 NAME
8              
9             SVG::Estimate::Role::ArgsWithUnits - Validate a list of arguments that could contain units
10              
11             =head1 VERSION
12              
13             version 1.0113
14              
15             =head1 METHODS
16              
17             =head2 BUILDARGS ( )
18              
19             Validate the set of args from the class's C method to make sure they don't have units
20              
21             =cut
22              
23             around BUILDARGS => sub {
24             my $orig = shift;
25             my $class = shift;
26             my @args = @_;
27             my $args = @args % 2 ? $args[0] : { @args };
28             foreach my $param ($class->args_with_units) {
29             if ($args->{$param} =~ /\d\D+$/) {
30             ouch 'units detected', "$param is not allowed to have units", $args->{$param};
31             }
32             }
33             ##Validate before the arguments are used
34             return $class->$orig($args);
35             };
36              
37              
38             1;