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' => (
33 predicate=>'has_dependent_type_constraint',
36 check_dependent=>'check',
40 =head2 constraining_type_constraint
42 This is a type constraint which defines what kind of value is allowed to be the
43 constraining value of the depending type.
47 has 'constraining_type_constraint' => (
50 predicate=>'has_constraining_type_constraint',
53 check_constraining=>'check',
57 =head2 comparison_callback
59 This is a callback which returns a boolean value. It get's passed the value
60 L</constraining_type_constraint> validates as well as the check value.
62 This callback is executed in addition to anything you put into a 'where' clause.
63 However, the 'where' clause only get's the check value.
65 Exercise some sanity, this should be limited to actual comparision operations,
66 not as a sneaky way to mess with the constraining value.
70 has 'comparison_callback' => (
73 predicate=>'has_comparison_callback',
77 =head2 constraint_generator
79 A subref or closure that contains the way we validate incoming values against
80 a set of type constraints.
84 has 'constraint_generator' => (
87 predicate=>'has_constraint_generator',
93 This class defines the following methods.
101 around 'new' => sub {
102 my ($new, $class, @args) = @_;
103 my $self = $class->$new(@args);
104 $self->coercion(MooseX::Meta::TypeCoercion::Dependent->new(
105 type_constraint => $self,
110 =head2 check($check_value, $constraining_value)
112 Make sure when properly dispatch all the right values to the right spots
116 around 'check' => sub {
117 my ($check, $self, $check_value, $constraining_value) = @_;
119 unless($self->check_dependent($check_value)) {
123 unless($self->check_constraining($constraining_value)) {
127 return $self->$check($check_value, $constraining_value);
130 =head2 generate_constraint_for ($type_constraints)
132 Given some type constraints, use them to generate validation rules for an ref
133 of values (to be passed at check time)
137 sub generate_constraint_for {
138 my ($self, $callback, $constraining) = @_;
140 my ($check_value, $constraining_value) = @_;
141 my $constraint_generator = $self->constraint_generator;
142 return $constraint_generator->(
150 =head2 parameterize ($dependent, $callback, $constraining)
152 Given a ref of type constraints, create a structured type.
157 my ($self, $dependent, $callback, $constraining) = @_;
158 my $class = ref $self;
159 my $name = $self->_generate_subtype_name($dependent, $callback, $constraining);
160 my $constraint_generator = $self->__infer_constraint_generator;
165 dependent_type_constraint=>$dependent,
166 comparison_callback=>$callback,
167 constraint_generator => $constraint_generator,
168 constraining_type_constraint => $constraining,
172 =head2 _generate_subtype_name
174 Returns a name for the dependent type that should be unique
178 sub _generate_subtype_name {
179 my ($self, $dependent, $callback, $constraining) = @_;
181 "%s_depends_on_%s_via_%s",
182 $dependent, $constraining, $callback
186 =head2 __infer_constraint_generator
188 This returns a CODEREF which generates a suitable constraint generator. Not
189 user servicable, you'll never call this directly.
191 TBD, this is definitely going to need some work.
195 sub __infer_constraint_generator {
197 if($self->has_constraint_generator) {
198 return $self->constraint_generator;
201 ## I'm not sure about this stuff but everything seems to work
205 $self->comparison_callback,
206 $self->constraining_type_constraint,
209 $self->constraint->($merged_tc, @_);
214 =head2 compile_type_constraint
216 hook into compile_type_constraint so we can set the correct validation rules.
220 around 'compile_type_constraint' => sub {
221 my ($compile_type_constraint, $self) = @_;
223 if($self->has_comparison_callback &&
224 $self->has_constraining_type_constraint) {
225 my $generated_constraint = $self->generate_constraint_for(
226 $self->comparison_callback,
227 $self->constraining_type_constraint,
229 $self->_set_constraint($generated_constraint);
232 return $self->$compile_type_constraint;
235 =head2 create_child_type
237 modifier to make sure we get the constraint_generator
239 around 'create_child_type' => sub {
240 my ($create_child_type, $self, %opts) = @_;
241 return $self->$create_child_type(
243 constraint_generator => $self->__infer_constraint_generator,
253 Override the base class behavior.
258 my ( $self, $type_or_name ) = @_;
259 my $other = Moose::Util::TypeConstraints::find_type_constraint($type_or_name);
261 return unless $other->isa(__PACKAGE__);
264 $self->type_constraints_equals($other)
266 $self->parent->equals( $other->parent )
270 =head2 type_constraints_equals
272 Checks to see if the internal type contraints are equal.
276 sub type_constraints_equals {
277 my ($self, $other) = @_;
278 my @self_type_constraints = @{$self->type_constraints||[]};
279 my @other_type_constraints = @{$other->type_constraints||[]};
281 ## Incoming ay be either arrayref or hashref, need top compare both
282 while(@self_type_constraints) {
283 my $self_type_constraint = shift @self_type_constraints;
284 my $other_type_constraint = shift @other_type_constraints
285 || return; ## $other needs the same number of children.
287 if( ref $self_type_constraint) {
288 $self_type_constraint->equals($other_type_constraint)
289 || return; ## type constraints obviously need top be equal
291 $self_type_constraint eq $other_type_constraint
292 || return; ## strings should be equal
297 return 1; ##If we get this far, everything is good.
302 Give you a better peek into what's causing the error. For now we stringify the
303 incoming deep value with L<Devel::PartialDump> and pass that on to either your
304 custom error message or the default one. In the future we'll try to provide a
305 more complete stack trace of the actual offending elements
309 around 'get_message' => sub {
310 my ($get_message, $self, $value) = @_;
311 my $new_value = Devel::PartialDump::dump($value);
312 return $self->$get_message($new_value);
317 The following modules or resources may be of interest.
319 L<Moose>, L<Moose::Meta::TypeConstraint>
323 John Napiorkowski, C<< <jjnapiork@cpan.org> >>
325 =head1 COPYRIGHT & LICENSE
327 This program is free software; you can redistribute it and/or modify
328 it under the same terms as Perl itself.
332 __PACKAGE__->meta->make_immutable;