2 package Moose::Meta::TypeCoercion;
10 use Moose::Meta::Attribute;
11 use Moose::Util::TypeConstraints ();
13 our $VERSION = '0.03';
14 our $AUTHORITY = 'cpan:STEVAN';
16 __PACKAGE__->meta->add_attribute('type_coercion_map' => (
17 reader => 'type_coercion_map',
21 __PACKAGE__->meta->add_attribute(
22 Moose::Meta::Attribute->new('type_constraint' => (
23 reader => 'type_constraint',
29 __PACKAGE__->meta->add_attribute('compiled_type_coercion' => (
30 accessor => '_compiled_type_coercion'
35 my $self = $class->meta->new_object(@_);
36 $self->compile_type_coercion();
40 sub compile_type_coercion {
42 my @coercion_map = @{$self->type_coercion_map};
44 while (@coercion_map) {
45 my ($constraint_name, $action) = splice(@coercion_map, 0, 2);
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";
50 $type_constraint->_compiled_type_constraint,
54 $self->_compiled_type_coercion(sub {
56 foreach my $coercion (@coercions) {
57 my ($constraint, $converter) = @$coercion;
58 if (defined $constraint->($thing)) {
60 return $converter->($thing);
67 sub coerce { $_[0]->_compiled_type_coercion->($_[1]) }
78 Moose::Meta::TypeCoercion - The Moose Type Coercion metaclass
82 For the most part, the only time you will ever encounter an
83 instance of this class is if you are doing some serious deep
84 introspection. This API should not be considered final, but
85 it is B<highly unlikely> that this will matter to a regular
88 If you wish to use features at this depth, please come to the
89 #moose IRC channel on irc.perl.org and we can talk :)
99 =item B<compile_type_coercion>
103 =item B<type_coercion_map>
105 =item B<type_constraint>
111 All complex software has bugs lurking in it, and this module is no
112 exception. If you find a bug please either email me, or add the bug
117 Stevan Little E<lt>stevan@iinteractive.comE<gt>
119 =head1 COPYRIGHT AND LICENSE
121 Copyright 2006, 2007 by Infinity Interactive, Inc.
123 L<http://www.iinteractive.com>
125 This library is free software; you can redistribute it and/or modify
126 it under the same terms as Perl itself.