1 package MooseX::Types::TypeDecorator;
6 use Carp::Clan qw( ^MooseX::Types );
7 use Moose::Util::TypeConstraints ();
8 use Moose::Meta::TypeConstraint::Union;
9 use Scalar::Util qw(blessed);
13 return shift->__type_constraint->name;
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
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);
32 MooseX::Types::TypeDecorator - More flexible access to a Type Constraint
36 This is a decorator object that contains an underlying type constraint. We use
37 this to control access to the type constraint and to add some features.
41 This class defines the following methods.
45 Old school instantiation
51 if(my $arg = shift @_) {
52 if(blessed $arg && $arg->isa('Moose::Meta::TypeConstraint')) {
53 return bless {'__type_constraint'=>$arg}, $class;
56 $arg->isa('MooseX::Types::UndefinedType')
58 ## stub in case we'll need to handle these types differently
59 return bless {'__type_constraint'=>$arg}, $class;
60 } elsif(blessed $arg) {
61 croak "Argument must be ->isa('Moose::Meta::TypeConstraint') or ->isa('MooseX::Types::UndefinedType'), not ". blessed $arg;
63 croak "Argument cannot be '$arg'";
66 croak "This method [new] requires a single argument.";
70 =head __type_constraint ($type_constraint)
72 Set/Get the type_constraint.
76 sub __type_constraint {
80 if(defined(my $tc = shift @_)) {
81 $self->{__type_constraint} = $tc;
83 return $self->{__type_constraint};
85 croak 'cannot call __type_constraint as a class method';
91 handle $self->isa since AUTOLOAD can't.
96 my ($self, $target) = @_;
98 return $self->__type_constraint->isa($target);
106 handle $self->can since AUTOLOAD can't.
111 my ($self, $target) = @_;
112 if(defined $target) {
113 return $self->__type_constraint->can($target);
121 We might need it later
131 Delegate to the decorator targe
136 my ($self, @args) = @_;
137 my ($method) = (our $AUTOLOAD =~ /([^:]+)$/);
138 if($self->__type_constraint->can($method)) {
139 return $self->__type_constraint->$method(@args);
141 croak "Method '$method' is not supported for ". ref($self->__type_constraint);
145 =head1 AUTHOR AND COPYRIGHT
147 John Napiorkowski (jnapiorkowski) <jjnapiork@cpan.org>
151 This program is free software; you can redistribute it and/or modify
152 it under the same terms as perl itself.