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