lib/Mojolicious/Plugin/Data/Validate/WithYAML.pm

filter: Statementstmt 100% 50 / 50 Branchbran 100% 18 / 18 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 10 / 10 Podpod 100% 1 / 1 totaltotal 100% 79 / 79 CC 10 SCAR 9.5
CC
10
Cov
100%
_validator
4.0
register
3.0
__ANON__
3.0
CRAP 2.6
↑ Summary
1 package Mojolicious::Plugin::Data::Validate::WithYAML;
2  
3 # ABSTRACT: validate form input with Data::Validate::WithYAML
4  
5 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 5 ▶use parent 'Mojolicious::Plugin';
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 5 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 5 ▶use Data::Validate::WithYAML;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 5 ▶use Mojo::File qw(path);
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 5 our $VERSION = 0.06;
15  
16 5 ▶sub register {
Subroutine register: called CC=3 SCAR=11.0
Pod documented
17 5 my ($self, $app, $config) = @_;
18  
19 5 ▶ $config->{conf_path} = $app->home if !$config->{conf_path};
Branch: 1 branch
BranchTrueFalseTotal
unless $config->{"conf_path"} 2 3 5
20 5 ▶ $config->{no_steps} = 1 if !defined $config->{no_steps};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $config->{"no_steps"} 3 2 5
21  
22 7 ▶ $app->helper( 'validate' => sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
23 7 my ($c, $file, $step) = @_;
24  
25 7 my $validator = _validator( $file, $config );
26 7 my %params = %{ $c->req->params->to_hash };
27 7 ▶ my @args = $step ? $step : ();
Branch: 1 branch
BranchTrueFalseTotal
$step ? : 2 5 7
28 7 my %errors = $validator->validate( @args, %params );
29  
30 my $prefix = exists $config->{error_prefix} ?
31 $config->{error_prefix} :
32 7 ▶ 'ERROR_';
Branch: 1 branch
BranchTrueFalseTotal
exists $config->{'error_prefix'} ? : 6 1 7
33  
34 7 my %prefixed_errors = map{ ( "$prefix$_" => $errors{$_} ) } keys %errors;
35  
36 7 return %prefixed_errors;
37 5 });
38  
39 6 ▶ $app->helper( 'fieldinfo' => sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
40 6 my ($c, $file, $field, $subinfo) = @_;
41  
42 6 my $validator = _validator( $file, $config );
43 3 my $info = $validator->fieldinfo( $field );
44  
45 3 ▶ return if !$info;
Branch: 1 branch
BranchTrueFalseTotal
unless $info 1 2 3
46  
47 2 ▶ return $info if !$subinfo;
Branch: 1 branch
BranchTrueFalseTotal
unless $subinfo 1 1 2
48 1 return $info->{$subinfo};
49 5 });
50 }
51  
52 13 ▶sub _validator {
Subroutine _validator: called CC=4 SCAR=13.9
53 13 my ($file, $config) = @_;
54  
55 13 ▶ if ( !$file ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($file) 6 7 13
56 6 my @caller = caller(3);
57 6 $file = (split /::/, $caller[3])[-1];
58 }
59  
60 13 my $path = path( $config->{conf_path}, $file . '.yml' )->to_string;
61  
62 13 ▶ croak "$path does not exist" if !-e $path;
Branch: 1 branch
BranchTrueFalseTotal
unless -e $path 2 11 13
63  
64 my $validator = Data::Validate::WithYAML->new(
65 $path,
66 11 ▶ %{$config},
Branch: 1 branch
BranchTrueFalseTotal
my $validator = "Data::Validate::WithYAML"->new($path, %{$config;}) or croak($Data::Validate::WithYAML::errstr) 10 1 11
67 ) or croak $Data::Validate::WithYAML::errstr;
68  
69 10 return $validator;
70 }
71  
72 5 1;
73  
74 __END__
↑ Summary