File Coverage

blib/lib/Specio/Constraint/AnyCan.pm
Criterion Covered Total %
statement 30 30 100.0
branch n/a
condition n/a
subroutine 12 12 100.0
pod n/a
total 42 42 100.0


line stmt bran cond sub pod time code
1              
2             use strict;
3 2     2   13 use warnings;
  2         4  
  2         56  
4 2     2   9  
  2         4  
  2         83  
5             our $VERSION = '0.48';
6              
7             use List::Util 1.33 ();
8 2     2   9 use Role::Tiny::With;
  2         37  
  2         34  
9 2     2   10 use Scalar::Util ();
  2         3  
  2         83  
10 2     2   10 use Specio::Helpers qw( perlstring );
  2         2  
  2         34  
11 2     2   7 use Specio::Library::Builtins;
  2         4  
  2         66  
12 2     2   10 use Specio::OO;
  2         3  
  2         12  
13 2     2   11  
  2         4  
  2         90  
14             use Specio::Constraint::Role::CanType;
15 2     2   12 with 'Specio::Constraint::Role::CanType';
  2         3  
  2         375  
16              
17             {
18             my $Defined = t('Defined');
19             }
20 2     2   13  
21             {
22             my $_inline_generator = sub {
23             my $self = shift;
24             my $val = shift;
25              
26             my $methods = join ', ', map { perlstring($_) } @{ $self->methods };
27             return sprintf( <<'EOF', $val, $methods );
28             (
29             do {
30             # We need to assign this since if it's something like $_[0] then
31             # inside the all block @_ gets redefined and we can no longer get at
32             # the value.
33             my $v = %s;
34             (
35             Scalar::Util::blessed($v) || (
36             defined($v)
37             && !ref($v)
38             && length($v)
39             && $v !~ /\A
40             \s*
41             -?[0-9]+(?:\.[0-9]+)?
42             (?:[Ee][\-+]?[0-9]+)?
43             \s*
44             \z/xs
45              
46             # Passing a GLOB from (my $glob = *GLOB) gives us a very weird
47             # scalar. It's not a ref and it has a length but trying to
48             # call ->can on it throws an exception
49             && ref( \$v ) ne 'GLOB'
50             )
51             ) && List::Util::all { $v->can($_) } %s;
52             }
53             )
54             EOF
55             };
56              
57             }
58              
59 2     2   62 ## no critic (Subroutines::ProhibitUnusedPrivateSubroutines)
60             ## use critic
61              
62             __PACKAGE__->_ooify;
63 2     2   4  
64             1;
65              
66             # ABSTRACT: A class for constraints which require a class name or object with a set of methods
67              
68              
69             =pod
70              
71             =encoding UTF-8
72              
73             =head1 NAME
74              
75             Specio::Constraint::AnyCan - A class for constraints which require a class name or object with a set of methods
76              
77             =head1 VERSION
78              
79             version 0.48
80              
81             =head1 SYNOPSIS
82              
83             my $type = Specio::Constraint::AnyCan->new(...);
84             print $_, "\n" for @{ $type->methods };
85              
86             =head1 DESCRIPTION
87              
88             This is a specialized type constraint class for types which require a class
89             name or object with a defined set of methods.
90              
91             =head1 API
92              
93             This class provides all of the same methods as L<Specio::Constraint::Simple>,
94             with a few differences:
95              
96             =head2 Specio::Constraint::AnyCan->new( ... )
97              
98             The C<parent> parameter is ignored if it passed, as it is always set to the
99             C<Defined> type.
100              
101             The C<inline_generator> and C<constraint> parameters are also ignored. This
102             class provides its own default inline generator subroutine reference.
103              
104             This class overrides the C<message_generator> default if none is provided.
105              
106             Finally, this class requires an additional parameter, C<methods>. This must be
107             an array reference of method names which the constraint requires. You can also
108             pass a single string and it will be converted to an array reference internally.
109              
110             =head2 $any_can->methods
111              
112             Returns an array reference containing the methods this constraint requires.
113              
114             =head1 ROLES
115              
116             This class does the L<Specio::Constraint::Role::IsaType>,
117             L<Specio::Constraint::Role::Interface>, and L<Specio::Role::Inlinable> roles.
118              
119             =head1 SUPPORT
120              
121             Bugs may be submitted at L<https://github.com/houseabsolute/Specio/issues>.
122              
123             =head1 SOURCE
124              
125             The source code repository for Specio can be found at L<https://github.com/houseabsolute/Specio>.
126              
127             =head1 AUTHOR
128              
129             Dave Rolsky <autarch@urth.org>
130              
131             =head1 COPYRIGHT AND LICENSE
132              
133             This software is Copyright (c) 2012 - 2022 by Dave Rolsky.
134              
135             This is free software, licensed under:
136              
137             The Artistic License 2.0 (GPL Compatible)
138              
139             The full text of the license can be found in the
140             F<LICENSE> file included with this distribution.
141              
142             =cut