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