2 package Moose::Meta::TypeCoercion;
10 use Moose::Meta::Attribute;
11 use Moose::Util::TypeConstraints ();
13 our $VERSION = '0.02';
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 $type_constraint = Moose::Util::TypeConstraints::find_type_constraint($constraint_name);
45 (defined $type_constraint)
46 || confess "Could not find the type constraint ($constraint_name) to coerce from";
48 $type_constraint->_compiled_type_constraint,
52 $self->_compiled_type_coercion(sub {
54 foreach my $coercion (@coercions) {
55 my ($constraint, $converter) = @$coercion;
56 if (defined $constraint->($thing)) {
58 return $converter->($thing);
65 sub coerce { $_[0]->_compiled_type_coercion->($_[1]) }
76 Moose::Meta::TypeCoercion - The Moose Type Coercion metaclass
80 For the most part, the only time you will ever encounter an
81 instance of this class is if you are doing some serious deep
82 introspection. This API should not be considered final, but
83 it is B<highly unlikely> that this will matter to a regular
86 If you wish to use features at this depth, please come to the
87 #moose IRC channel on irc.perl.org and we can talk :)
97 =item B<compile_type_coercion>
101 =item B<type_coercion_map>
103 =item B<type_constraint>
109 All complex software has bugs lurking in it, and this module is no
110 exception. If you find a bug please either email me, or add the bug
115 Stevan Little E<lt>stevan@iinteractive.comE<gt>
117 =head1 COPYRIGHT AND LICENSE
119 Copyright 2006 by Infinity Interactive, Inc.
121 L<http://www.iinteractive.com>
123 This library is free software; you can redistribute it and/or modify
124 it under the same terms as Perl itself.