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