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