0.18 ... pretty much ready to go
[gitmo/Moose.git] / lib / Moose / Meta / TypeCoercion.pm
CommitLineData
6bf30233 1
2package Moose::Meta::TypeCoercion;
3
4use strict;
5use warnings;
6use metaclass;
7
8use Carp 'confess';
9
a27aa600 10use Moose::Meta::Attribute;
a3c7e2fe 11use Moose::Util::TypeConstraints ();
a27aa600 12
d44714be 13our $VERSION = '0.03';
14our $AUTHORITY = 'cpan:STEVAN';
6bf30233 15
a27aa600 16__PACKAGE__->meta->add_attribute('type_coercion_map' => (
17 reader => 'type_coercion_map',
18 default => sub { [] }
19));
d44714be 20
a27aa600 21__PACKAGE__->meta->add_attribute(
22 Moose::Meta::Attribute->new('type_constraint' => (
23 reader => 'type_constraint',
24 weak_ref => 1
25 ))
26);
27
28# private accessor
29__PACKAGE__->meta->add_attribute('compiled_type_coercion' => (
30 accessor => '_compiled_type_coercion'
31));
32
33sub new {
34 my $class = shift;
35 my $self = $class->meta->new_object(@_);
36 $self->compile_type_coercion();
37 return $self;
38}
39
40sub compile_type_coercion {
41 my $self = shift;
42 my @coercion_map = @{$self->type_coercion_map};
43 my @coercions;
44 while (@coercion_map) {
45 my ($constraint_name, $action) = splice(@coercion_map, 0, 2);
e95c7c42 46 my $type_constraint = Moose::Util::TypeConstraints::find_type_constraint($constraint_name);
47 (defined $type_constraint)
48 || confess "Could not find the type constraint ($constraint_name) to coerce from";
49 push @coercions => [
50 $type_constraint->_compiled_type_constraint,
51 $action
52 ];
a27aa600 53 }
54 $self->_compiled_type_coercion(sub {
55 my $thing = shift;
56 foreach my $coercion (@coercions) {
57 my ($constraint, $converter) = @$coercion;
58 if (defined $constraint->($thing)) {
59 local $_ = $thing;
60 return $converter->($thing);
61 }
62 }
63 return $thing;
64 });
65}
66
67sub coerce { $_[0]->_compiled_type_coercion->($_[1]) }
68
69
6bf30233 701;
71
72__END__
73
74=pod
75
76=head1 NAME
77
6ba6d68c 78Moose::Meta::TypeCoercion - The Moose Type Coercion metaclass
6bf30233 79
80=head1 DESCRIPTION
81
6ba6d68c 82For the most part, the only time you will ever encounter an
83instance of this class is if you are doing some serious deep
84introspection. This API should not be considered final, but
85it is B<highly unlikely> that this will matter to a regular
86Moose user.
87
88If you wish to use features at this depth, please come to the
89#moose IRC channel on irc.perl.org and we can talk :)
90
6bf30233 91=head1 METHODS
92
93=over 4
94
95=item B<meta>
96
a27aa600 97=item B<new>
98
a27aa600 99=item B<compile_type_coercion>
100
6ba6d68c 101=item B<coerce>
102
a27aa600 103=item B<type_coercion_map>
104
105=item B<type_constraint>
106
6bf30233 107=back
108
109=head1 BUGS
110
111All complex software has bugs lurking in it, and this module is no
112exception. If you find a bug please either email me, or add the bug
113to cpan-RT.
114
115=head1 AUTHOR
116
117Stevan Little E<lt>stevan@iinteractive.comE<gt>
118
119=head1 COPYRIGHT AND LICENSE
120
b77fdbed 121Copyright 2006, 2007 by Infinity Interactive, Inc.
6bf30233 122
123L<http://www.iinteractive.com>
124
125This library is free software; you can redistribute it and/or modify
126it under the same terms as Perl itself.
127
128=cut