| 1 | package SVG::Estimate::Role::ArgsWithUnits; | ||
| 2 | $SVG::Estimate::Role::ArgsWithUnits::VERSION = '1.0116'; | ||
| 3 | use strict; | ||
| 4 | use Moo::Role; | ||
| 5 | use Ouch; | ||
| 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.0116 | ||
| 14 | |||
| 15 | =head1 METHODS | ||
| 16 | |||
| 17 | =head2 BUILDARGS ( ) | ||
| 18 | |||
| 19 | Validate the set of args from the class's C<args_with_units> 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; |