cleaning up
[gitmo/Moose.git] / lib / Moose / Meta / TypeConstraint.pm
CommitLineData
4e036ee4 1
2package Moose::Meta::TypeConstraint;
3
4use strict;
5use warnings;
6use metaclass;
7
66811d63 8use Sub::Name 'subname';
9use Carp 'confess';
10
11our $VERSION = '0.01';
12
66811d63 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
17# private accessor
18__PACKAGE__->meta->add_attribute('compiled_type_constraint' => (
19 accessor => '_compiled_type_constraint'
20));
21
22__PACKAGE__->meta->add_attribute('coercion_code' => (
23 reader => 'coercion_code',
24 writer => 'set_coercion_code',
25 predicate => 'has_coercion'
26));
27
28sub new {
29 my $class = shift;
30 my $self = $class->meta->new_object(@_);
31 $self->compile_type_constraint();
32 return $self;
33}
34
35sub compile_type_constraint () {
36 my $self = shift;
37 my $check = $self->constraint;
38 (defined $check)
39 || confess "Could not compile type constraint '" . $self->name . "' because no constraint check";
40 my $parent = $self->parent;
41 if (defined $parent) {
42 $parent = $parent->_compiled_type_constraint;
43 $self->_compiled_type_constraint(subname $self->name => sub {
44 local $_ = $_[0];
45 return undef unless defined $parent->($_[0]) && $check->($_[0]);
46 $_[0];
47 });
48 }
49 else {
50 $self->_compiled_type_constraint(subname $self->name => sub {
51 local $_ = $_[0];
52 return undef unless $check->($_[0]);
53 $_[0];
54 });
55 }
56}
57
58# backwards for now
59sub constraint_code { (shift)->_compiled_type_constraint }
4e036ee4 60
611;
62
63__END__
64
65=pod
66
67=head1 NAME
68
69Moose::Meta::TypeConstraint - The Moose Type Constraint metaobject
70
71=head1 SYNOPSIS
72
73=head1 DESCRIPTION
74
75=head1 METHODS
76
77=over 4
78
79=item B<meta>
80
81=item B<new>
82
83=item B<name>
84
66811d63 85=item B<parent>
86
4e036ee4 87=item B<check>
88
66811d63 89=item B<constraint>
90
4e036ee4 91=item B<coerce>
92
93=item B<coercion_code>
94
95=item B<set_coercion_code>
96
97=item B<constraint_code>
98
99=item B<has_coercion>
100
66811d63 101=item B<compile_type_constraint>
102
4e036ee4 103=back
104
105=head1 BUGS
106
107All complex software has bugs lurking in it, and this module is no
108exception. If you find a bug please either email me, or add the bug
109to cpan-RT.
110
111=head1 AUTHOR
112
113Stevan Little E<lt>stevan@iinteractive.comE<gt>
114
115=head1 COPYRIGHT AND LICENSE
116
117Copyright 2006 by Infinity Interactive, Inc.
118
119L<http://www.iinteractive.com>
120
121This library is free software; you can redistribute it and/or modify
122it under the same terms as Perl itself.
123
124=cut