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