File Coverage

blib/lib/Function/Interface/Types.pm
Criterion Covered Total %
statement 17 17 100.0
branch n/a
condition n/a
subroutine 6 6 100.0
pod n/a
total 23 23 100.0


line stmt bran cond sub pod time code
1             package Function::Interface::Types;
2              
3 1     1   224191 use v5.14.0;
  1         9  
4 1     1   6 use warnings;
  1         2  
  1         49  
5              
6             our $VERSION = "0.04";
7              
8 1         11 use Type::Library -base,
9 1     1   433 -declare => qw( ImplOf );
  1         22450  
10              
11 1     1   1019 use Types::Standard -types;
  1         48686  
  1         10  
12 1     1   4636 use Class::Load qw(is_class_loaded);
  1         3  
  1         51  
13 1     1   7 use Scalar::Util qw(blessed);
  1         2  
  1         261  
14              
15             __PACKAGE__->add_type({
16             name => 'ImplOf',
17             parent => ClassName | Object,
18             constraint_generator => sub {
19             my $self = $Type::Tiny::parameterize_type;
20             my @interfaces = @_;
21              
22             my $display_name = $self->name_generator->($self, @interfaces);
23              
24             my %options = (
25             constraint => sub {
26             my ($package) = @_;
27             for (@interfaces) {
28             return unless Function::Interface::Impl::impl_of($package, $_);
29             }
30             return !!1;
31             },
32             display_name => $display_name,
33             parameters => [@interfaces],
34             message => sub {
35             my $package = ref $_ ? blessed($_) : $_;
36             return "$package is not loaded" if !is_class_loaded($package);
37             return sprintf '%s did not pass type constraint "%s"', Type::Tiny::_dd($_), $display_name;
38             },
39             );
40              
41             return $self->create_child_type(%options);
42             },
43             });
44              
45             __PACKAGE__->meta->make_immutable;
46              
47             __END__