add manual coverage & spelling tests. Fix spelling errors
[gitmo/MooseX-Types.git] / lib / MooseX / Types / UndefinedType.pm
CommitLineData
52d358e2 1package MooseX::Types::UndefinedType;
e211870f 2
e211870f 3use warnings;
4use strict;
5
077ac262 6use Moose::Util::TypeConstraints ();
7use Carp::Clan qw( ^MooseX::Types );
8
e211870f 9use overload '""' => sub { shift->name },
10 fallback => 1;
11
12=head1 DESCRIPTION
13
14Whenever a type handle function (e.g. C<Int()> can't find a type
15constraint under it's full name, it assumes it has not yet been defined.
16It will then return an instance of this class, handling only
17stringification, name and possible identification of undefined types.
18
077ac262 19Later, when you try to use the Undefined Type Constraint, autovivification will
20be attempted.
21
e211870f 22=head1 METHODS
23
24=head2 new
25
26Takes a full type name as argument and returns an instance of this
27class.
28
29=cut
30
077ac262 31sub new {
32 return bless { name => $_[1] }, $_[0];
33}
e211870f 34
35=head2 name
36
37Returns the stored type name.
38
39=cut
40
077ac262 41sub name {
42 return $_[0]->{name};
43}
44
e512ffb3 45=head2 can_be_inlined
46
5a1fdc82 47Always returns false. Needed for compatibility with Moose 2.0100+.
e512ffb3 48
49=cut
50
51sub can_be_inlined { 0 }
52
077ac262 53=head2 __autovivify
54
55Try to see if the type constraint has yet been defined and if so create it.
56
57=cut
58
59sub __autovivify {
60 my ($self) = @_;
61 if(my $tc = $self->{instance}) {
62 return $tc;
63 } elsif( my $new_tc = Moose::Util::TypeConstraints::find_type_constraint($self->name)) {
64 $self->{instance} = $new_tc;
65 return $new_tc;
66 } else {
67 return;
68 }
69}
70
71=head2 AUTOLOAD
72
73Try to autovivify and delegate
74
75=cut
76
77sub AUTOLOAD {
78 my ($self, @args) = @_;
79 my ($method) = our $AUTOLOAD =~ /([^:]+)$/;
80
81 if(my $type_constraint = $self->__autovivify) {
82 return $type_constraint->$method(@args);
83 } else {
84 croak "Method '$method' is not supported for " . $self->name;
85 }
86}
87
88=head2 DESTROY
89
90Moose::Meta::TypeConstraint::Parameterizable complains if this isn't here. TODO
91to find out why.
92
93=cut
94
95sub DESTROY {
96 return;
97}
e211870f 98
99=head1 SEE ALSO
100
52d358e2 101L<MooseX::Types::Moose>,
e211870f 102L<Moose::Util::TypeConstraints>,
077ac262 103L<Moose::Meta::TypeConstraint>,
104L<Carp::Clan>
e211870f 105
e211870f 106=head1 LICENSE
107
108This program is free software; you can redistribute it and/or modify
109it under the same terms as perl itself.
110
111=cut
112
113
1141;