2 package Moose::Meta::TypeConstraint;
8 use Sub::Name 'subname';
11 our $VERSION = '0.02';
13 __PACKAGE__->meta->add_attribute('name' => (reader => 'name' ));
14 __PACKAGE__->meta->add_attribute('parent' => (reader => 'parent' ));
15 __PACKAGE__->meta->add_attribute('constraint' => (reader => 'constraint'));
16 __PACKAGE__->meta->add_attribute('message' => (
17 accessor => 'message',
18 predicate => 'has_message'
20 __PACKAGE__->meta->add_attribute('coercion' => (
21 accessor => 'coercion',
22 predicate => 'has_coercion'
26 __PACKAGE__->meta->add_attribute('compiled_type_constraint' => (
27 accessor => '_compiled_type_constraint'
32 my $self = $class->meta->new_object(@_);
33 $self->compile_type_constraint();
37 sub compile_type_constraint () {
39 my $check = $self->constraint;
41 || confess "Could not compile type constraint '" . $self->name . "' because no constraint check";
42 my $parent = $self->parent;
43 if (defined $parent) {
44 # we have a subtype ...
45 $parent = $parent->_compiled_type_constraint;
46 $self->_compiled_type_constraint(subname $self->name => sub {
48 return undef unless defined $parent->($_[0]) && $check->($_[0]);
54 $self->_compiled_type_constraint(subname $self->name => sub {
56 return undef unless $check->($_[0]);
62 sub check { $_[0]->_compiled_type_constraint->($_[1]) }
65 my ($self, $value) = @_;
66 if ($self->_compiled_type_constraint->($value)) {
70 if ($self->has_message) {
72 return $self->message->($value);
75 return "Validation failed for '" . $self->name . "' failed.";
88 Moose::Meta::TypeConstraint - The Moose Type Constraint metaclass
92 For the most part, the only time you will ever encounter an
93 instance of this class is if you are doing some serious deep
94 introspection. This API should not be considered final, but
95 it is B<highly unlikely> that this will matter to a regular
98 If you wish to use features at this depth, please come to the
99 #moose IRC channel on irc.perl.org and we can talk :)
109 =item B<compile_type_constraint>
111 =item B<check ($value)>
113 This method will return a true (C<1>) if the C<$value> passes the
114 constraint, and false (C<0>) otherwise.
116 =item B<validate ($value)>
118 This method is similar to C<check>, but it deals with the error
119 message. If the C<$value> passes the constraint, C<undef> will be
120 returned. If the C<$value> does B<not> pass the constraint, then
121 the C<message> will be used to construct a custom error message.
133 =item B<has_coercion>
141 All complex software has bugs lurking in it, and this module is no
142 exception. If you find a bug please either email me, or add the bug
147 Stevan Little E<lt>stevan@iinteractive.comE<gt>
149 =head1 COPYRIGHT AND LICENSE
151 Copyright 2006 by Infinity Interactive, Inc.
153 L<http://www.iinteractive.com>
155 This library is free software; you can redistribute it and/or modify
156 it under the same terms as Perl itself.