2 package Moose::Meta::TypeConstraint;
8 use Sub::Name 'subname';
11 our $VERSION = '0.03';
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";
81 my ($class, @type_constraints) = @_;
82 return Moose::Meta::TypeConstraint::Union->new(
83 type_constraints => \@type_constraints
87 package Moose::Meta::TypeConstraint::Union;
93 our $VERSION = '0.01';
95 __PACKAGE__->meta->add_attribute('type_constraints' => (
96 accessor => 'type_constraints',
102 my $self = $class->meta->new_object(@_);
106 sub name { join ' | ' => map { $_->name } @{$_[0]->type_constraints} }
111 foreach my $type (@{$self->type_constraints}) {
112 return 1 if $type->check($value);
121 foreach my $type (@{$self->type_constraints}) {
122 my $err = $type->validate($value);
123 return unless defined $err;
124 $message .= ($message ? ' and ' : '') . $err
127 return ($message . ' in (' . $self->name . ')') ;
138 Moose::Meta::TypeConstraint - The Moose Type Constraint metaclass
142 For the most part, the only time you will ever encounter an
143 instance of this class is if you are doing some serious deep
144 introspection. This API should not be considered final, but
145 it is B<highly unlikely> that this will matter to a regular
148 If you wish to use features at this depth, please come to the
149 #moose IRC channel on irc.perl.org and we can talk :)
159 =item B<compile_type_constraint>
161 =item B<check ($value)>
163 This method will return a true (C<1>) if the C<$value> passes the
164 constraint, and false (C<0>) otherwise.
166 =item B<validate ($value)>
168 This method is similar to C<check>, but it deals with the error
169 message. If the C<$value> passes the constraint, C<undef> will be
170 returned. If the C<$value> does B<not> pass the constraint, then
171 the C<message> will be used to construct a custom error message.
183 =item B<has_coercion>
191 =item B<union (@type_constraints)>
197 All complex software has bugs lurking in it, and this module is no
198 exception. If you find a bug please either email me, or add the bug
203 Stevan Little E<lt>stevan@iinteractive.comE<gt>
205 =head1 COPYRIGHT AND LICENSE
207 Copyright 2006 by Infinity Interactive, Inc.
209 L<http://www.iinteractive.com>
211 This library is free software; you can redistribute it and/or modify
212 it under the same terms as Perl itself.