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