| 1 | package Type::Coercion::FromMoose; | ||
| 2 | |||
| 3 | use 5.008001; | ||
| 4 | use strict; | ||
| 5 | use warnings; | ||
| 6 | |||
| 7 | BEGIN { | ||
| 8 | $Type::Coercion::FromMoose::AUTHORITY = 'cpan:TOBYINK'; | ||
| 9 | $Type::Coercion::FromMoose::VERSION = '2.010001'; | ||
| 10 | } | ||
| 11 | |||
| 12 | $Type::Coercion::FromMoose::VERSION =~ tr/_//d; | ||
| 13 | |||
| 14 | use Scalar::Util qw< blessed >; | ||
| 15 | use Types::TypeTiny (); | ||
| 16 | |||
| 17 | sub _croak ($;@) { require Error::TypeTiny; goto \&Error::TypeTiny::croak } | ||
| 18 | |||
| 19 | require Type::Coercion; | ||
| 20 | our @ISA = 'Type::Coercion'; | ||
| 21 | |||
| 22 | sub type_coercion_map { | ||
| 23 | my $self = shift; | ||
| 24 | |||
| 25 | my @from; | ||
| 26 | if ( $self->type_constraint ) { | ||
| 27 | my $moose = $self->type_constraint->{moose_type}; | ||
| 28 | @from = @{ $moose->coercion->type_coercion_map } | ||
| 29 | if $moose && $moose->has_coercion; | ||
| 30 | } | ||
| 31 | else { | ||
| 32 | _croak | ||
| 33 | "The type constraint attached to this coercion has been garbage collected... PANIC"; | ||
| 34 | } | ||
| 35 | |||
| 36 | my @return; | ||
| 37 | while ( @from ) { | ||
| 38 | my ( $type, $code ) = splice( @from, 0, 2 ); | ||
| 39 | $type = Moose::Util::TypeConstraints::find_type_constraint( $type ) | ||
| 40 | unless ref $type; | ||
| 41 | push @return, Types::TypeTiny::to_TypeTiny( $type ), $code; | ||
| 42 | } | ||
| 43 | |||
| 44 | return \@return; | ||
| 45 | } #/ sub type_coercion_map | ||
| 46 | |||
| 47 | sub add_type_coercions { | ||
| 48 | my $self = shift; | ||
| 49 | _croak "Adding coercions to Type::Coercion::FromMoose not currently supported" | ||
| 50 | if @_; | ||
| 51 | } | ||
| 52 | |||
| 53 | sub _build_moose_coercion { | ||
| 54 | my $self = shift; | ||
| 55 | |||
| 56 | if ( $self->type_constraint ) { | ||
| 57 | my $moose = $self->type_constraint->{moose_type}; | ||
| 58 | return $moose->coercion if $moose && $moose->has_coercion; | ||
| 59 | } | ||
| 60 | |||
| 61 | $self->SUPER::_build_moose_coercion( @_ ); | ||
| 62 | } #/ sub _build_moose_coercion | ||
| 63 | |||
| 64 | sub can_be_inlined { | ||
| 65 | 0; | ||
| 66 | } | ||
| 67 | |||
| 68 | 1; | ||
| 69 | |||
| 70 | __END__ |