| 1 | package Moose::Meta::TypeCoercion; | ||||||||||
| 2 | 398 | our $VERSION = '2.4001'; | |||||||||
| 3 | |||||||||||
| 4 | 398 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 398 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 398 | ▶ | use metaclass; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | |||||||||||
| 8 | 398 | ▶ | use Moose::Meta::Attribute; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | 398 | ▶ | use Moose::Util::TypeConstraints (); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | |||||||||||
| 11 | 398 | ▶ | use Moose::Util 'throw_exception'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 12 | |||||||||||
| 13 | __PACKAGE__->meta->add_attribute('type_coercion_map' => ( | ||||||||||
| 14 | reader => 'type_coercion_map', | ||||||||||
| 15 | 5 | ▶ | default => sub { [] }, | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 16 | 398 | Class::MOP::_definition_context(), | |||||||||
| 17 | )); | ||||||||||
| 18 | |||||||||||
| 19 | 398 | __PACKAGE__->meta->add_attribute( | |||||||||
| 20 | Moose::Meta::Attribute->new('type_constraint' => ( | ||||||||||
| 21 | reader => 'type_constraint', | ||||||||||
| 22 | weak_ref => 1, | ||||||||||
| 23 | Class::MOP::_definition_context(), | ||||||||||
| 24 | )) | ||||||||||
| 25 | ); | ||||||||||
| 26 | |||||||||||
| 27 | # private accessor | ||||||||||
| 28 | 398 | __PACKAGE__->meta->add_attribute('compiled_type_coercion' => ( | |||||||||
| 29 | accessor => '_compiled_type_coercion', | ||||||||||
| 30 | Class::MOP::_definition_context(), | ||||||||||
| 31 | )); | ||||||||||
| 32 | |||||||||||
| 33 | 43 | ▶ | sub new { | ||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 34 | 43 | my $class = shift; | |||||||||
| 35 | 43 | my $self = Class::MOP::class_of($class)->new_object(@_); | |||||||||
| 36 | 43 | $self->compile_type_coercion; | |||||||||
| 37 | 41 | return $self; | |||||||||
| 38 | } | ||||||||||
| 39 | |||||||||||
| 40 | 42 | ▶ | sub compile_type_coercion { | ||||||||
|
Subroutine
compile_type_coercion: called CC=4 SCAR=13.9
Pod
undocumented
| |||||||||||
| 41 | 42 | my $self = shift; | |||||||||
| 42 | 42 | my @coercion_map = @{$self->type_coercion_map}; | |||||||||
| 43 | 42 | my @coercions; | |||||||||
| 44 | 42 | ▶ | while (@coercion_map) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 45 | 50 | my ($constraint_name, $action) = splice(@coercion_map, 0, 2); | |||||||||
| 46 | 50 | ▶ | my $type_constraint = ref $constraint_name ? $constraint_name : Moose::Util::TypeConstraints::find_or_parse_type_constraint($constraint_name); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 47 | |||||||||||
| 48 | 50 | ▶ | unless ( defined $type_constraint ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 49 | 1 | throw_exception( CouldNotFindTypeConstraintToCoerceFrom => constraint_name => $constraint_name, | |||||||||
| 50 | instance => $self | ||||||||||
| 51 | ); | ||||||||||
| 52 | } | ||||||||||
| 53 | |||||||||||
| 54 | 49 | push @coercions => [ | |||||||||
| 55 | $type_constraint->_compiled_type_constraint, | ||||||||||
| 56 | $action | ||||||||||
| 57 | ]; | ||||||||||
| 58 | } | ||||||||||
| 59 | 172 | ▶ | $self->_compiled_type_coercion(sub { | ||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||
| 60 | 172 | my $thing = shift; | |||||||||
| 61 | 172 | foreach my $coercion (@coercions) { | |||||||||
| 62 | 186 | my ($constraint, $converter) = @$coercion; | |||||||||
| 63 | 186 | ▶ | if ($constraint->($thing)) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 64 | 159 | local $_ = $thing; | |||||||||
| 65 | 159 | return $converter->($thing); | |||||||||
| 66 | } | ||||||||||
| 67 | } | ||||||||||
| 68 | 13 | return $thing; | |||||||||
| 69 | 41 | }); | |||||||||
| 70 | } | ||||||||||
| 71 | |||||||||||
| 72 | 2 | ▶ | sub has_coercion_for_type { | ||||||||
|
Subroutine
has_coercion_for_type: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||
| 73 | 2 | my ($self, $type_name) = @_; | |||||||||
| 74 | 2 | my %coercion_map = @{$self->type_coercion_map}; | |||||||||
| 75 | 2 | ▶ | exists $coercion_map{$type_name} ? 1 : 0; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 76 | } | ||||||||||
| 77 | |||||||||||
| 78 | 5 | ▶ | sub add_type_coercions { | ||||||||
|
Subroutine
add_type_coercions: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||
| 79 | 5 | my ($self, @new_coercion_map) = @_; | |||||||||
| 80 | |||||||||||
| 81 | 5 | my $coercion_map = $self->type_coercion_map; | |||||||||
| 82 | 5 | my %has_coercion = @$coercion_map; | |||||||||
| 83 | |||||||||||
| 84 | 5 | ▶ | while (@new_coercion_map) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 85 | 5 | my ($constraint_name, $action) = splice(@new_coercion_map, 0, 2); | |||||||||
| 86 | |||||||||||
| 87 | 5 | ▶ | if ( exists $has_coercion{$constraint_name} ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 88 | 1 | throw_exception( CoercionAlreadyExists => constraint_name => $constraint_name, | |||||||||
| 89 | instance => $self | ||||||||||
| 90 | ); | ||||||||||
| 91 | } | ||||||||||
| 92 | |||||||||||
| 93 | 4 | push @{$coercion_map} => ($constraint_name, $action); | |||||||||
| 94 | } | ||||||||||
| 95 | |||||||||||
| 96 | # and re-compile ... | ||||||||||
| 97 | 4 | $self->compile_type_coercion; | |||||||||
| 98 | } | ||||||||||
| 99 | |||||||||||
| 100 | 129 | ▶ | sub coerce { $_[0]->_compiled_type_coercion->($_[1]) } | ||||||||
|
Subroutine
coerce: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 101 | |||||||||||
| 102 | |||||||||||
| 103 | 398 | 1; | |||||||||
| 104 | |||||||||||
| 105 | # ABSTRACT: The Moose Type Coercion metaclass | ||||||||||
| 106 | |||||||||||
| 107 | __END__ | ||||||||||