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