File Coverage

lib/Pcore/Core/CLI/Type.pm
Criterion Covered Total %
statement 6 31 19.3
branch 0 26 0.0
condition n/a
subroutine 2 3 66.6
pod n/a
total 8 60 13.3


line stmt bran cond sub pod time code
1             package Pcore::Core::CLI::Type;
2              
3 5     5   3146 use Pcore -role, -const;
  5         15  
  5         46  
4 5     5   61 use Pcore::Util::Scalar qw[is_plain_arrayref is_plain_hashref];
  5         15  
  5         53  
5              
6             const our $TYPE => {
7             Str => sub ($val) {
8             return Str->check($val);
9             },
10             Bool => sub ($val) {
11             return Bool->check($val);
12             },
13             Int => sub ($val) {
14             return Int->check($val);
15             },
16             PositiveInt => sub ($val) {
17             return PositiveInt->check($val);
18             },
19             PositiveOrZeroInt => sub ($val) {
20             return PositiveOrZeroInt->check($val);
21             },
22             Num => sub ($val) {
23             return Num->check($val);
24             },
25             Path => sub ($val) {
26             return -e $val;
27             },
28             Dir => sub ($val) {
29             return -d $val;
30             },
31             File => sub ($val) {
32             return -f $val;
33             },
34             };
35              
36 0     0     sub _validate_isa ( $self, @ ) {
  0            
  0            
37 0 0         my $vals = is_plain_arrayref $_[1] ? $_[1] : is_plain_hashref $_[1] ? [ values $_[1]->%* ] : [ $_[1] ];
    0          
38              
39 0           my $isa_ref = ref $self->isa;
40              
41 0           for my $val ( $vals->@* ) {
42 0 0         if ( !$isa_ref ) {
    0          
    0          
    0          
43 0 0         return qq[value "$val" is not a ] . uc $self->isa if !$TYPE->{ $self->isa }->($val);
44             }
45             elsif ( $isa_ref eq 'CODE' ) {
46 0 0         if ( my $error_msg = $self->isa->($val) ) {
47 0           return $error_msg;
48             }
49             }
50             elsif ( $isa_ref eq 'Regexp' ) {
51 0 0         return qq[value "$val" should match regexp ] . $self->isa if $val !~ $self->isa;
52             }
53             elsif ( $isa_ref eq 'ARRAY' ) {
54 0           my $possible_val = [];
55              
56 0           for ( $self->isa->@* ) {
57 0 0         if ( index( $_, $val, 0 ) == 0 ) {
58 0 0         if ( length == length $val ) {
59              
60             # select current value if matched completely
61 0           $possible_val = [$_];
62              
63 0           last;
64             }
65              
66 0           push $possible_val->@*, $_;
67             }
68             }
69              
70 0 0         if ( !$possible_val->@* ) {
    0          
71 0           return qq[value "$val" should be one of the: ] . join q[, ], map {qq["$_"]} $self->isa->@*;
  0            
72             }
73             elsif ( $possible_val->@* > 1 ) {
74 0           return qq[value "$val" is ambigous, did you mean: ] . join q[, ], map {qq["$_"]} $possible_val->@*;
  0            
75             }
76             else {
77 0           $val = $possible_val->[0];
78             }
79             }
80             }
81              
82 0           return;
83             }
84              
85             1;
86             ## -----SOURCE FILTER LOG BEGIN-----
87             ##
88             ## PerlCritic profile "pcore-script" policy violations:
89             ## +------+----------------------+----------------------------------------------------------------------------------------------------------------+
90             ## | Sev. | Lines | Policy |
91             ## |======+======================+================================================================================================================|
92             ## | 3 | 36 | Subroutines::ProhibitUnusedPrivateSubroutines - Private subroutine/method '_validate_isa' declared but not |
93             ## | | | used |
94             ## +------+----------------------+----------------------------------------------------------------------------------------------------------------+
95             ##
96             ## -----SOURCE FILTER LOG END-----
97             __END__
98             =pod
99              
100             =encoding utf8
101              
102             =head1 NAME
103              
104             Pcore::Core::CLI::Type
105              
106             =head1 SYNOPSIS
107              
108             =head1 DESCRIPTION
109              
110             =head1 ATTRIBUTES
111              
112             =head1 METHODS
113              
114             =head1 SEE ALSO
115              
116             =cut