1 package ## Hide from PAUSE
2 MooseX::Dependent::Meta::TypeConstraint::Dependent;
5 use Moose::Util::TypeConstraints ();
6 extends 'Moose::Meta::TypeConstraint';
10 MooseX::Dependent::Meta::TypeConstraint::Dependent - Metaclass for Dependent type constraints.
14 see L<MooseX::Dependent> for examples and details of how to use dependent
15 types. This class is a subclass of L<Moose::Meta::TypeConstraint> which
16 provides the gut functionality to enable dependent type constraints.
20 This class defines the following attributes.
22 =head2 parent_type_constraint
24 The type constraint whose validity is being made dependent.
28 has 'parent_type_constraint' => (
31 predicate=>'has_parent_type_constraint',
33 Moose::Util::TypeConstraints::find_type_constraint("Any");
38 =head2 constraining_value_type_constraint
40 This is a type constraint which defines what kind of value is allowed to be the
41 constraining value of the dependent type.
45 has 'constraining_value_type_constraint' => (
48 predicate=>'has_constraining_value_type_constraint',
50 Moose::Util::TypeConstraints::find_type_constraint("Any");
55 =head2 constrainting_value
57 This is the actual value that constraints the L</parent_type_constraint>
61 has 'constraining_value' => (
62 reader=>'constraining_value',
63 writer=>'_set_constraining_value',
64 predicate=>'has_constraining_value',
67 =head2 constraint_generator
69 A subref or closure that contains the way we validate incoming values against
70 a set of type constraints.
73 has 'constraint_generator' => (
76 predicate=>'has_constraint_generator',
82 This class defines the following methods.
86 We intercept validate in order to custom process the message.
88 override 'validate' => sub {
89 my ($self, @args) = @_;
90 my $compiled_type_constraint = $self->_compiled_type_constraint;
91 my $message = bless {message=>undef}, 'MooseX::Types::Dependent::Message';
92 my $result = $compiled_type_constraint->(@args, $message);
97 my $args = Devel::PartialDump::dump(@args);
98 if(my $message = $message->{message}) {
99 return $self->get_message("$args, Internal Validation Error is: $message");
101 return $self->get_message($args);
106 =head2 generate_constraint_for ($type_constraints)
108 Given some type constraints, use them to generate validation rules for an ref
109 of values (to be passed at check time)
112 sub generate_constraint_for {
113 my ($self, $callback) = @_;
115 my $dependent_pair = shift @_;
116 my ($dependent, $constraining) = @$dependent_pair;
118 ## First need to test the bits
119 unless($self->check_dependent($dependent)) {
120 $_[0]->{message} = $self->get_message_dependent($dependent)
125 unless($self->check_constraining($constraining)) {
126 $_[0]->{message} = $self->get_message_constraining($constraining)
131 my $constraint_generator = $self->constraint_generator;
132 return $constraint_generator->(
140 =head2 parameterize ($dependent, $callback, $constraining)
142 Given a ref of type constraints, create a structured type.
147 my ($self, $dependent_tc, $callback, $constraining_tc) = @_;
151 my $class = ref $self;
152 my $name = $self->_generate_subtype_name($dependent_tc, $callback, $constraining_tc);
153 my $constraint_generator = $self->__infer_constraint_generator;
158 dependent_type_constraint=>$dependent_tc,
159 comparison_callback=>$callback,
160 constraint_generator => $constraint_generator,
161 constraining_type_constraint => $constraining_tc,
165 =head2 _generate_subtype_name
167 Returns a name for the dependent type that should be unique
171 sub _generate_subtype_name {
172 my ($self, $parent_tc, $constraining_tc) = @_;
175 $parent_tc, $constraining_tc,
179 =head2 __infer_constraint_generator
181 This returns a CODEREF which generates a suitable constraint generator. Not
182 user servicable, you'll never call this directly.
184 TBD, this is definitely going to need some work. Cargo culted from some
185 code I saw in Moose::Meta::TypeConstraint::Parameterized or similar. I
186 Don't think I need this, since Dependent types require parameters, so
187 will always have a constrain generator.
191 sub __infer_constraint_generator {
193 if($self->has_constraint_generator) {
194 return $self->constraint_generator;
196 warn "I'm doing the questionable infer generator thing";
198 ## I'm not sure about this stuff but everything seems to work
204 $self->constraint->($merged_tc, @_);
209 =head2 compile_type_constraint
211 hook into compile_type_constraint so we can set the correct validation rules.
215 around 'compile_type_constraint' => sub {
216 my ($compile_type_constraint, $self) = @_;
218 if($self->has_comparison_callback &&
219 $self->has_constraining_type_constraint) {
220 my $generated_constraint = $self->generate_constraint_for(
221 $self->comparison_callback,
223 $self->_set_constraint($generated_constraint);
226 return $self->$compile_type_constraint;
229 =head2 create_child_type
231 modifier to make sure we get the constraint_generator
235 around 'create_child_type' => sub {
236 my ($create_child_type, $self, %opts) = @_;
237 return $self->$create_child_type(
239 #constraint_generator => $self->__infer_constraint_generator,
245 Override the base class behavior.
248 my ( $self, $type_or_name ) = @_;
249 my $other = Moose::Util::TypeConstraints::find_type_constraint("$type_or_name");
252 $other->isa(__PACKAGE__)
254 $self->dependent_type_constraint->equals($other)
256 $self->constraining_type_constraint->equals($other)
258 $self->parent->equals($other->parent)
264 Give you a better peek into what's causing the error.
266 around 'get_message' => sub {
267 my ($get_message, $self, $value) = @_;
268 return $self->$get_message($value);
271 =head2 _throw_error ($error)
273 Given a string, delegate to the Moose exception object
279 my $err = defined $_[0] ? $_[0] : 'Exception Thrown without Message';
280 require Moose; Moose->throw_error($err);
285 The following modules or resources may be of interest.
287 L<Moose>, L<Moose::Meta::TypeConstraint>
291 John Napiorkowski, C<< <jjnapiork@cpan.org> >>
293 =head1 COPYRIGHT & LICENSE
295 This program is free software; you can redistribute it and/or modify
296 it under the same terms as Perl itself.
300 __PACKAGE__->meta->make_immutable;