X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FMooseX%2FDependent%2FMeta%2FTypeCoercion%2FDependent.pm;h=564026721209c8e1364da4e6a4eb56f249d23ce5;hb=26cf337eb5c6275eddbcda39f7187416d6314b2f;hp=a2b718d8d564e1d3c95522ff48fe8f54c0573692;hpb=21df4517825e6c4bc201683264ecf7a3b8e25230;p=gitmo%2FMooseX-Dependent.git diff --git a/lib/MooseX/Dependent/Meta/TypeCoercion/Dependent.pm b/lib/MooseX/Dependent/Meta/TypeCoercion/Dependent.pm index a2b718d..5640267 100644 --- a/lib/MooseX/Dependent/Meta/TypeCoercion/Dependent.pm +++ b/lib/MooseX/Dependent/Meta/TypeCoercion/Dependent.pm @@ -16,6 +16,59 @@ TBD This class defines the following methods. +=head + +=cut + +sub coerce { + my $self = shift @_; + my $coderef = $self->_compiled_type_coercion; + return $coderef->(@_); +} + +around 'add_type_coercions' => sub { + my ($add_type_coercions, $self, @args) = @_; + if($self->type_constraint->has_constraining_value) { + Moose->throw_error("Cannot add type coercions to a dependent type constraint that's been defined."); + } else { + return $self->$add_type_coercions(@args); + } +}; + +sub compile_type_coercion { + my $self = shift; + my @coercion_map = @{$self->type_coercion_map}; + my @coercions; + while (@coercion_map) { + my ($constraint_name, $action) = splice(@coercion_map, 0, 2); + my $type_constraint = ref $constraint_name ? $constraint_name : Moose::Util::TypeConstraints::find_or_parse_type_constraint($constraint_name); + + unless ( defined $type_constraint ) { + require Moose; + Moose->throw_error("Could not find the type constraint ($constraint_name) to coerce from"); + } + + push @coercions => [ + $type_constraint->_compiled_type_constraint, + $action + ]; + } + $self->_compiled_type_coercion(sub { + my $thing = shift; + foreach my $coercion (@coercions) { + my ($constraint, $converter) = @$coercion; + if ($constraint->($thing)) { + local $_ = $thing; + return $converter->($thing, @_); + } + } + return $thing; + }); +} + + + + =head1 SEE ALSO The following modules or resources may be of interest.