File Coverage

blib/lib/MooseX/Types/UndefinedType.pm
Criterion Covered Total %
statement 34 37 91.8
branch 6 8 75.0
condition n/a
subroutine 12 13 92.3
pod 3 3 100.0
total 55 61 90.1


line stmt bran cond sub pod time code
1 18     18   276 use warnings;
  18         30  
  18         632  
2 18     18   86 use strict;
  18         56  
  18         877  
3             package MooseX::Types::UndefinedType;
4             # ABSTRACT: a fallback type for when a type cannot be found
5              
6             our $VERSION = '0.50';
7              
8 18     18   88 use Moose::Util::TypeConstraints ();
  18         22  
  18         372  
9 18     18   82 use Carp::Clan '^MooseX::Types';
  18         23  
  18         140  
10 18     18   3139 use namespace::autoclean 0.16;
  18         549  
  18         145  
11              
12 116     116   221 use overload '""' => sub { shift->name },
13 18     18   1698 fallback => 1;
  18         51  
  18         214  
14              
15             #pod =head1 DESCRIPTION
16             #pod
17             #pod Whenever a type handle function (e.g. C<Int()> can't find a type
18             #pod constraint under its full name, it assumes it has not yet been defined.
19             #pod It will then return an instance of this class, handling only
20             #pod stringification, name and possible identification of undefined types.
21             #pod
22             #pod Later, when you try to use the Undefined Type Constraint, autovivification will
23             #pod be attempted.
24             #pod
25             #pod =head1 METHODS
26             #pod
27             #pod =head2 new
28             #pod
29             #pod Takes a full type name as argument and returns an instance of this
30             #pod class.
31             #pod
32             #pod =cut
33              
34             sub new {
35 58     58 1 240 return bless { name => $_[1] }, $_[0];
36             }
37              
38             #pod =head2 name
39             #pod
40             #pod Returns the stored type name.
41             #pod
42             #pod =cut
43              
44             sub name {
45 286     286 1 1000 return $_[0]->{name};
46             }
47              
48             #pod =head2 __autovivify
49             #pod
50             #pod Try to see if the type constraint has yet been defined and if so create it.
51             #pod
52             #pod =cut
53              
54             sub __autovivify {
55 9     9   10 my ($self) = @_;
56 9 100       59 if(my $tc = $self->{instance}) {
    100          
57 6         23 return $tc;
58             } elsif( my $new_tc = Moose::Util::TypeConstraints::find_type_constraint($self->name)) {
59 1         114 $self->{instance} = $new_tc;
60 1         4 return $new_tc;
61             } else {
62 2         106 return;
63             }
64             }
65              
66             #pod =head2 can_be_inlined
67             #pod
68             #pod Make sure that if a type hasn't been defined yet when Moose wants to inline it,
69             #pod we don't allow inlining.
70             #pod
71             #pod =cut
72              
73             sub can_be_inlined {
74 2     2 1 4 my $self = shift;
75 2 50       6 if(my $type_constraint = $self->__autovivify) {
76 0         0 return $type_constraint->can_be_inlined;
77             } else {
78 2         8 return;
79             }
80             }
81              
82             #pod =head2 AUTOLOAD
83             #pod
84             #pod Try to autovivify and delegate
85             #pod
86             #pod =cut
87              
88             sub AUTOLOAD {
89 7     7   14 my ($self, @args) = @_;
90 7         30 my ($method) = our $AUTOLOAD =~ /([^:]+)$/;
91              
92 7 50       22 if(my $type_constraint = $self->__autovivify) {
93 7         31 return $type_constraint->$method(@args);
94             } else {
95 0           croak "Method '$method' is not supported for " . $self->name;
96             }
97             }
98              
99             #pod =head2 DESTROY
100             #pod
101             #pod Moose::Meta::TypeConstraint::Parameterizable complains if this isn't here. TODO
102             #pod to find out why.
103             #pod
104             #pod =cut
105              
106             sub DESTROY {
107 0     0     return;
108             }
109              
110             #pod =head1 SEE ALSO
111             #pod
112             #pod L<MooseX::Types::Moose>,
113             #pod L<Moose::Util::TypeConstraints>,
114             #pod L<Moose::Meta::TypeConstraint>,
115             #pod L<Carp::Clan>
116             #pod
117             #pod =cut
118              
119              
120             1;
121              
122             __END__
123              
124             =pod
125              
126             =encoding UTF-8
127              
128             =head1 NAME
129              
130             MooseX::Types::UndefinedType - a fallback type for when a type cannot be found
131              
132             =head1 VERSION
133              
134             version 0.50
135              
136             =head1 DESCRIPTION
137              
138             Whenever a type handle function (e.g. C<Int()> can't find a type
139             constraint under its full name, it assumes it has not yet been defined.
140             It will then return an instance of this class, handling only
141             stringification, name and possible identification of undefined types.
142              
143             Later, when you try to use the Undefined Type Constraint, autovivification will
144             be attempted.
145              
146             =head1 METHODS
147              
148             =head2 new
149              
150             Takes a full type name as argument and returns an instance of this
151             class.
152              
153             =head2 name
154              
155             Returns the stored type name.
156              
157             =head2 __autovivify
158              
159             Try to see if the type constraint has yet been defined and if so create it.
160              
161             =head2 can_be_inlined
162              
163             Make sure that if a type hasn't been defined yet when Moose wants to inline it,
164             we don't allow inlining.
165              
166             =head2 AUTOLOAD
167              
168             Try to autovivify and delegate
169              
170             =head2 DESTROY
171              
172             Moose::Meta::TypeConstraint::Parameterizable complains if this isn't here. TODO
173             to find out why.
174              
175             =head1 SEE ALSO
176              
177             L<MooseX::Types::Moose>,
178             L<Moose::Util::TypeConstraints>,
179             L<Moose::Meta::TypeConstraint>,
180             L<Carp::Clan>
181              
182             =head1 SUPPORT
183              
184             Bugs may be submitted through L<the RT bug tracker|https://rt.cpan.org/Public/Dist/Display.html?Name=MooseX-Types>
185             (or L<bug-MooseX-Types@rt.cpan.org|mailto:bug-MooseX-Types@rt.cpan.org>).
186              
187             There is also a mailing list available for users of this distribution, at
188             L<http://lists.perl.org/list/moose.html>.
189              
190             There is also an irc channel available for users of this distribution, at
191             L<C<#moose> on C<irc.perl.org>|irc://irc.perl.org/#moose>.
192              
193             =head1 AUTHOR
194              
195             Robert "phaylon" Sedlacek <rs@474.at>
196              
197             =head1 COPYRIGHT AND LICENCE
198              
199             This software is copyright (c) 2007 by Robert "phaylon" Sedlacek.
200              
201             This is free software; you can redistribute it and/or modify it under
202             the same terms as the Perl 5 programming language system itself.
203              
204             =cut