1 package MooseX::Types::Dependent;
7 #use Carp::Clan qw( ^MooseX::Types );
8 use Moose::Util::TypeConstraints ();
9 use Scalar::Util qw(blessed);
15 return $self->__internal_type_constraint->name;
25 MooseX::Types::Dependent - Type Constraints that are dependent on others
29 use MooseX::Types::Dependent;
31 ## Assuming the type constraint 'Set' isa Set::Scalar
34 as Dependent[Int,Set],
36 ## ok Set->check($set), 'Good $set';
37 ## ok Int->check($val), 'Already an Int'
39 ## If the $set already has $val, then it's not unique
40 return $set->has($val) ? 0:1
43 my $set = Set::Scalar->new(1..10);
45 ok UniqueInt->check([1, $set]); ## Fails, 1 is already in $set;
46 ok UniqueInt->check(['a', $set]); ## Fails, 'a' is not an Int;
47 ok UniqueInt->check([1, $obj]); ## Fails, $obj is not a Set;
48 ok UniqueInt->check([20, $set]); ## PASSES
52 This is a decorator object that contains an underlying type constraint. We use
53 this to control access to the type constraint and to add some features.
57 This class defines the following methods.
61 Old school instantiation
69 if(my $arg = shift @_) {
70 if(blessed $arg && $arg->isa('Moose::Meta::TypeConstraint')) {
71 return bless {'__type_constraint'=>$arg}, $class;
74 $arg->isa('MooseX::Types::UndefinedType')
76 ## stub in case we'll need to handle these types differently
77 return bless {'__type_constraint'=>$arg}, $class;
78 } elsif(blessed $arg) {
79 croak "Argument must be ->isa('Moose::Meta::TypeConstraint') or ->isa('MooseX::Types::UndefinedType'), not ". blessed $arg;
81 croak "Argument cannot be '$arg'";
84 croak "This method [new] requires a single argument.";
88 =head2 __internal_type_constraint ($type_constraint)
90 Set/Get the type_constraint we are making dependent.
94 sub __internal_type_constraint {
97 if(defined(my $tc = shift @_)) {
98 $self->{__type_constraint} = $tc;
100 return $self->{__type_constraint};
102 croak 'cannot call __internal_type_constraint as a class method';
108 handle $self->isa since AUTOLOAD can't.
113 my ($self, $target) = @_;
114 if(defined $target) {
116 return $self->__internal_type_constraint->isa($target);
127 handle $self->can since AUTOLOAD can't.
132 my ($self, $target) = @_;
133 if(defined $target) {
135 return $self->__internal_type_constraint->can($target);
146 have meta examine the underlying type constraints
153 return $self->__internal_type_constraint->meta;
160 We might need it later
170 Delegate to the decorator targe
176 my ($self, @args) = @_;
177 my ($method) = (our $AUTOLOAD =~ /([^:]+)$/);
179 ## We delegate with this method in an attempt to support a value of
180 ## __type_constraint which is also AUTOLOADing, in particular the class
181 ## MooseX::Types::UndefinedType which AUTOLOADs during autovivication.
186 $return = $self->__internal_type_constraint->$method(@args);
194 =head1 AUTHOR AND COPYRIGHT
196 John Napiorkowski (jnapiorkowski) <jjnapiork@cpan.org>
200 This program is free software; you can redistribute it and/or modify
201 it under the same terms as perl itself.