1 package ## Hide from PAUSE
2 MooseX::Meta::TypeConstraint::Dependent;
5 use Moose::Util::TypeConstraints ();
6 use MooseX::Meta::TypeCoercion::Dependent;
7 extends 'Moose::Meta::TypeConstraint';
11 MooseX::Meta::TypeConstraint::Dependent - Metaclass for Dependent type constraints.
15 see L<MooseX::Types::Dependent> for examples and details of how to use dependent
16 types. This class is a subclass of L<Moose::Meta::TypeConstraint> which
17 provides the gut functionality to enable dependent type constraints.
21 This class defines the following attributes.
23 =head2 dependent_type_constraint
25 The type constraint whose validity is being made dependent on a value that is a
26 L</constraining_type_constraint>
30 has 'dependent_type_constraint' => (
32 predicate=>'has_dependent_type_constraint',
35 =head2 constraining_type_constraint
37 This is a type constraint which defines what kind of value is allowed to be the
38 constraining value of the depending type.
42 has 'constraining_type_constraint' => (
44 predicate=>'has_constraining_type_constraint',
47 =head2 comparision_callback
49 This is a callback which returns a boolean value. It get's passed the value
50 L</constraining_type_constraint> validates as well as the check value.
52 This callback is executed in addition to anything you put into a 'where' clause.
53 However, the 'where' clause only get's the check value.
55 Exercise some sanity, this should be limited to actual comparision operations,
56 not as a sneaky way to mess with the constraining value.
60 has 'comparision_callback' => (
63 predicate=>'has_comparision_callback',
66 =head2 constraint_generator
68 A subref or closure that contains the way we validate incoming values against
69 a set of type constraints.
73 has 'constraint_generator' => (
76 predicate=>'has_constraint_generator',
81 This class defines the following methods.
90 my ($new, $class, @args) = @_;
91 my $self = $class->$new(@args);
92 $self->coercion(MooseX::Meta::TypeCoercion::Structured->new(
93 type_constraint => $self,
98 =head2 generate_constraint_for ($type_constraints)
100 Given some type constraints, use them to generate validation rules for an ref
101 of values (to be passed at check time)
105 sub generate_constraint_for {
106 my ($self, $dependent, $callback, $constraining) = @_;
109 my $constraint_generator = $self->constraint_generator;
110 return $constraint_generator->($dependent, $callback, $constraining, @args);
114 =head2 parameterize (@type_constraints)
116 Given a ref of type constraints, create a structured type.
122 my ($self, $dependent, $callback, $constraining) = @_;
123 my $class = ref $self;
124 my $name = $self->_generate_subtype_name($dependent, $constraining);
125 my $constraint_generator = $self->__infer_constraint_generator;
130 dependent_type_constraint=>$dependent,
131 comparision_callback=>$callback,
132 constraint_generator => $constraint_generator,
136 =head2 _generate_subtype_name
138 Returns a name for the dependent type that should be unique
142 sub _generate_subtype_name {
143 my ($self, $dependent, $constraining) = @_;
146 $dependent, $constraining
150 =head2 __infer_constraint_generator
152 This returns a CODEREF which generates a suitable constraint generator. Not
153 user servicable, you'll never call this directly.
155 TBD, this is definitely going to need some work.
159 sub __infer_constraint_generator {
161 if($self->has_constraint_generator) {
162 return $self->constraint_generator;
165 ## I'm not sure about this stuff but everything seems to work
169 $self->dependent_type_constraint,
170 $self->comparision_callback,
171 $self->constraining_type_constraint,
174 $self->constraint->($merged_tc, @_);
179 =head2 compile_type_constraint
181 hook into compile_type_constraint so we can set the correct validation rules.
185 around 'compile_type_constraint' => sub {
186 my ($compile_type_constraint, $self, @args) = @_;
188 if($self->has_type_constraints) {
189 my $type_constraints = $self->type_constraints;
190 my $constraint = $self->generate_constraint_for($type_constraints);
191 $self->_set_constraint($constraint);
194 return $self->$compile_type_constraint(@args);
197 =head2 create_child_type
199 modifier to make sure we get the constraint_generator
203 around 'create_child_type' => sub {
204 my ($create_child_type, $self, %opts) = @_;
205 return $self->$create_child_type(
207 constraint_generator => $self->__infer_constraint_generator,
217 Override the base class behavior.
222 my ( $self, $type_or_name ) = @_;
223 my $other = Moose::Util::TypeConstraints::find_type_constraint($type_or_name);
225 return unless $other->isa(__PACKAGE__);
228 $self->type_constraints_equals($other)
230 $self->parent->equals( $other->parent )
234 =head2 type_constraints_equals
236 Checks to see if the internal type contraints are equal.
240 sub type_constraints_equals {
241 my ($self, $other) = @_;
242 my @self_type_constraints = @{$self->type_constraints||[]};
243 my @other_type_constraints = @{$other->type_constraints||[]};
245 ## Incoming ay be either arrayref or hashref, need top compare both
246 while(@self_type_constraints) {
247 my $self_type_constraint = shift @self_type_constraints;
248 my $other_type_constraint = shift @other_type_constraints
249 || return; ## $other needs the same number of children.
251 if( ref $self_type_constraint) {
252 $self_type_constraint->equals($other_type_constraint)
253 || return; ## type constraints obviously need top be equal
255 $self_type_constraint eq $other_type_constraint
256 || return; ## strings should be equal
261 return 1; ##If we get this far, everything is good.
266 Give you a better peek into what's causing the error. For now we stringify the
267 incoming deep value with L<Devel::PartialDump> and pass that on to either your
268 custom error message or the default one. In the future we'll try to provide a
269 more complete stack trace of the actual offending elements
273 around 'get_message' => sub {
274 my ($get_message, $self, $value) = @_;
275 my $new_value = Devel::PartialDump::dump($value);
276 return $self->$get_message($new_value);
281 The following modules or resources may be of interest.
283 L<Moose>, L<Moose::Meta::TypeConstraint>
287 John Napiorkowski, C<< <jjnapiork@cpan.org> >>
289 =head1 COPYRIGHT & LICENSE
291 This program is free software; you can redistribute it and/or modify
292 it under the same terms as Perl itself.
296 __PACKAGE__->meta->make_immutable;