bump version to 0.26
[gitmo/MooseX-Types.git] / lib / MooseX / Types / TypeDecorator.pm
1 package MooseX::Types::TypeDecorator;
2 our $VERSION = "0.26";
3
4 use strict;
5 use warnings;
6
7
8 use Carp::Clan qw( ^MooseX::Types );
9 use Moose::Util::TypeConstraints ();
10 use Moose::Meta::TypeConstraint::Union;
11 use Scalar::Util qw(blessed);
12
13 use overload(
14     '0+' => sub {
15             my $self = shift @_;
16             my $tc = $self->{__type_constraint};
17             return 0+$tc;
18      },
19     '""' => sub {
20                 my $self = shift @_;
21                 if(blessed $self) {
22                         return $self->__type_constraint->name;                  
23                 } else {
24                         return "$self";
25                 }
26     },
27     bool => sub { 1 },
28     '|' => sub {
29         
30         ## It's kind of ugly that we need to know about Union Types, but this
31         ## is needed for syntax compatibility.  Maybe someday we'll all just do
32         ## Or[Str,Str,Int]
33
34         my @args = @_[0,1]; ## arg 3 is special,  see the overload docs.
35         my @tc = grep {blessed $_} map {
36             blessed $_ ? $_ :
37             Moose::Util::TypeConstraints::find_or_parse_type_constraint($_)
38               || __PACKAGE__->_throw_error( "$_ is not a type constraint")
39         } @args;
40
41         ( scalar @tc == scalar @args)
42             || __PACKAGE__->_throw_error(
43                           "one of your type constraints is bad.  Passed: ". join(', ', @args) ." Got: ". join(', ', @tc));
44
45         ( scalar @tc >= 2 )
46             || __PACKAGE__->_throw_error("You must pass in at least 2 type names to make a union");
47
48         my $union = Moose::Meta::TypeConstraint::Union->new(type_constraints=>\@tc);
49         return Moose::Util::TypeConstraints::register_type_constraint($union);
50     },
51     fallback => 1,
52     
53 );
54
55 =head1 NAME
56
57 MooseX::Types::TypeDecorator - More flexible access to a Type Constraint
58
59 =head1 DESCRIPTION
60
61 This is a decorator object that contains an underlying type constraint.  We use
62 this to control access to the type constraint and to add some features.
63
64 =head1 METHODS
65
66 This class defines the following methods.
67
68 =head2 new
69
70 Old school instantiation
71
72 =cut
73
74 sub new {
75     my $class = shift @_;
76     if(my $arg = shift @_) {
77         if(blessed $arg && $arg->isa('Moose::Meta::TypeConstraint')) {
78             return bless {'__type_constraint'=>$arg}, $class;
79         } elsif(
80             blessed $arg &&
81             $arg->isa('MooseX::Types::UndefinedType') 
82           ) {
83             ## stub in case we'll need to handle these types differently
84             return bless {'__type_constraint'=>$arg}, $class;
85         } elsif(blessed $arg) {
86             __PACKAGE__->_throw_error("Argument must be ->isa('Moose::Meta::TypeConstraint') or ->isa('MooseX::Types::UndefinedType'), not ". blessed $arg);
87         } else {
88             __PACKAGE__->_throw_error("Argument cannot be '$arg'");
89         }
90     } else {
91         __PACKAGE__->_throw_error("This method [new] requires a single argument.");        
92     }
93 }
94
95 =head2 __type_constraint ($type_constraint)
96
97 Set/Get the type_constraint.
98
99 =cut
100
101 sub __type_constraint {
102     my $self = shift @_;    
103     if(blessed $self) {
104         if(defined(my $tc = shift @_)) {
105             $self->{__type_constraint} = $tc;
106         }
107         return $self->{__type_constraint};        
108     } else {
109         __PACKAGE__->_throw_error('cannot call __type_constraint as a class method');
110     }
111 }
112
113 =head2 isa
114
115 handle $self->isa since AUTOLOAD can't.
116
117 =cut
118
119 sub isa {
120     my ($self, $target) = @_;  
121     if(defined $target) {
122         if(blessed $self) {
123                 return $self->__type_constraint->isa($target);
124         } else {
125                 return;
126         }
127     } else {
128         return;
129     }
130 }
131
132
133 =head2 can
134
135 handle $self->can since AUTOLOAD can't.
136
137 =cut
138
139 sub can {
140     my ($self, $target) = @_;
141     if(defined $target) {
142         if(blessed $self) {
143                 return $self->__type_constraint->can($target);
144         } else {
145                 return;
146         }
147     } else {
148         return;
149     }
150 }
151
152 =head2 meta
153
154 have meta examine the underlying type constraints
155
156 =cut
157
158 sub meta {
159         my $self = shift @_;
160         if(blessed $self) {
161                 return $self->__type_constraint->meta;
162         } 
163 }
164
165 =head2 _throw_error
166
167 properly delegate error messages
168
169 =cut
170
171 sub _throw_error {
172     shift;
173     require Moose;
174     unshift @_, 'Moose';
175     goto &Moose::throw_error;
176 }
177
178 =head2 DESTROY
179
180 We might need it later
181
182 =cut
183
184 sub DESTROY {
185     return;
186 }
187
188 =head2 AUTOLOAD
189
190 Delegate to the decorator targe
191
192 =cut
193
194 sub AUTOLOAD {
195     
196     my ($self, @args) = @_;
197     my ($method) = (our $AUTOLOAD =~ /([^:]+)$/);
198     
199     ## We delegate with this method in an attempt to support a value of
200     ## __type_constraint which is also AUTOLOADing, in particular the class
201     ## MooseX::Types::UndefinedType which AUTOLOADs during autovivication.
202     
203     my $return;
204     eval {
205         $return = $self->__type_constraint->$method(@args);
206     }; if($@) {
207         __PACKAGE__->_throw_error($@);
208     } else {
209         return $return;
210     }
211 }
212
213 =head1 AUTHOR
214
215 See L<MooseX::Types/AUTHOR>.
216
217 =head1 LICENSE
218
219 This program is free software; you can redistribute it and/or modify
220 it under the same terms as perl itself.
221
222 =cut
223
224 1;