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