| 1 | package MooX::ValidateSubs; | ||||||||||
| 2 | |||||||||||
| 3 | 10 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 10 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | |||||||||||
| 6 | 10 | ▶ | use MooX::ReturnModifiers; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | 10 | ▶ | use B; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | 10 | our $VERSION = '1.012010'; | |||||||||
| 9 | |||||||||||
| 10 | 19 | ▶ | sub import { | ||||||||
|
Subroutine
import: called CC=2 SCAR=6.9
| |||||||||||
| 11 | 19 | my $target = caller; | |||||||||
| 12 | 19 | my %modifiers = return_modifiers($target, [qw/has with around sub/]); | |||||||||
| 13 | |||||||||||
| 14 | 19 | ▶ | my $raise_context_error = sub { | ||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||
| 15 | 19 | my ($error, $c) = @_; | |||||||||
| 16 | 19 | ▶ | if (ref $error) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 17 | 12 | my $gv = B::svref_2object($c)->GV; | |||||||||
| 18 | 12 | $error->{context}->{file} = $gv->FILE . '::' . $gv->NAME; | |||||||||
| 19 | 12 | $error->{context}->{line} = $gv->LINE; | |||||||||
| 20 | } | ||||||||||
| 21 | 19 | die $error; | |||||||||
| 22 | 19 | }; | |||||||||
| 23 | |||||||||||
| 24 | 14 | ▶ | my $validate_subs = sub { | ||||||||
|
Subroutine
__ANON__: called CC=5 SCAR=16.1
| |||||||||||
| 25 | 14 | my @attr = @_; | |||||||||
| 26 | 14 | ▶ | while (@attr) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 27 | 27 | ▶ | my @names = ref $attr[0] eq 'ARRAY' ? @{ shift @attr } : shift @attr; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 28 | 27 | my $spec = shift @attr; | |||||||||
| 29 | 27 | for my $name (@names) { | |||||||||
| 30 | 33 | my $store_spec = sprintf '%s_spec', $name; | |||||||||
| 31 | 33 | ▶ | $modifiers{has}->( $store_spec => ( is => 'ro', default => sub { $spec } ) ); | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 32 | 33 | ▶ | unless ( $name =~ m/^\+/ ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 33 | $modifiers{around}->( | ||||||||||
| 34 | $name, | ||||||||||
| 35 | 70 | ▶ | sub { | ||||||||
|
Subroutine
__ANON__: called CC=9 SCAR=22.0
| |||||||||||
| 36 | 70 | my ( $orig, $self, @params ) = @_; | |||||||||
| 37 | 70 | my @caller = caller; | |||||||||
| 38 | |||||||||||
| 39 | 70 | ▶ | if (! ref $self) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 40 | 1 | $self = $self->new; | |||||||||
| 41 | } | ||||||||||
| 42 | |||||||||||
| 43 | 70 | my $current_spec = $self->$store_spec; | |||||||||
| 44 | |||||||||||
| 45 | 70 | ▶ | if ( my $param_spec = $current_spec->{params} ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 46 | 61 | @params = eval { $self->_validate_sub( | |||||||||
| 47 | $name, 'params', $param_spec, @params | ||||||||||
| 48 | ) }; | ||||||||||
| 49 | 61 | ▶ | if ($@) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 50 | 15 | $raise_context_error->($@, $orig); | |||||||||
| 51 | } | ||||||||||
| 52 | } | ||||||||||
| 53 | |||||||||||
| 54 | 55 | ▶ | if (my $keys = $current_spec->{keys}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 55 | 6 | ▶ | my $hash = scalar @params > 1 ? { @params } : $params[0]; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 56 | 6 | @params = map { $hash->{$_} } @{ $keys }; | |||||||||
| 57 | } | ||||||||||
| 58 | |||||||||||
| 59 | 55 | @params = $self->$orig(@params); | |||||||||
| 60 | |||||||||||
| 61 | 55 | ▶ | if ( my $param_spec = $current_spec->{returns} ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 62 | 15 | @params = eval { $self->_validate_sub( | |||||||||
| 63 | $name, 'returns', $param_spec, @params | ||||||||||
| 64 | ) }; | ||||||||||
| 65 | 15 | ▶ | if ($@) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 66 | 4 | $raise_context_error->($@, $orig); | |||||||||
| 67 | } | ||||||||||
| 68 | } | ||||||||||
| 69 | |||||||||||
| 70 | 51 | ▶ | return wantarray ? @params : shift @params; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 71 | } | ||||||||||
| 72 | 32 | ); | |||||||||
| 73 | } | ||||||||||
| 74 | } | ||||||||||
| 75 | } | ||||||||||
| 76 | 19 | }; | |||||||||
| 77 | |||||||||||
| 78 | 19 | ▶ | $target->can('_validate_sub') or $modifiers{with}->('MooX::ValidateSubs::Role'); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 79 | |||||||||||
| 80 | 19 | $modifiers{sub}->('validate_subs', $validate_subs); | |||||||||
| 81 | |||||||||||
| 82 | 19 | return 1; | |||||||||
| 83 | } | ||||||||||
| 84 | |||||||||||
| 85 | 10 | 1; | |||||||||
| 86 | |||||||||||
| 87 | __END__ | ||||||||||