Commit | Line | Data |
4e036ee4 |
1 | |
2 | package Moose::Meta::TypeConstraint; |
3 | |
4 | use strict; |
5 | use warnings; |
6 | use metaclass; |
7 | |
66811d63 |
8 | use Sub::Name 'subname'; |
9 | use Carp 'confess'; |
10 | |
11 | our $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 | |
28 | sub new { |
29 | my $class = shift; |
30 | my $self = $class->meta->new_object(@_); |
31 | $self->compile_type_constraint(); |
32 | return $self; |
33 | } |
34 | |
35 | sub 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 |
59 | sub constraint_code { (shift)->_compiled_type_constraint } |
4e036ee4 |
60 | |
61 | 1; |
62 | |
63 | __END__ |
64 | |
65 | =pod |
66 | |
67 | =head1 NAME |
68 | |
69 | Moose::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 | |
107 | All complex software has bugs lurking in it, and this module is no |
108 | exception. If you find a bug please either email me, or add the bug |
109 | to cpan-RT. |
110 | |
111 | =head1 AUTHOR |
112 | |
113 | Stevan Little E<lt>stevan@iinteractive.comE<gt> |
114 | |
115 | =head1 COPYRIGHT AND LICENSE |
116 | |
117 | Copyright 2006 by Infinity Interactive, Inc. |
118 | |
119 | L<http://www.iinteractive.com> |
120 | |
121 | This library is free software; you can redistribute it and/or modify |
122 | it under the same terms as Perl itself. |
123 | |
124 | =cut |