blib/lib/Kelp/Module/ValidateTiny.pm

filter: Statementstmt 75.0% 51 / 68 Branchbran 40.9% 9 / 22 Conditioncond 73.3% 11 / 15 MC/DCmcdc 44.4% 4 / 9 Subroutinesub 72.7% 8 / 11 Podpod 100% 1 / 1 totaltotal 66.6% 84 / 126 CC 19 SCAR 20.3
CC
19
Cov
68%
DESTROY
12.0
build
11.3
_validate
6.3
CRAP 7.7
↑ Summary
1 {
2 package Kelp::Module::ValidateTiny;
3  
4 5 ▶use Kelp::Base 'Kelp::Module';
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 5 ▶use Validate::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 5 ▶use Class::Load;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 5 ▶use Sub::Install;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 5 our $VERSION = '0.04';
12  
13 # These are Validate::Tiny functions that we
14 # forward into the application namespace
15
16 5 my @forward_ok = qw{
17 filter
18 is_required
19 is_required_if
20 is_equal
21 is_long_between
22 is_long_at_least
23 is_long_at_most
24 is_a
25 is_like
26 is_in
27 };
28  
29 4 ▶sub build {
Subroutine build: called CC=11 SCAR=24.2
Pod documented
30
31 4 my ($self, %args) = @_;
32
33 4 my @import;
34 # Imported from Validate::Tiny?
35 4 ▶ if (%args &&
Branch: 1 branch
BranchTrueFalseTotal
if (%args and exists $args{"subs"}) 3 1 4
Condition66%2/3
%args and exists $args{"subs"}
!ll&&!rl&&r
103
MC/DC50%1/2
%args and exists $args{"subs"}
%args exists $args{"subs"}
Truth table66%2/3
A and B
A= %argsB= exists $args{"subs"}
ABresult
0X0
100
111
36 exists $args{subs}) {
37  
38 3 @import = @{$args{subs}};
39 }
40
41 4 ▶ @import = @forward_ok if (@import && $import[0] eq ':all');
Branch: 1 branch
BranchTrueFalseTotal
if @import and $import[0] eq ":all" 0 4 4
Condition66%2/3
@import and $import[0] eq ":all"
!ll&&!rl&&r
130
MC/DC0%0/2
@import and $import[0] eq ":all"
@import $import[0] eq ":all"
Truth table66%2/3
A and B
A= @importB= $import[0] eq ":all"
ABresult
0X0
100
111
42  
43 # Namespaces to import into (default is our App)
44 # If our App name is Kelp, we are probably running
45 # from a standalone script and our classname is main
46
47 4 my $class = ref $self->app;
48 4 ▶ $class = 'main' if ($class eq 'Kelp');
Branch: 1 branch
BranchTrueFalseTotal
if $class eq "Kelp" 4 0 4
49 4 my @into = ($class);
50  
51 4 ▶ if (%args &&
Branch: 1 branch
BranchTrueFalseTotal
if (%args and exists $args{"into"}) 1 3 4
Condition100%3/3
%args and exists $args{"into"}
!ll&&!rl&&r
121
MC/DC100%2/2
%args and exists $args{"into"}
%args exists $args{"into"}
Truth table100%3/3
A and B
A= %argsB= exists $args{"into"}
ABresult
0X0
100
111
52 exists $args{into}) {
53
54 1 push @into, @{$args{into}};
55 }
56
57 # Import!
58 4 foreach (@into) {
59
60 5 my $class = $_;
61  
62 5 ▶ Class::Load::load_class($class)
Branch: 1 branch
BranchTrueFalseTotal
unless Class::Load::is_class_loaded($class) 0 5 5
63 unless Class::Load::is_class_loaded($class);
64
65 5 foreach (@import) {
66
67 8 Sub::Install::install_sub({
68 code => Validate::Tiny->can($_),
69 from => 'Validate::Tiny',
70 into => $class,
71 });
72 }
73 }
74  
75 # Register a single method - self->validate
76 $self->register(
77 4 validate => \&_validate
78 );
79 }
80  
81 14 ▶sub _validate {
Subroutine _validate: called CC=5 SCAR=18.4
82  
83 14 my $self = shift;
84 14 my $rules = shift;
85 14 my %args = @_;
86
87 # Combine all params
88 # TODO: check if mixed can be avoided
89 # on the Hash::Multivalue "parameters"
90  
91 my $input = {
92 14 %{$self->req->parameters->mixed},
93 14 %{$self->req->named}
94 };
95
96 14 my $result = Validate::Tiny->new($input, $rules);
97
98 return $result if (
99 14 ▶ $result->success || (!(%args && exists $args{on_error}))
Branch: 1 branch
BranchTrueFalseTotal
if $result->success or not %args && exists $args{"on_error"} 12 2 14
Condition66%4/6
$result->success or not %args && exists $args{"on_error"}
l!l&&r!l&&!r
0122
%args && exists $args{'on_error'}
!ll&&!rl&&r
1202
MC/DC33%1/3
$result->success or not %args && exists $args{"on_error"}
$result->success %args exists $args{'on_error'}
Truth table50%2/4
A or not(B && C)
A= $result->successB= %argsC= exists $args{'on_error'}
ABCresult
1XX1
00X1
0101
0110
100 );
101
102 # There are errors and a template is passed
103
104 2 my $data = $result->data;
105 0 $data->{error} = $result->error;
106  
107 0 ▶ if (exists $args{data}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args{"data"}) 0 0 0
108 $data = {
109 %$data,
110 0 %{$args{data}},
111 };
112 }
113
114 return Validate::Tiny::PlackResponse->new(
115 $result,
116 0 $self->res->template($args{on_error}, $data)
117 );
118 }
119  
120 }
121  
122  
123 {
124 5 package Validate::Tiny::PlackResponse;
125  
126 5 ▶ use parent Validate::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
127 5 ▶ use Scalar::Util qw{blessed refaddr};
Subroutine BEGIN: called CC=1 SCAR=0.0
128
129 5 my %_response;
130
131 0 ▶ sub new {
Subroutine new: not called CC=2 SCAR=17.9
132
133 0 my ($class, $obj, $response) = @_;
134
135 0 ▶ die "Incorrect Parent Class. Not an instance of Validate::Tiny"
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($obj) eq "Validate::Tiny" 0 0 0
136 unless blessed($obj) eq 'Validate::Tiny';
137
138 0 $_response{refaddr $obj} = $response;
139  
140 0 bless $obj, $class;
141
142 0 return $obj;
143 }
144
145 0 ▶ sub response {
Subroutine response: not called CC=2 SCAR=17.9
146
147 0 my $self = shift;
148 0 ▶ die "Incorrect Parent Class. Not an instance of Validate::Tiny::PlackResponse"
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($self) eq "Validate::Tiny::PlackResponse" 0 0 0
149 unless blessed($self) eq 'Validate::Tiny::PlackResponse';
150
151 0 return $_response{refaddr $self};
152 }
153
154 0 ▶ sub DESTROY {
Subroutine DESTROY: not called CC=3 SCAR=24.8
155
156 0 my $self = shift;
157  
158 0 my $key = refaddr $self;
159 0 ▶ delete $_response{$key} if exists $_response{$key};
Branch: 1 branch
BranchTrueFalseTotal
if exists $_response{$key} 0 0 0
160
161 0 ▶ $self->SUPER::DESTROY if $self->SUPER::can(DESTROY);
Branch: 1 branch
BranchTrueFalseTotal
if $self->SUPER::can("DESTROY") 0 0 0
162 }
163 }
164  
165 5 1;
166 __END__
↑ Summary