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')); |
a27aa600 |
16 | __PACKAGE__->meta->add_attribute('coercion' => ( |
17 | accessor => 'coercion', |
18 | predicate => 'has_coercion' |
19 | )); |
66811d63 |
20 | |
21 | # private accessor |
22 | __PACKAGE__->meta->add_attribute('compiled_type_constraint' => ( |
23 | accessor => '_compiled_type_constraint' |
24 | )); |
25 | |
66811d63 |
26 | sub new { |
a27aa600 |
27 | my $class = shift; |
28 | my $self = $class->meta->new_object(@_); |
66811d63 |
29 | $self->compile_type_constraint(); |
30 | return $self; |
31 | } |
32 | |
33 | sub compile_type_constraint () { |
a27aa600 |
34 | my $self = shift; |
35 | my $check = $self->constraint; |
66811d63 |
36 | (defined $check) |
37 | || confess "Could not compile type constraint '" . $self->name . "' because no constraint check"; |
38 | my $parent = $self->parent; |
39 | if (defined $parent) { |
a27aa600 |
40 | # we have a subtype ... |
66811d63 |
41 | $parent = $parent->_compiled_type_constraint; |
42 | $self->_compiled_type_constraint(subname $self->name => sub { |
43 | local $_ = $_[0]; |
44 | return undef unless defined $parent->($_[0]) && $check->($_[0]); |
45 | $_[0]; |
46 | }); |
47 | } |
48 | else { |
a27aa600 |
49 | # we have a type .... |
66811d63 |
50 | $self->_compiled_type_constraint(subname $self->name => sub { |
51 | local $_ = $_[0]; |
52 | return undef unless $check->($_[0]); |
53 | $_[0]; |
54 | }); |
55 | } |
56 | } |
57 | |
a27aa600 |
58 | sub check { $_[0]->_compiled_type_constraint->($_[1]) } |
4e036ee4 |
59 | |
60 | 1; |
61 | |
62 | __END__ |
63 | |
64 | =pod |
65 | |
66 | =head1 NAME |
67 | |
6ba6d68c |
68 | Moose::Meta::TypeConstraint - The Moose Type Constraint metaclass |
4e036ee4 |
69 | |
70 | =head1 DESCRIPTION |
71 | |
6ba6d68c |
72 | For the most part, the only time you will ever encounter an |
73 | instance of this class is if you are doing some serious deep |
74 | introspection. This API should not be considered final, but |
75 | it is B<highly unlikely> that this will matter to a regular |
76 | Moose user. |
77 | |
78 | If you wish to use features at this depth, please come to the |
79 | #moose IRC channel on irc.perl.org and we can talk :) |
80 | |
4e036ee4 |
81 | =head1 METHODS |
82 | |
83 | =over 4 |
84 | |
85 | =item B<meta> |
86 | |
87 | =item B<new> |
88 | |
6ba6d68c |
89 | =item B<compile_type_constraint> |
90 | |
91 | =item B<check> |
92 | |
4e036ee4 |
93 | =item B<name> |
94 | |
66811d63 |
95 | =item B<parent> |
96 | |
66811d63 |
97 | =item B<constraint> |
98 | |
4e036ee4 |
99 | =item B<has_coercion> |
100 | |
a27aa600 |
101 | =item B<coercion> |
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 |