File Coverage

blib/lib/Types/Self.pm
Criterion Covered Total %
statement 42 42 100.0
branch 8 12 66.6
condition n/a
subroutine 15 15 100.0
pod n/a
total 65 69 94.2


line stmt bran cond sub pod time code
1 3     3   118199 use 5.008001;
  3         27  
2 3     3   13 use strict;
  3         4  
  3         57  
3 3     3   11 use warnings;
  3         5  
  3         228  
4              
5              
6             our $AUTHORITY = 'cpan:TOBYINK';
7             our $VERSION = '0.001';
8              
9             use Exporter::Tiny qw();
10 3     3   1219 our @ISA = qw( Exporter::Tiny );
  3         8468  
  3         151  
11             our @EXPORT = qw( Self );
12             our @EXPORT_OK = qw( is_Self assert_Self to_Self );
13              
14             use Role::Hooks qw();
15 3     3   1343 use Types::Standard qw( InstanceOf ConsumerOf );
  3         11927  
  3         79  
16 3     3   1758  
  3         254324  
  3         40  
17             my ( $me, $name, $args, $globals ) = ( shift, @_ );
18              
19 4     4   801 return sub () {
20             $me->_make_type_constraint( $globals ) unless defined $globals->{type};
21             return $globals->{type};
22 6 100   6   8462 };
23 6         7964 }
24 4         27  
25             my ( $me, $name, $args, $globals ) = ( shift, @_ );
26              
27             return sub ($) {
28 1     1   45 my ( $value ) = @_;
29             $me->_make_type_constraint( $globals ) unless defined $globals->{type};
30             return $globals->{type}->check( $value );
31 2     2   24 };
32 2 50       7 }
33 2         5  
34 1         7 my ( $me, $name, $args, $globals ) = ( shift, @_ );
35              
36             return sub {
37             my ( $value ) = @_;
38 1     1   40 $me->_make_type_constraint( $globals ) unless defined $globals->{type};
39             return $globals->{type}->assert_return( $value );
40             };
41 2     2   452 }
42 2 50       8  
43 2         10 my ( $me, $name, $args, $globals ) = ( shift, @_ );
44 1         5  
45             return sub ($) {
46             my ( $value ) = @_;
47             $me->_make_type_constraint( $globals ) unless defined $globals->{type};
48 1     1   40 return $globals->{type}->coerce( $value );
49             };
50             }
51 1     1   9607  
52 1 50       6 my ( $me, $globals ) = @_;
53 1         17 return $globals->{type} if defined $globals->{type};
54 1         5  
55             my $caller = $globals->{into};
56             my $is_role = 'Role::Hooks'->is_role( $caller );
57             my $base = $is_role ? ConsumerOf : InstanceOf;
58 3     3   10  
59 3 50       11 $globals->{type} = $base->parameterize( $caller );
60             }
61 3         8  
62 3         37  
63 3 100       70 =pod
64              
65 3         65 =encoding utf-8
66              
67             =head1 NAME
68              
69             Types::Self - provides a "Self" type constraint, referring to the caller class or role
70              
71             =head1 SYNOPSIS
72              
73             {
74             package Cow;
75             use Moo;
76             }
77            
78             {
79             package Horse;
80             use Moo;
81             use Types::Self;
82             use Types::Standard qw( Str );
83            
84             has name => ( is => 'ro', isa => Str );
85             has mother => ( is => 'ro', isa => Self );
86             has father => ( is => 'ro', isa => Self );
87             }
88            
89             my $alice = Horse->new( name => 'Alice' );
90             my $bob = Horse->new( name => 'Bob' );
91            
92             # Okay
93             my $baby = Horse->new(
94             name => 'Baby',
95             mother => $alice,
96             father => $bob,
97             );
98            
99             # Dies
100             my $baby = Horse->new(
101             name => 'Baby',
102             mother => Cow->new,
103             father => $bob,
104             );
105              
106             =head1 DESCRIPTION
107              
108             This module exports a C<Self> type constraint which consrtains values to be
109             blessed objects in the same class as the package it was imported into, or
110             blessed objects which consume the role it was imported into. It should do
111             the right thing with inheritance.
112              
113             This module also exports C<is_Self>, which returns a boolean.
114              
115             package Marriage;
116             use Moo::Role;
117             use Types::Self qw( is_Self );
118            
119             has spouse => ( is => 'rwp', init_arg => undef );
120            
121             sub marry {
122             my ( $me, $maybe_spouse ) = @_;
123             if ( is_Self( $maybe_spouse ) ) {
124             $me->_set_spouse( $maybe_spouse );
125             $maybe_spouse->_set_spouse( $me );
126             }
127             else {
128             warn "Cannot marry this!";
129             }
130             return $me;
131             }
132              
133             The module also exports C<assert_Self> which acts like C<is_Self> but instead
134             of returning a boolean, either lives or dies. This can be useful is you need
135             to check that the first argument to a function is a blessed object.
136              
137             sub connect {
138             my ( $self ) = ( shift );
139             assert_Self $self; # dies if called as a class method
140             $self->{connected} = 1;
141             return $self;
142             }
143              
144             The module also exports C<to_Self> which will attempt to coerce other types
145             to the B<Self> type.
146              
147             Only C<Self> is exported by default.
148              
149             =head1 BUGS
150              
151             Please report any bugs to
152             L<http://rt.cpan.org/Dist/Display.html?Queue=Types-Self>.
153              
154             =head1 SEE ALSO
155              
156             L<Types::Standard>, L<Type::Tiny::Manual>.
157              
158             =head1 AUTHOR
159              
160             Toby Inkster E<lt>tobyink@cpan.orgE<gt>.
161              
162             =head1 COPYRIGHT AND LICENCE
163              
164             This software is copyright (c) 2022 by Toby Inkster.
165              
166             This is free software; you can redistribute it and/or modify it under
167             the same terms as the Perl 5 programming language system itself.
168              
169             =head1 DISCLAIMER OF WARRANTIES
170              
171             THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED
172             WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
173             MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.