2 package Moose::Meta::TypeCoercion;
10 use Moose::Meta::Attribute;
11 use Moose::Util::TypeConstraints ();
13 our $VERSION = '0.01';
15 __PACKAGE__->meta->add_attribute('type_coercion_map' => (
16 reader => 'type_coercion_map',
19 __PACKAGE__->meta->add_attribute(
20 Moose::Meta::Attribute->new('type_constraint' => (
21 reader => 'type_constraint',
27 __PACKAGE__->meta->add_attribute('compiled_type_coercion' => (
28 accessor => '_compiled_type_coercion'
33 my $self = $class->meta->new_object(@_);
34 $self->compile_type_coercion();
38 sub compile_type_coercion {
40 my @coercion_map = @{$self->type_coercion_map};
42 while (@coercion_map) {
43 my ($constraint_name, $action) = splice(@coercion_map, 0, 2);
44 my $constraint = Moose::Util::TypeConstraints::find_type_constraint($constraint_name)->_compiled_type_constraint;
46 || confess "Could not find the type constraint ($constraint_name)";
47 push @coercions => [ $constraint, $action ];
49 $self->_compiled_type_coercion(sub {
51 foreach my $coercion (@coercions) {
52 my ($constraint, $converter) = @$coercion;
53 if (defined $constraint->($thing)) {
55 return $converter->($thing);
62 sub coerce { $_[0]->_compiled_type_coercion->($_[1]) }
73 Moose::Meta::TypeCoercion - The Moose Type Coercion metaclass
77 For the most part, the only time you will ever encounter an
78 instance of this class is if you are doing some serious deep
79 introspection. This API should not be considered final, but
80 it is B<highly unlikely> that this will matter to a regular
83 If you wish to use features at this depth, please come to the
84 #moose IRC channel on irc.perl.org and we can talk :)
94 =item B<compile_type_coercion>
98 =item B<type_coercion_map>
100 =item B<type_constraint>
106 All complex software has bugs lurking in it, and this module is no
107 exception. If you find a bug please either email me, or add the bug
112 Stevan Little E<lt>stevan@iinteractive.comE<gt>
114 =head1 COPYRIGHT AND LICENSE
116 Copyright 2006 by Infinity Interactive, Inc.
118 L<http://www.iinteractive.com>
120 This library is free software; you can redistribute it and/or modify
121 it under the same terms as Perl itself.