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 3     3   22 use strict;
  3         6  
  3         93  
4 3     3   13 use warnings;
  3         7  
  3         152  
5              
6             our $VERSION = '0.46';
7              
8 3     3   15 use Scalar::Util qw( blessed );
  3         6  
  3         142  
9 3     3   18 use Specio::PartialDump qw( partial_dump );
  3         5  
  3         156  
10 3     3   18 use Storable qw( dclone );
  3         6  
  3         123  
11              
12 3     3   18 use Role::Tiny;
  3         6  
  3         19  
13              
14 3     3   595 use Specio::Constraint::Role::Interface;
  3         7  
  3         1663  
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 10     10   26 return $attrs;
36             }
37             }
38              
39             ## no critic (Subroutines::ProhibitUnusedPrivateSubroutines)
40             sub _wrap_message_generator {
41 7     7   17 my $self = shift;
42 7         25 my $generator = shift;
43              
44 7         50 my $type = ( split /::/, blessed $self)[-1];
45 7         30 my $class = $self->class;
46 7         45 my $allow_classes = $self->_allow_classes;
47              
48 7 50       29 unless ( defined $generator ) {
49             $generator = sub {
50 184     184   315 shift;
51 184         374 my $value = shift;
52              
53 184 100       556 return "An undef will never pass an $type check (wants $class)"
54             unless defined $value;
55              
56 178 100 100     863 if ( ref $value && !blessed $value) {
57 30         119 my $dump = partial_dump($value);
58             return
59 30         204 "An unblessed reference ($dump) will never pass an $type check (wants $class)";
60             }
61              
62 148 100       454 if ( !blessed $value) {
63             return
64 58 100       221 "An empty string will never pass an $type check (wants $class)"
65             unless length $value;
66              
67 52 100       283 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         274 "A number ($value) will never pass an $type check (wants $class)";
77             }
78              
79 19 100       61 if ( !$allow_classes ) {
80 10         42 my $dump = partial_dump($value);
81             return
82 10         87 "A plain scalar ($dump) will never pass an $type check (wants $class)";
83             }
84             }
85              
86 99         266 my $got = blessed $value;
87 99   66     265 $got ||= $value;
88              
89 99         563 return "The $got class is not a subclass of the $class class";
90 7         54 };
91             }
92              
93 7     184   37 return sub { $generator->( undef, @_ ) };
  184         8882  
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.46
114              
115             =head1 DESCRIPTION
116              
117             See L<Specio::Constraint::AnyIsa> and L<Specio::Constraint::ObjectIsa> for details.
118              
119             =head1 SUPPORT
120              
121             Bugs may be submitted at L<https://github.com/houseabsolute/Specio/issues>.
122              
123             I am also usually active on IRC as 'autarch' on C<irc://irc.perl.org>.
124              
125             =head1 SOURCE
126              
127             The source code repository for Specio can be found at L<https://github.com/houseabsolute/Specio>.
128              
129             =head1 AUTHOR
130              
131             Dave Rolsky <autarch@urth.org>
132              
133             =head1 COPYRIGHT AND LICENSE
134              
135             This software is Copyright (c) 2012 - 2020 by Dave Rolsky.
136              
137             This is free software, licensed under:
138              
139             The Artistic License 2.0 (GPL Compatible)
140              
141             The full text of the license can be found in the
142             F<LICENSE> file included with this distribution.
143              
144             =cut