better error checking, more correct stringification
[gitmo/MooseX-Types.git] / lib / MooseX / Types / TypeDecorator.pm
1 package MooseX::Types::TypeDecorator;
2
3 use strict;
4 use warnings;
5
6 use Carp::Clan qw( ^MooseX::Types );
7 use Moose::Util::TypeConstraints ();
8 use Moose::Meta::TypeConstraint::Union;
9 use Scalar::Util qw(blessed);
10
11 use overload(
12     '""' => sub {
13         return shift->__type_constraint->name; 
14     },
15     '|' => sub {
16         
17         ## It's kind of ugly that we need to know about Union Types, but this
18         ## is needed for syntax compatibility.  Maybe someday we'll all just do
19         ## Or[Str,Str,Int]
20         
21         my @tc = grep {blessed $_} @_;
22         my $union = Moose::Meta::TypeConstraint::Union->new(type_constraints=>\@tc);
23         return Moose::Util::TypeConstraints::register_type_constraint($union);
24     },
25 );
26
27
28 =head1 NAME
29
30 MooseX::Types::TypeDecorator - More flexible access to a Type Constraint
31
32 =head1 DESCRIPTION
33
34 This is a decorator object that contains an underlying type constraint.  We use
35 this to control access to the type constraint and to add some features.
36
37 =head1 METHODS
38
39 This class defines the following methods.
40
41 =head2 new
42
43 Old school instantiation
44
45 =cut
46
47 sub new {
48     my $class = shift @_;
49     if(my $arg = shift @_) {
50         if(blessed $arg && $arg->isa('Moose::Meta::TypeConstraint')) {
51             return bless {'__type_constraint'=>$arg}, $class;
52         } elsif(blessed $arg && $arg->isa('MooseX::Types::UndefinedType')) {
53             ## stub in case we'll need to handle these types differently
54             return bless {'__type_constraint'=>$arg}, $class;
55         } elsif(blessed $arg) {
56             croak "Argument must be ->isa('Moose::Meta::TypeConstraint') or ->isa('MooseX::Types::UndefinedType'), not ". blessed $arg;
57         } else {
58             croak "Argument cannot be '$arg'";
59         }
60     } else {
61         croak "This method [new] requires a single argument of 'arg'.";        
62     }
63 }
64
65 =head __type_constraint ($type_constraint)
66
67 Set/Get the type_constraint.
68
69 =cut
70
71 sub __type_constraint {
72     my $self = shift @_;
73     
74     if(blessed $self) {
75         if(defined(my $tc = shift @_)) {
76             $self->{__type_constraint} = $tc;
77         }
78         return $self->{__type_constraint};        
79     } else {
80         croak 'cannot call __type_constraint as a class method';
81     }
82 }
83
84 =head2 isa
85
86 handle $self->isa since AUTOLOAD can't.
87
88 =cut
89
90 sub isa {
91     my ($self, $target) = @_;  
92     if(defined $target) {
93         return $self->__type_constraint->isa($target);
94     } else {
95         return;
96     }
97 }
98
99 =head2 can
100
101 handle $self->can since AUTOLOAD can't.
102
103 =cut
104
105 sub can {
106     my ($self, $target) = @_;
107     if(defined $target) {
108         return $self->__type_constraint->can($target);
109     } else {
110         return;
111     }
112 }
113
114 =head2 DESTROY
115
116 We might need it later
117
118 =cut
119
120 sub DESTROY {
121     return;
122 }
123
124 =head2 AUTOLOAD
125
126 Delegate to the decorator targe
127
128 =cut
129
130 sub AUTOLOAD {
131     my ($self, @args) = @_;
132     my ($method) = (our $AUTOLOAD =~ /([^:]+)$/);
133     if($self->__type_constraint->can($method)) {
134         return $self->__type_constraint->$method(@args);
135     } else {
136         croak "Method '$method' is not supported";   
137     }
138 }
139
140 =head1 AUTHOR AND COPYRIGHT
141
142 John Napiorkowski (jnapiorkowski) <jjnapiork@cpan.org>
143
144 =head1 LICENSE
145
146 This program is free software; you can redistribute it and/or modify
147 it under the same terms as perl itself.
148
149 =cut
150
151 1;