1 package MooseX::Types::TypeDecorator;
8 use Carp::Clan qw( ^MooseX::Types );
9 use Moose::Util::TypeConstraints ();
10 use Moose::Meta::TypeConstraint::Union;
11 use Scalar::Util qw(blessed);
17 return $self->__type_constraint->name;
24 ## It's kind of ugly that we need to know about Union Types, but this
25 ## is needed for syntax compatibility. Maybe someday we'll all just do
28 my @args = @_[0,1]; ## arg 3 is special, see the overload docs.
29 my @tc = grep {blessed $_} map {
31 Moose::Util::TypeConstraints::find_or_parse_type_constraint($_)
32 || __PACKAGE__->_throw_error( "$_ is not a type constraint")
35 ( scalar @tc == scalar @args)
36 || __PACKAGE__->_throw_error(
37 "one of your type constraints is bad. Passed: ". join(', ', @args) ." Got: ". join(', ', @tc));
40 || __PACKAGE__->_throw_error("You must pass in at least 2 type names to make a union");
42 my $union = Moose::Meta::TypeConstraint::Union->new(type_constraints=>\@tc);
43 return Moose::Util::TypeConstraints::register_type_constraint($union);
51 MooseX::Types::TypeDecorator - More flexible access to a Type Constraint
55 This is a decorator object that contains an underlying type constraint. We use
56 this to control access to the type constraint and to add some features.
60 This class defines the following methods.
64 Old school instantiation
70 if(my $arg = shift @_) {
71 if(blessed $arg && $arg->isa('Moose::Meta::TypeConstraint')) {
72 return bless {'__type_constraint'=>$arg}, $class;
75 $arg->isa('MooseX::Types::UndefinedType')
77 ## stub in case we'll need to handle these types differently
78 return bless {'__type_constraint'=>$arg}, $class;
79 } elsif(blessed $arg) {
80 __PACKAGE__->_throw_error("Argument must be ->isa('Moose::Meta::TypeConstraint') or ->isa('MooseX::Types::UndefinedType'), not ". blessed $arg);
82 __PACKAGE__->_throw_error("Argument cannot be '$arg'");
85 __PACKAGE__->_throw_error("This method [new] requires a single argument.");
89 =head2 __type_constraint ($type_constraint)
91 Set/Get the type_constraint.
95 sub __type_constraint {
98 if(defined(my $tc = shift @_)) {
99 $self->{__type_constraint} = $tc;
101 return $self->{__type_constraint};
103 __PACKAGE__->_throw_error('cannot call __type_constraint as a class method');
109 handle $self->isa since AUTOLOAD can't.
114 my ($self, $target) = @_;
115 if(defined $target) {
117 return $self->__type_constraint->isa($target);
129 handle $self->can since AUTOLOAD can't.
134 my ($self, $target) = @_;
135 if(defined $target) {
137 return $self->__type_constraint->can($target);
148 have meta examine the underlying type constraints
155 return $self->__type_constraint->meta;
161 properly delegate error messages
169 goto &Moose::throw_error;
174 We might need it later
184 Delegate to the decorator targe
190 my ($self, @args) = @_;
191 my ($method) = (our $AUTOLOAD =~ /([^:]+)$/);
193 ## We delegate with this method in an attempt to support a value of
194 ## __type_constraint which is also AUTOLOADing, in particular the class
195 ## MooseX::Types::UndefinedType which AUTOLOADs during autovivication.
200 $return = $self->__type_constraint->$method(@args);
202 __PACKAGE__->_throw_error($@);
210 See L<MooseX::Types/AUTHOR>.
214 This program is free software; you can redistribute it and/or modify
215 it under the same terms as perl itself.