1 package MooseX::Types::TypeDecorator;
6 use Carp::Clan qw( ^MooseX::Types );
7 use Moose::Util::TypeConstraints ();
8 use Moose::Meta::TypeConstraint::Union;
12 return shift->__type_constraint->name;
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
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);
28 MooseX::Types::TypeDecorator - More flexible access to a Type Constraint
32 This is a decorator object that contains an underlying type constraint. We use
33 this to control access to the type constraint and to add some features.
37 This class defines the following methods.
41 Old school instantiation
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;
54 croak "Argument must be ->isa('Moose::Meta::TypeConstraint') or ->isa('MooseX::Types::UndefinedType'), not ". ref $arg;
56 croak "Argument cannot be '$arg'";
59 croak "This method [new] requires a single argument of 'arg'.";
63 =head __type_constraint ($type_constraint)
65 Set/Get the type_constraint.
69 sub __type_constraint {
71 if(defined(my $tc = shift @_)) {
72 $self->{__type_constraint} = $tc;
74 return $self->{__type_constraint};
79 handle $self->isa since AUTOLOAD can't.
84 my ($self, $target) = @_;
86 return $self->__type_constraint->isa($target);
94 handle $self->can since AUTOLOAD can't.
99 my ($self, $target) = @_;
100 if(defined $target) {
101 return $self->__type_constraint->can($target);
109 We might need it later
119 Delegate to the decorator targe
124 my ($self, @args) = @_;
125 my ($method) = (our $AUTOLOAD =~ /([^:]+)$/);
126 if($self->__type_constraint->can($method)) {
127 return $self->__type_constraint->$method(@args);
129 croak "Method '$method' is not supported";
133 =head1 AUTHOR AND COPYRIGHT
135 John Napiorkowski (jnapiorkowski) <jjnapiork@cpan.org>
139 This program is free software; you can redistribute it and/or modify
140 it under the same terms as perl itself.