no trailing whitespace
[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
e9dc30af 14Whenever a type handle function (e.g. C<Int()> can't find a type
e211870f 15constraint under it's full name, it assumes it has not yet been defined.
e9dc30af 16It will then return an instance of this class, handling only
e211870f 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
45=head2 __autovivify
46
47Try to see if the type constraint has yet been defined and if so create it.
48
49=cut
50
51sub __autovivify {
52 my ($self) = @_;
53 if(my $tc = $self->{instance}) {
54 return $tc;
55 } elsif( my $new_tc = Moose::Util::TypeConstraints::find_type_constraint($self->name)) {
56 $self->{instance} = $new_tc;
57 return $new_tc;
58 } else {
59 return;
60 }
61}
62
c1533506 63=head2 can_be_inlined
64
65Make sure that if a type hasn't been defined yet when Moose wants to inline it,
66we don't allow inlining.
67
68=cut
69
70sub can_be_inlined {
71 my $self = shift;
72 if(my $type_constraint = $self->__autovivify) {
73 return $type_constraint->can_be_inlined;
74 } else {
75 return;
76 }
77}
78
077ac262 79=head2 AUTOLOAD
80
81Try to autovivify and delegate
82
83=cut
84
85sub AUTOLOAD {
86 my ($self, @args) = @_;
e9dc30af 87 my ($method) = our $AUTOLOAD =~ /([^:]+)$/;
077ac262 88
89 if(my $type_constraint = $self->__autovivify) {
90 return $type_constraint->$method(@args);
91 } else {
92 croak "Method '$method' is not supported for " . $self->name;
93 }
94}
95
96=head2 DESTROY
97
98Moose::Meta::TypeConstraint::Parameterizable complains if this isn't here. TODO
99to find out why.
100
101=cut
102
103sub DESTROY {
104 return;
105}
e211870f 106
107=head1 SEE ALSO
108
52d358e2 109L<MooseX::Types::Moose>,
e9dc30af 110L<Moose::Util::TypeConstraints>,
077ac262 111L<Moose::Meta::TypeConstraint>,
112L<Carp::Clan>
e211870f 113
e211870f 114=head1 LICENSE
115
116This program is free software; you can redistribute it and/or modify
117it under the same terms as perl itself.
118
119=cut
120
121
1221;