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',
35 check_dependent=>'check',
39 =head2 constraining_type_constraint
41 This is a type constraint which defines what kind of value is allowed to be the
42 constraining value of the depending type.
46 has 'constraining_type_constraint' => (
49 predicate=>'has_constraining_type_constraint',
51 check_constraining=>'check',
55 =head2 comparison_callback
57 This is a callback which returns a boolean value. It get's passed the value
58 L</constraining_type_constraint> validates as well as the check value.
60 This callback is executed in addition to anything you put into a 'where' clause.
61 However, the 'where' clause only get's the check value.
63 Exercise some sanity, this should be limited to actual comparision operations,
64 not as a sneaky way to mess with the constraining value.
68 has 'comparison_callback' => (
71 predicate=>'has_comparison_callback',
74 =head2 constraint_generator
76 A subref or closure that contains the way we validate incoming values against
77 a set of type constraints.
81 has 'constraint_generator' => (
84 predicate=>'has_constraint_generator',
90 This class defines the following methods.
99 my ($new, $class, @args) = @_;
100 my $self = $class->$new(@args);
101 $self->coercion(MooseX::Meta::TypeCoercion::Dependent->new(
102 type_constraint => $self,
107 =head2 generate_constraint_for ($type_constraints)
109 Given some type constraints, use them to generate validation rules for an ref
110 of values (to be passed at check time)
114 sub generate_constraint_for {
115 my ($self, $callback) = @_;
117 my ($dependent_pair) = @_;
118 my ($dependent, $constraining) = @$dependent_pair;
120 ## First need to test the bits
121 unless($self->check_dependent($dependent)) {
125 unless($self->check_constraining($constraining)) {
129 my $constraint_generator = $self->constraint_generator;
130 return $constraint_generator->(
138 =head2 parameterize ($dependent, $callback, $constraining)
140 Given a ref of type constraints, create a structured type.
145 my ($self, $dependent_tc, $callback, $constraining_tc) = @_;
146 my $class = ref $self;
147 my $name = $self->_generate_subtype_name($dependent_tc, $callback, $constraining_tc);
148 my $constraint_generator = $self->__infer_constraint_generator;
153 dependent_type_constraint=>$dependent_tc,
154 comparison_callback=>$callback,
155 constraint_generator => $constraint_generator,
156 constraining_type_constraint => $constraining_tc,
160 =head2 _generate_subtype_name
162 Returns a name for the dependent type that should be unique
166 sub _generate_subtype_name {
167 my ($self, $dependent_tc, $callback, $constraining_tc) = @_;
169 "%s_depends_on_%s_via_%s",
170 $dependent_tc, $constraining_tc, $callback,
174 =head2 __infer_constraint_generator
176 This returns a CODEREF which generates a suitable constraint generator. Not
177 user servicable, you'll never call this directly.
179 TBD, this is definitely going to need some work.
183 sub __infer_constraint_generator {
185 if($self->has_constraint_generator) {
186 return $self->constraint_generator;
188 warn "I'm doing the questioning infer generator thing";
190 ## I'm not sure about this stuff but everything seems to work
194 $self->comparison_callback,
195 $self->constraining_type_constraint,
198 $self->constraint->($merged_tc, @_);
203 =head2 compile_type_constraint
205 hook into compile_type_constraint so we can set the correct validation rules.
209 around 'compile_type_constraint' => sub {
210 my ($compile_type_constraint, $self) = @_;
212 if($self->has_comparison_callback &&
213 $self->has_constraining_type_constraint) {
214 my $generated_constraint = $self->generate_constraint_for(
215 $self->comparison_callback,
217 $self->_set_constraint($generated_constraint);
220 return $self->$compile_type_constraint;
223 =head2 create_child_type
225 modifier to make sure we get the constraint_generator
229 around 'create_child_type' => sub {
230 my ($create_child_type, $self, %opts) = @_;
231 return $self->$create_child_type(
233 constraint_generator => $self->__infer_constraint_generator,
243 Override the base class behavior.
248 my ( $self, $type_or_name ) = @_;
249 my $other = Moose::Util::TypeConstraints::find_type_constraint($type_or_name);
251 return unless $other->isa(__PACKAGE__);
254 $self->type_constraints_equals($other)
256 $self->parent->equals( $other->parent )
260 =head2 type_constraints_equals
262 Checks to see if the internal type contraints are equal.
266 sub type_constraints_equals {
267 my ($self, $other) = @_;
268 my @self_type_constraints = @{$self->type_constraints||[]};
269 my @other_type_constraints = @{$other->type_constraints||[]};
271 ## Incoming ay be either arrayref or hashref, need top compare both
272 while(@self_type_constraints) {
273 my $self_type_constraint = shift @self_type_constraints;
274 my $other_type_constraint = shift @other_type_constraints
275 || return; ## $other needs the same number of children.
277 if( ref $self_type_constraint) {
278 $self_type_constraint->equals($other_type_constraint)
279 || return; ## type constraints obviously need top be equal
281 $self_type_constraint eq $other_type_constraint
282 || return; ## strings should be equal
287 return 1; ##If we get this far, everything is good.
292 Give you a better peek into what's causing the error. For now we stringify the
293 incoming deep value with L<Devel::PartialDump> and pass that on to either your
294 custom error message or the default one. In the future we'll try to provide a
295 more complete stack trace of the actual offending elements
299 around 'get_message' => sub {
300 my ($get_message, $self, $value) = @_;
301 my $new_value = Devel::PartialDump::dump($value);
302 return $self->$get_message($new_value);
307 The following modules or resources may be of interest.
309 L<Moose>, L<Moose::Meta::TypeConstraint>
313 John Napiorkowski, C<< <jjnapiork@cpan.org> >>
315 =head1 COPYRIGHT & LICENSE
317 This program is free software; you can redistribute it and/or modify
318 it under the same terms as Perl itself.
322 __PACKAGE__->meta->make_immutable;