| 1 | package MooseX::Role::Validatable; | ||||||||||
| 2 | |||||||||||
| 3 | 2 | ▶ | use Moose::Role; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 2 | ▶ | use MooseX::Role::Validatable::Error; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | |||||||||||
| 6 | 2 | our $VERSION = '0.12'; | |||||||||
| 7 | |||||||||||
| 8 | 2 | ▶ | use Class::Load qw/load_class/; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | 2 | ▶ | use Carp qw(confess); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | 2 | ▶ | use Scalar::Util qw/blessed/; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 11 | |||||||||||
| 12 | has '_init_errors' => ( | ||||||||||
| 13 | is => 'ro', | ||||||||||
| 14 | isa => 'ArrayRef', | ||||||||||
| 15 | init_arg => undef, | ||||||||||
| 16 | 2 | ▶ | default => sub { return [] }, | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 17 | 2 | ); | |||||||||
| 18 | has '_validation_errors' => ( | ||||||||||
| 19 | is => 'ro', | ||||||||||
| 20 | isa => 'ArrayRef', | ||||||||||
| 21 | init_arg => undef, | ||||||||||
| 22 | 2 | ▶ | default => sub { return [] }, | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 23 | 2 | ); | |||||||||
| 24 | |||||||||||
| 25 | has 'error_class' => ( | ||||||||||
| 26 | is => 'ro', | ||||||||||
| 27 | 2 | ▶ | default => sub { 'MooseX::Role::Validatable::Error' }, | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 28 | 0 | ▶ | trigger => sub { | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 29 | 0 | my $self = shift; | |||||||||
| 30 | 0 | load_class($self->error_class); | |||||||||
| 31 | 2 | }); | |||||||||
| 32 | |||||||||||
| 33 | 2 | has validation_methods => ( | |||||||||
| 34 | is => 'ro', | ||||||||||
| 35 | isa => 'ArrayRef[Str]', | ||||||||||
| 36 | lazy_build => 1, | ||||||||||
| 37 | ); | ||||||||||
| 38 | |||||||||||
| 39 | 1 | ▶ | sub _build_validation_methods { | ||||||||
|
Subroutine
_build_validation_methods: called CC=1 SCAR=0.0
| |||||||||||
| 40 | 1 | my $self = shift; | |||||||||
| 41 | 1 | return [grep { $_ =~ /^_validate_/ } ($self->meta->get_all_method_names)]; | |||||||||
| 42 | } | ||||||||||
| 43 | |||||||||||
| 44 | 3 | ▶ | sub all_errors { | ||||||||
|
Subroutine
all_errors: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 45 | 3 | my $self = shift; | |||||||||
| 46 | 3 | return (@{$self->{_init_errors}}, @{$self->{_validation_errors}}); | |||||||||
| 47 | } | ||||||||||
| 48 | |||||||||||
| 49 | 1 | ▶ | sub all_init_errors { | ||||||||
|
Subroutine
all_init_errors: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 50 | 1 | return @{(shift)->{_init_errors}}; | |||||||||
| 51 | } | ||||||||||
| 52 | |||||||||||
| 53 | 0 | ▶ | sub all_validation_errors { | ||||||||
|
Subroutine
all_validation_errors: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 54 | 0 | return @{(shift)->{_validation_errors}}; | |||||||||
| 55 | } | ||||||||||
| 56 | |||||||||||
| 57 | 2 | ▶ | sub passes_validation { | ||||||||
|
Subroutine
passes_validation: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||
| 58 | 2 | my $self = shift; | |||||||||
| 59 | 2 | my @all_errors = $self->all_errors; | |||||||||
| 60 | 2 | ▶ | return (scalar @all_errors) ? 0 : 1; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 61 | } | ||||||||||
| 62 | |||||||||||
| 63 | 0 | ▶ | sub should_alert { | ||||||||
|
Subroutine
should_alert: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||
| 64 | 0 | my $self = shift; | |||||||||
| 65 | 0 | ▶ | return (grep { $_->alert } ($self->all_errors)) ? 1 : 0; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 66 | } | ||||||||||
| 67 | |||||||||||
| 68 | 1 | ▶ | sub confirm_validity { | ||||||||
|
Subroutine
confirm_validity: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 69 | 1 | my $self = shift; | |||||||||
| 70 | $self->{_validation_errors} = [ | ||||||||||
| 71 | 2 | map { $self->_errfilter($_) } | |||||||||
| 72 | 1 | map { $self->$_ } @{$self->validation_methods}]; | |||||||||
| 73 | 1 | return $self->passes_validation; | |||||||||
| 74 | } | ||||||||||
| 75 | |||||||||||
| 76 | 1 | ▶ | sub add_errors { | ||||||||
|
Subroutine
add_errors: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 77 | 1 | my ($self, @errors) = @_; | |||||||||
| 78 | 1 | push @{$self->{_init_errors}}, map { $self->_errfilter($_) } @errors; | |||||||||
| 79 | 1 | return scalar @errors; | |||||||||
| 80 | } | ||||||||||
| 81 | |||||||||||
| 82 | 2 | ▶ | sub initialized_correctly { | ||||||||
|
Subroutine
initialized_correctly: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 83 | 2 | my $self = shift; | |||||||||
| 84 | 2 | ▶ | return (@{$self->{_init_errors}}) ? 0 : 1; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 85 | } | ||||||||||
| 86 | |||||||||||
| 87 | 0 | ▶ | sub all_errors_by_severity { | ||||||||
|
Subroutine
all_errors_by_severity: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 88 | 0 | my $self = shift; | |||||||||
| 89 | 0 | return (sort { $b->severity <=> $a->severity } ($self->all_errors)); | |||||||||
| 90 | } | ||||||||||
| 91 | |||||||||||
| 92 | 0 | ▶ | sub primary_validation_error { | ||||||||
|
Subroutine
primary_validation_error: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||
| 93 | 0 | my $self = shift; | |||||||||
| 94 | |||||||||||
| 95 | 0 | my @errors = $self->all_errors_by_severity; | |||||||||
| 96 | 0 | ▶ | return unless @errors; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 97 | |||||||||||
| 98 | # We may wish to do something with perm v. transient here at some point. | ||||||||||
| 99 | 0 | return $errors[0]; | |||||||||
| 100 | } | ||||||||||
| 101 | |||||||||||
| 102 | 3 | ▶ | sub _errfilter { | ||||||||
|
Subroutine
_errfilter: called CC=6 SCAR=18.2
| |||||||||||
| 103 | 3 | my ($self, $error) = @_; | |||||||||
| 104 | 3 | ▶ | return $error if blessed($error); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 105 | |||||||||||
| 106 | 3 | ▶ | $error = {message => $error} unless ref($error); # when it's a string | ||||||||
|
Branch: 1 branch
| |||||||||||
| 107 | |||||||||||
| 108 | 3 | ▶ | confess "Cannot add validation error which is not blessed nor hashref" unless ref($error) eq 'HASH'; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 109 | 3 | ▶ | $error->{message_to_client} = $error->{message} unless exists $error->{message_to_client}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 110 | 3 | ▶ | $error->{set_by} = caller(1) unless exists $error->{set_by}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 111 | 3 | return $self->error_class->new($error); | |||||||||
| 112 | } | ||||||||||
| 113 | |||||||||||
| 114 | 2 | ▶ | no Moose::Role; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 115 | |||||||||||
| 116 | 2 | 1; | |||||||||
| 117 | __END__ | ||||||||||