1 package MooseX::Types::TypeDecorator;
7 use Carp::Clan qw( ^MooseX::Types );
8 use Moose::Util::TypeConstraints ();
9 use Moose::Meta::TypeConstraint::Union;
10 use Scalar::Util qw(blessed);
16 return $self->__type_constraint->name;
23 ## It's kind of ugly that we need to know about Union Types, but this
24 ## is needed for syntax compatibility. Maybe someday we'll all just do
27 my @args = @_[0,1]; ## arg 3 is special, see the overload docs.
28 my @tc = grep {blessed $_} map {
30 Moose::Util::TypeConstraints::find_or_parse_type_constraint($_)
31 || croak "$_ is not a type constraint"
34 ( scalar @tc == scalar @args)
35 || croak "one of your type constraints is bad. Passed: ". join(', ', @args) ." Got: ". join(', ', @tc);
38 || croak "You must pass in at least 2 type names to make a union";
40 my $union = Moose::Meta::TypeConstraint::Union->new(type_constraints=>\@tc);
41 return Moose::Util::TypeConstraints::register_type_constraint($union);
49 MooseX::Types::TypeDecorator - More flexible access to a Type Constraint
53 This is a decorator object that contains an underlying type constraint. We use
54 this to control access to the type constraint and to add some features.
58 This class defines the following methods.
62 Old school instantiation
68 if(my $arg = shift @_) {
69 if(blessed $arg && $arg->isa('Moose::Meta::TypeConstraint')) {
70 return bless {'__type_constraint'=>$arg}, $class;
73 $arg->isa('MooseX::Types::UndefinedType')
75 ## stub in case we'll need to handle these types differently
76 return bless {'__type_constraint'=>$arg}, $class;
77 } elsif(blessed $arg) {
78 croak "Argument must be ->isa('Moose::Meta::TypeConstraint') or ->isa('MooseX::Types::UndefinedType'), not ". blessed $arg;
80 croak "Argument cannot be '$arg'";
83 croak "This method [new] requires a single argument.";
87 =head2 __type_constraint ($type_constraint)
89 Set/Get the type_constraint.
93 sub __type_constraint {
96 if(defined(my $tc = shift @_)) {
97 $self->{__type_constraint} = $tc;
99 return $self->{__type_constraint};
101 croak 'cannot call __type_constraint as a class method';
107 handle $self->isa since AUTOLOAD can't.
112 my ($self, $target) = @_;
113 if(defined $target) {
115 return $self->__type_constraint->isa($target);
126 handle $self->can since AUTOLOAD can't.
131 my ($self, $target) = @_;
132 if(defined $target) {
134 return $self->__type_constraint->can($target);
145 have meta examine the underlying type constraints
152 return $self->__type_constraint->meta;
159 We might need it later
169 Delegate to the decorator targe
175 my ($self, @args) = @_;
176 my ($method) = (our $AUTOLOAD =~ /([^:]+)$/);
178 ## We delegate with this method in an attempt to support a value of
179 ## __type_constraint which is also AUTOLOADing, in particular the class
180 ## MooseX::Types::UndefinedType which AUTOLOADs during autovivication.
185 $return = $self->__type_constraint->$method(@args);
193 =head1 AUTHOR AND COPYRIGHT
195 John Napiorkowski (jnapiorkowski) <jjnapiork@cpan.org>
199 This program is free software; you can redistribute it and/or modify
200 it under the same terms as perl itself.