File Coverage

blib/lib/Specio/Constraint/ObjectCan.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::ObjectCan;
2              
3 2     2   15 use strict;
  2         6  
  2         63  
4 2     2   10 use warnings;
  2         5  
  2         89  
5              
6             our $VERSION = '0.47';
7              
8 2     2   11 use List::Util 1.33 ();
  2         105  
  2         45  
9 2     2   12 use Role::Tiny::With;
  2         5  
  2         88  
10 2     2   11 use Scalar::Util ();
  2         4  
  2         36  
11 2     2   10 use Specio::Helpers qw( perlstring );
  2         3  
  2         76  
12 2     2   446 use Specio::Library::Builtins;
  2         5  
  2         23  
13 2     2   14 use Specio::OO;
  2         5  
  2         126  
14              
15 2     2   1211 use Specio::Constraint::Role::CanType;
  2         6  
  2         367  
16             with 'Specio::Constraint::Role::CanType';
17              
18             {
19             my $Object = t('Object');
20 5     5   54 sub _build_parent {$Object}
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             my $v = %s;
33             Scalar::Util::blessed($v)
34             && List::Util::all { $v->can($_) } %s;
35             }
36             )
37             EOF
38             };
39              
40 5     5   140 sub _build_inline_generator {$_inline_generator}
41             }
42              
43             ## no critic (Subroutines::ProhibitUnusedPrivateSubroutines)
44 5     5   14 sub _allow_classes {0}
45             ## use critic
46              
47             __PACKAGE__->_ooify;
48              
49             1;
50              
51             # ABSTRACT: A class for constraints which require an object with a set of methods
52              
53             __END__
54              
55             =pod
56              
57             =encoding UTF-8
58              
59             =head1 NAME
60              
61             Specio::Constraint::ObjectCan - A class for constraints which require an object with a set of methods
62              
63             =head1 VERSION
64              
65             version 0.47
66              
67             =head1 SYNOPSIS
68              
69             my $type = Specio::Constraint::ObjectCan->new(...);
70             print $_, "\n" for @{ $type->methods };
71              
72             =head1 DESCRIPTION
73              
74             This is a specialized type constraint class for types which require an object
75             with a defined set of methods.
76              
77             =head1 API
78              
79             This class provides all of the same methods as L<Specio::Constraint::Simple>,
80             with a few differences:
81              
82             =head2 Specio::Constraint::ObjectCan->new( ... )
83              
84             The C<parent> parameter is ignored if it passed, as it is always set to the
85             C<Object> type.
86              
87             The C<inline_generator> and C<constraint> parameters are also ignored. This
88             class provides its own default inline generator subroutine reference.
89              
90             This class overrides the C<message_generator> default if none is provided.
91              
92             Finally, this class requires an additional parameter, C<methods>. This must be
93             an array reference of method names which the constraint requires. You can also
94             pass a single string and it will be converted to an array reference internally.
95              
96             =head2 $object_can->methods
97              
98             Returns an array reference containing the methods this constraint requires.
99              
100             =head1 ROLES
101              
102             This class does the L<Specio::Constraint::Role::CanType>,
103             L<Specio::Constraint::Role::Interface>, and L<Specio::Role::Inlinable> roles.
104              
105             =head1 SUPPORT
106              
107             Bugs may be submitted at L<https://github.com/houseabsolute/Specio/issues>.
108              
109             I am also usually active on IRC as 'autarch' on C<irc://irc.perl.org>.
110              
111             =head1 SOURCE
112              
113             The source code repository for Specio can be found at L<https://github.com/houseabsolute/Specio>.
114              
115             =head1 AUTHOR
116              
117             Dave Rolsky <autarch@urth.org>
118              
119             =head1 COPYRIGHT AND LICENSE
120              
121             This software is Copyright (c) 2012 - 2021 by Dave Rolsky.
122              
123             This is free software, licensed under:
124              
125             The Artistic License 2.0 (GPL Compatible)
126              
127             The full text of the license can be found in the
128             F<LICENSE> file included with this distribution.
129              
130             =cut