1 package MooseX::Meta::TypeConstraint::Structured;
4 use Moose::Util::TypeConstraints ();
5 use MooseX::Meta::TypeCoercion::Structured;
6 extends 'Moose::Meta::TypeConstraint';
10 MooseX::Meta::TypeConstraint::Structured - Structured type constraints.
14 A structure is a set of L<Moose::Meta::TypeConstraint> that are 'aggregated' in
15 such a way as that they are all applied to an incoming list of arguments. The
16 idea here is that a Type Constraint could be something like, "An Int followed by
17 an Int and then a Str" and that this could be done so with a declaration like:
19 Tuple[Int,Int,Str]; ## Example syntax
21 So a structure is a list of Type constraints (the "Int,Int,Str" in the above
22 example) which are intended to function together.
26 This class defines the following attributes.
28 =head2 type_constraints
30 A list of L<Moose::Meta::TypeConstraint> objects.
34 has 'type_constraints' => (
37 predicate=>'has_type_constraints',
40 =head2 constraint_generator
42 A subref or closure that contains the way we validate incoming values against
43 a set of type constraints.
47 has 'constraint_generator' => (is=>'ro', isa=>'CodeRef');
51 This class defines the following methods.
60 my ($new, $class, @args) = @_;
61 my $self = $class->$new(@args);
62 $self->coercion(MooseX::Meta::TypeCoercion::Structured->new(
63 type_constraint => $self,
68 =head2 generate_constraint_for ($type_constraints)
70 Given some type constraints, use them to generate validation rules for an ref
71 of values (to be passed at check time)
75 sub generate_constraint_for {
76 my ($self, $type_constraints) = @_;
78 my $constraint_generator = $self->constraint_generator;
79 return $constraint_generator->($type_constraints, @_);
83 =head2 parameterize (@type_constraints)
85 Given a ref of type constraints, create a structured type.
90 my ($self, @type_constraints) = @_;
91 my $class = ref $self;
92 my $name = $self->name .'['. join(',', map {"$_"} @type_constraints) .']';
97 type_constraints => \@type_constraints,
98 constraint_generator => $self->constraint_generator || sub {
100 my $merged_tc = [@$tc, @{$self->parent->type_constraints}];
101 $self->constraint->($merged_tc, @_);
106 =head2 compile_type_constraint
108 hook into compile_type_constraint so we can set the correct validation rules.
112 around 'compile_type_constraint' => sub {
113 my ($compile_type_constraint, $self, @args) = @_;
115 if($self->has_type_constraints) {
116 my $type_constraints = $self->type_constraints;
117 my $constraint = $self->generate_constraint_for($type_constraints);
118 $self->_set_constraint($constraint);
121 return $self->$compile_type_constraint(@args);
124 =head2 create_child_type
126 modifier to make sure we get the constraint_generator
130 around 'create_child_type' => sub {
131 my ($create_child_type, $self, %opts) = @_;
132 return $self->$create_child_type(
134 constraint_generator => $self->constraint_generator,
144 Override the base class behavior.
149 my ( $self, $type_or_name ) = @_;
150 my $other = Moose::Util::TypeConstraints::find_type_constraint($type_or_name);
152 return unless $other->isa(__PACKAGE__);
155 $self->type_constraints_equals($other)
157 $self->parent->equals( $other->parent )
161 =head2 type_constraints_equals
163 Checks to see if the internal type contraints are equal.
167 sub type_constraints_equals {
168 my ($self, $other) = @_;
169 my @self_type_constraints = @{$self->type_constraints||[]};
170 my @other_type_constraints = @{$other->type_constraints||[]};
172 ## Incoming ay be either arrayref or hashref, need top compare both
173 while(@self_type_constraints) {
174 my $self_type_constraint = shift @self_type_constraints;
175 my $other_type_constraint = shift @other_type_constraints
176 || return; ## $other needs the same number of children.
178 if( ref $self_type_constraint) {
179 $self_type_constraint->equals($other_type_constraint)
180 || return; ## type constraints obviously need top be equal
182 $self_type_constraint eq $other_type_constraint
183 || return; ## strings should be equal
188 return 1; ##If we get this far, everything is good.
193 May want to override this to set a more useful error message
197 The following modules or resources may be of interest.
199 L<Moose>, L<Moose::Meta::TypeConstraint>
203 John Napiorkowski, C<< <jjnapiork@cpan.org> >>
205 =head1 COPYRIGHT & LICENSE
207 This program is free software; you can redistribute it and/or modify
208 it under the same terms as Perl itself.