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