File Coverage

blib/lib/Specio/Constraint/Role/IsaType.pm
Criterion Covered Total %
statement 47 47 100.0
branch 13 14 92.8
condition 5 6 83.3
subroutine 11 11 100.0
pod n/a
total 76 78 97.4


line stmt bran cond sub pod time code
1              
2             use strict;
3 4     4   25 use warnings;
  4         10  
  4         109  
4 4     4   18  
  4         12  
  4         179  
5             our $VERSION = '0.48';
6              
7             use Scalar::Util qw( blessed );
8 4     4   21 use Specio::PartialDump qw( partial_dump );
  4         10  
  4         189  
9 4     4   24 use Storable qw( dclone );
  4         7  
  4         153  
10 4     4   23  
  4         8  
  4         135  
11             use Role::Tiny;
12 4     4   27  
  4         9  
  4         25  
13             use Specio::Constraint::Role::Interface;
14 4     4   612 with 'Specio::Constraint::Role::Interface';
  4         10  
  4         1986  
15              
16             {
17             ## no critic (Subroutines::ProtectPrivateSubs)
18             my $attrs = dclone( Specio::Constraint::Role::Interface::_attrs() );
19             ## use critic
20              
21             for my $name (qw( parent _inline_generator )) {
22             $attrs->{$name}{init_arg} = undef;
23             $attrs->{$name}{builder}
24             = $name =~ /^_/ ? '_build' . $name : '_build_' . $name;
25             }
26              
27             $attrs->{class} = {
28             isa => 'ClassName',
29             required => 1,
30             };
31              
32             ## no critic (Subroutines::ProhibitUnusedPrivateSubroutines)
33             return $attrs;
34             }
35 12     12   348 }
36              
37             ## no critic (Subroutines::ProhibitUnusedPrivateSubroutines)
38             my $self = shift;
39             my $generator = shift;
40              
41 8     8   16 my $type = ( split /::/, blessed $self)[-1];
42 8         19 my $class = $self->class;
43             my $allow_classes = $self->_allow_classes;
44 8         45  
45 8         32 unless ( defined $generator ) {
46 8         39 $generator = sub {
47             shift;
48 8 50       27 my $value = shift;
49              
50 184     184   266 return "An undef will never pass an $type check (wants $class)"
51 184         266 unless defined $value;
52              
53 184 100       414 if ( ref $value && !blessed $value) {
54             my $dump = partial_dump($value);
55             return
56 178 100 100     734 "An unblessed reference ($dump) will never pass an $type check (wants $class)";
57 30         88 }
58              
59 30         155 if ( !blessed $value) {
60             return
61             "An empty string will never pass an $type check (wants $class)"
62 148 100       393 unless length $value;
63              
64 58 100       173 if (
65             $value =~ /\A
66             \s*
67 52 100       243 -?[0-9]+(?:\.[0-9]+)?
68             (?:[Ee][\-+]?[0-9]+)?
69             \s*
70             \z/xs
71             ) {
72             return
73             "A number ($value) will never pass an $type check (wants $class)";
74             }
75              
76 33         190 if ( !$allow_classes ) {
77             my $dump = partial_dump($value);
78             return
79 19 100       45 "A plain scalar ($dump) will never pass an $type check (wants $class)";
80 10         32 }
81             }
82 10         96  
83             my $got = blessed $value;
84             $got ||= $value;
85              
86 99         198 return "The $got class is not a subclass of the $class class";
87 99   66     226 };
88             }
89 99         450  
90 8         52 return sub { $generator->( undef, @_ ) };
91             }
92             ## use critic
93 8     184   35  
  184         7003  
94             1;
95              
96             # ABSTRACT: Provides a common implementation for Specio::Constraint::AnyIsa and Specio::Constraint::ObjectIsa
97              
98              
99             =pod
100              
101             =encoding UTF-8
102              
103             =head1 NAME
104              
105             Specio::Constraint::Role::IsaType - Provides a common implementation for Specio::Constraint::AnyIsa and Specio::Constraint::ObjectIsa
106              
107             =head1 VERSION
108              
109             version 0.48
110              
111             =head1 DESCRIPTION
112              
113             See L<Specio::Constraint::AnyIsa> and L<Specio::Constraint::ObjectIsa> for
114             details.
115              
116             =head1 SUPPORT
117              
118             Bugs may be submitted at L<https://github.com/houseabsolute/Specio/issues>.
119              
120             =head1 SOURCE
121              
122             The source code repository for Specio can be found at L<https://github.com/houseabsolute/Specio>.
123              
124             =head1 AUTHOR
125              
126             Dave Rolsky <autarch@urth.org>
127              
128             =head1 COPYRIGHT AND LICENSE
129              
130             This software is Copyright (c) 2012 - 2022 by Dave Rolsky.
131              
132             This is free software, licensed under:
133              
134             The Artistic License 2.0 (GPL Compatible)
135              
136             The full text of the license can be found in the
137             F<LICENSE> file included with this distribution.
138              
139             =cut