line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
#!/usr/bin/perl |
2
|
|
|
|
|
|
|
package Sidekick::Check; |
3
|
|
|
|
|
|
|
{ |
4
|
|
|
|
|
|
|
$Sidekick::Check::VERSION = '0.0.1'; |
5
|
|
|
|
|
|
|
} |
6
|
|
|
|
|
|
|
|
7
|
1
|
|
|
1
|
|
17361
|
use v5.10; |
|
1
|
|
|
|
|
5
|
|
|
1
|
|
|
|
|
59
|
|
8
|
|
|
|
|
|
|
|
9
|
1
|
|
|
1
|
|
7
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
73
|
|
10
|
1
|
|
|
1
|
|
7
|
use warnings; |
|
1
|
|
|
|
|
7
|
|
|
1
|
|
|
|
|
41
|
|
11
|
|
|
|
|
|
|
|
12
|
1
|
|
|
1
|
|
551
|
use Module::Pluggable::Object (); |
|
1
|
|
|
|
|
7005
|
|
|
1
|
|
|
|
|
189
|
|
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
my $package = __PACKAGE__; |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
# for internal functions |
17
|
|
|
|
|
|
|
my $parse_args; |
18
|
|
|
|
|
|
|
|
19
|
0
|
|
|
0
|
1
|
|
sub new { return $package; } |
20
|
|
|
|
|
|
|
|
21
|
0
|
|
|
0
|
1
|
|
sub is { return 0 + !( shift->errors( @_ ) ); } |
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
sub errors { |
24
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
25
|
0
|
|
|
|
|
|
my $value = shift; |
26
|
0
|
|
|
|
|
|
my @checks = @_; |
27
|
0
|
|
|
|
|
|
my @errors; |
28
|
|
|
|
|
|
|
|
29
|
0
|
|
|
|
|
|
for my $check ( @checks ) { |
30
|
0
|
|
|
|
|
|
my ($method, $name, @args) = $check->$parse_args( $value ); |
31
|
0
|
0
|
|
|
|
|
next if $self->$method( @args ); |
32
|
0
|
|
|
|
|
|
push @errors, $name; |
33
|
0
|
0
|
|
|
|
|
last unless wantarray; |
34
|
|
|
|
|
|
|
} |
35
|
|
|
|
|
|
|
|
36
|
0
|
0
|
|
|
|
|
return wantarray ? @errors : shift @errors; |
37
|
|
|
|
|
|
|
} |
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
# dinamically add is_* methods based on plugins |
40
|
|
|
|
|
|
|
my $finder = Module::Pluggable::Object->new( |
41
|
|
|
|
|
|
|
'package' => $package, 'require' => 1, |
42
|
|
|
|
|
|
|
); |
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
{ |
45
|
1
|
|
|
1
|
|
7
|
no strict 'refs'; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
576
|
|
46
|
|
|
|
|
|
|
for my $plugin ( $finder->plugins ) { |
47
|
|
|
|
|
|
|
my $check = $plugin->can('check') |
48
|
|
|
|
|
|
|
|| next; |
49
|
|
|
|
|
|
|
my $method = join( '::', ( split '::', lc $plugin )[3,] ); |
50
|
|
|
|
|
|
|
*{ sprintf '%s::is_%s', $package, $method } = $check; |
51
|
|
|
|
|
|
|
} |
52
|
|
|
|
|
|
|
} |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
# internal functions |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
$parse_args = sub { |
57
|
|
|
|
|
|
|
my $method = shift; |
58
|
|
|
|
|
|
|
my $value = shift; |
59
|
|
|
|
|
|
|
my $name = shift; |
60
|
|
|
|
|
|
|
my @args; |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
given ( ref $method ) { |
63
|
|
|
|
|
|
|
when ( 'ARRAY' ) { |
64
|
|
|
|
|
|
|
($method, @args) = @{ $method }; |
65
|
|
|
|
|
|
|
return $method->$parse_args( $value, undef, @args ); |
66
|
|
|
|
|
|
|
} |
67
|
|
|
|
|
|
|
when ( 'HASH' ) { |
68
|
|
|
|
|
|
|
($method, $name, my $args) = @{ $method }{ qw(is name args) }; |
69
|
|
|
|
|
|
|
return $method->$parse_args( $value, $name, $args || () ); |
70
|
|
|
|
|
|
|
} |
71
|
|
|
|
|
|
|
when ( 'CODE' ) { } |
72
|
|
|
|
|
|
|
when ( '' ) { |
73
|
|
|
|
|
|
|
$name ||= $method; |
74
|
|
|
|
|
|
|
$method = join( '_', 'is', $method ); |
75
|
|
|
|
|
|
|
} |
76
|
|
|
|
|
|
|
default { |
77
|
|
|
|
|
|
|
die 'unssupported' |
78
|
|
|
|
|
|
|
} |
79
|
|
|
|
|
|
|
} |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
return ( $method, $name, $value, @args ); |
82
|
|
|
|
|
|
|
}; |
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
1; |
85
|
|
|
|
|
|
|
# ABSTRACT: Plugin based validation mechanism |
86
|
|
|
|
|
|
|
# vim:ts=4:sw=4:syn=perl |
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
__END__ |