2 package Moose::Meta::TypeConstraint;
8 use Sub::Name 'subname';
10 use Scalar::Util 'blessed';
12 our $VERSION = '0.03';
14 __PACKAGE__->meta->add_attribute('name' => (reader => 'name' ));
15 __PACKAGE__->meta->add_attribute('parent' => (reader => 'parent' ));
16 __PACKAGE__->meta->add_attribute('constraint' => (reader => 'constraint'));
17 __PACKAGE__->meta->add_attribute('message' => (
18 accessor => 'message',
19 predicate => 'has_message'
21 __PACKAGE__->meta->add_attribute('coercion' => (
22 accessor => 'coercion',
23 predicate => 'has_coercion'
27 __PACKAGE__->meta->add_attribute('compiled_type_constraint' => (
28 accessor => '_compiled_type_constraint'
33 my $self = $class->meta->new_object(@_);
34 $self->compile_type_constraint();
38 sub compile_type_constraint {
40 my $check = $self->constraint;
42 || confess "Could not compile type constraint '" . $self->name . "' because no constraint check";
43 my $parent = $self->parent;
44 if (defined $parent) {
45 # we have a subtype ...
46 $parent = $parent->_compiled_type_constraint;
47 $self->_compiled_type_constraint(subname $self->name => sub {
49 return undef unless defined $parent->($_[0]) && $check->($_[0]);
55 $self->_compiled_type_constraint(subname $self->name => sub {
57 return undef unless $check->($_[0]);
63 sub check { $_[0]->_compiled_type_constraint->($_[1]) }
66 my ($self, $value) = @_;
67 if ($self->_compiled_type_constraint->($value)) {
71 if ($self->has_message) {
73 return $self->message->($value);
76 return "Validation failed for '" . $self->name . "' failed";
82 my ($self, $type_name) = @_;
84 while (my $parent = $current->parent) {
85 return 1 if $parent->name eq $type_name;
92 my ($class, @type_constraints) = @_;
93 (scalar @type_constraints >= 2)
94 || confess "You must pass in at least 2 Moose::Meta::TypeConstraint instances to make a union";
95 (blessed($_) && $_->isa('Moose::Meta::TypeConstraint'))
96 || confess "You must pass in only Moose::Meta::TypeConstraint instances to make unions"
97 foreach @type_constraints;
98 return Moose::Meta::TypeConstraint::Union->new(
99 type_constraints => \@type_constraints
103 package Moose::Meta::TypeConstraint::Union;
109 our $VERSION = '0.01';
111 __PACKAGE__->meta->add_attribute('type_constraints' => (
112 accessor => 'type_constraints',
113 default => sub { [] }
118 my $self = $class->meta->new_object(@_);
122 sub name { join ' | ' => map { $_->name } @{$_[0]->type_constraints} }
125 # this should probably never be used
126 # but we include it here for completeness
129 sub { $self->check($_[0]) };
132 # conform to the TypeConstraint API
134 sub coercion { undef }
135 sub has_coercion { 0 }
136 sub message { undef }
137 sub has_message { 0 }
142 foreach my $type (@{$self->type_constraints}) {
143 return 1 if $type->check($value);
152 foreach my $type (@{$self->type_constraints}) {
153 my $err = $type->validate($value);
154 return unless defined $err;
155 $message .= ($message ? ' and ' : '') . $err
158 return ($message . ' in (' . $self->name . ')') ;
169 Moose::Meta::TypeConstraint - The Moose Type Constraint metaclass
173 For the most part, the only time you will ever encounter an
174 instance of this class is if you are doing some serious deep
175 introspection. This API should not be considered final, but
176 it is B<highly unlikely> that this will matter to a regular
179 If you wish to use features at this depth, please come to the
180 #moose IRC channel on irc.perl.org and we can talk :)
190 =item B<is_subtype_of>
192 =item B<compile_type_constraint>
194 =item B<check ($value)>
196 This method will return a true (C<1>) if the C<$value> passes the
197 constraint, and false (C<0>) otherwise.
199 =item B<validate ($value)>
201 This method is similar to C<check>, but it deals with the error
202 message. If the C<$value> passes the constraint, C<undef> will be
203 returned. If the C<$value> does B<not> pass the constraint, then
204 the C<message> will be used to construct a custom error message.
216 =item B<has_coercion>
224 =item B<union (@type_constraints)>
230 All complex software has bugs lurking in it, and this module is no
231 exception. If you find a bug please either email me, or add the bug
236 Stevan Little E<lt>stevan@iinteractive.comE<gt>
238 =head1 COPYRIGHT AND LICENSE
240 Copyright 2006 by Infinity Interactive, Inc.
242 L<http://www.iinteractive.com>
244 This library is free software; you can redistribute it and/or modify
245 it under the same terms as Perl itself.