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