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