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