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