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 metaobject
89 =item B<compile_type_coercion>
91 =item B<type_coercion_map>
93 =item B<type_constraint>
99 All complex software has bugs lurking in it, and this module is no
100 exception. If you find a bug please either email me, or add the bug
105 Stevan Little E<lt>stevan@iinteractive.comE<gt>
107 =head1 COPYRIGHT AND LICENSE
109 Copyright 2006 by Infinity Interactive, Inc.
111 L<http://www.iinteractive.com>
113 This library is free software; you can redistribute it and/or modify
114 it under the same terms as Perl itself.