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 17     17   79 use warnings;
  17         30  
  17         468  
2 17     17   114 use strict;
  17         28  
  17         859  
3             package MooseX::Types::UndefinedType;
4             # ABSTRACT: a fallback type for when a type cannot be found
5              
6             our $VERSION = '0.46';
7              
8 17     17   80 use Moose::Util::TypeConstraints ();
  17         27  
  17         271  
9 17     17   74 use Carp::Clan '^MooseX::Types';
  17         29  
  17         108  
10 17     17   2339 use namespace::autoclean 0.16;
  17         465  
  17         101  
11              
12 104     104   230 use overload '""' => sub { shift->name },
13 17     17   1291 fallback => 1;
  17         34  
  17         175  
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 52     52 1 231 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 256     256 1 929 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   14 my ($self) = @_;
56 9 100       57 if(my $tc = $self->{instance}) {
    100          
57 6         35 return $tc;
58             } elsif( my $new_tc = Moose::Util::TypeConstraints::find_type_constraint($self->name)) {
59 1         90 $self->{instance} = $new_tc;
60 1         5 return $new_tc;
61             } else {
62 2         103 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 3 my $self = shift;
75 2 50       5 if(my $type_constraint = $self->__autovivify) {
76 0         0 return $type_constraint->can_be_inlined;
77             } else {
78 2         7 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         34 my ($method) = our $AUTOLOAD =~ /([^:]+)$/;
91              
92 7 50       23 if(my $type_constraint = $self->__autovivify) {
93 7         38 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.46
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 AUTHOR
183              
184             Robert "phaylon" Sedlacek <rs@474.at>
185              
186             =head1 COPYRIGHT AND LICENSE
187              
188             This software is copyright (c) 2007 by Robert "phaylon" Sedlacek.
189              
190             This is free software; you can redistribute it and/or modify it under
191             the same terms as the Perl 5 programming language system itself.
192              
193             =cut