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;
54 } elsif(blessed $arg && $arg->isa('MooseX::Types::UndefinedType')) {
55 ## stub in case we'll need to handle these types differently
56 return bless {'__type_constraint'=>$arg}, $class;
57 } elsif(blessed $arg) {
58 croak "Argument must be ->isa('Moose::Meta::TypeConstraint') or ->isa('MooseX::Types::UndefinedType'), not ". blessed $arg;
60 croak "Argument cannot be '$arg'";
63 croak "This method [new] requires a single argument of 'arg'.";
67 =head __type_constraint ($type_constraint)
69 Set/Get the type_constraint.
73 sub __type_constraint {
77 if(defined(my $tc = shift @_)) {
78 $self->{__type_constraint} = $tc;
80 return $self->{__type_constraint};
82 croak 'cannot call __type_constraint as a class method';
88 handle $self->isa since AUTOLOAD can't.
93 my ($self, $target) = @_;
95 return $self->__type_constraint->isa($target);
103 handle $self->can since AUTOLOAD can't.
108 my ($self, $target) = @_;
109 if(defined $target) {
110 return $self->__type_constraint->can($target);
118 We might need it later
128 Delegate to the decorator targe
133 my ($self, @args) = @_;
134 my ($method) = (our $AUTOLOAD =~ /([^:]+)$/);
135 if($self->__type_constraint->can($method)) {
136 return $self->__type_constraint->$method(@args);
138 croak "Method '$method' is not supported";
142 =head1 AUTHOR AND COPYRIGHT
144 John Napiorkowski (jnapiorkowski) <jjnapiork@cpan.org>
148 This program is free software; you can redistribute it and/or modify
149 it under the same terms as perl itself.