« blib/lib/Moose/Meta/TypeConstraint/Parameterized.pm
↑ Summary
blib/lib/Moose/Meta/Method/Constructor.pm »
| 1 | package Moose::Meta::TypeConstraint::Parameterizable; | ||||||||||||||||||||||||||||
| 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 parent 'Moose::Meta::TypeConstraint'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 398 | ▶ | use Moose::Meta::TypeConstraint::Parameterized; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 398 | ▶ | use Moose::Util::TypeConstraints (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 398 | ▶ | use Moose::Util 'throw_exception'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | 398 | ▶ | use Carp 'confess'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | 398 | __PACKAGE__->meta->add_attribute('constraint_generator' => ( | |||||||||||||||||||||||||||
| 17 | accessor => 'constraint_generator', | ||||||||||||||||||||||||||||
| 18 | predicate => 'has_constraint_generator', | ||||||||||||||||||||||||||||
| 19 | Class::MOP::_definition_context(), | ||||||||||||||||||||||||||||
| 20 | )); | ||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | 398 | __PACKAGE__->meta->add_attribute('inline_generator' => ( | |||||||||||||||||||||||||||
| 23 | accessor => 'inline_generator', | ||||||||||||||||||||||||||||
| 24 | predicate => 'has_inline_generator', | ||||||||||||||||||||||||||||
| 25 | Class::MOP::_definition_context(), | ||||||||||||||||||||||||||||
| 26 | )); | ||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | 341 | ▶ | sub generate_constraint_for { | ||||||||||||||||||||||||||
|
Subroutine
generate_constraint_for: called CC=5 SCAR=16.2
Pod
undocumented
| |||||||||||||||||||||||||||||
| 29 | 341 | my ($self, $type) = @_; | |||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | 341 | ▶ | return unless $self->has_constraint_generator; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | 341 | ▶ | return $self->constraint_generator->($type->type_parameter) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 34 | if $type->is_subtype_of($self->name); | ||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | 201 | ▶ | return $self->_can_coerce_constraint_from($type) | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$self->has_coercion and $self->coercion->has_coercion_for_type($type->parent->name)
MC/DC50%1/2
$self->has_coercion and $self->coercion->has_coercion_for_type($type->parent->name)
$self->has_coercion
$self->coercion->has_coercion_for_type($type->parent->name)
Truth table66%2/3
A and B
A= $self->has_coercionB= $self->coercion->has_coercion_for_type($type->parent->name)
| |||||||||||||||||||||||||||||
| 37 | if $self->has_coercion | ||||||||||||||||||||||||||||
| 38 | && $self->coercion->has_coercion_for_type($type->parent->name); | ||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | 199 | return; | |||||||||||||||||||||||||||
| 41 | } | ||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | 2 | ▶ | sub _can_coerce_constraint_from { | ||||||||||||||||||||||||||
|
Subroutine
_can_coerce_constraint_from: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 44 | 2 | my ($self, $type) = @_; | |||||||||||||||||||||||||||
| 45 | 2 | my $coercion = $self->coercion; | |||||||||||||||||||||||||||
| 46 | 2 | my $constraint = $self->constraint_generator->($type->type_parameter); | |||||||||||||||||||||||||||
| 47 | 4 | ▶ | return sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 48 | 4 | local $_ = $coercion->coerce($_); | |||||||||||||||||||||||||||
| 49 | 4 | $constraint->(@_); | |||||||||||||||||||||||||||
| 50 | 2 | }; | |||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | 337 | ▶ | sub generate_inline_for { | ||||||||||||||||||||||||||
|
Subroutine
generate_inline_for: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 54 | 337 | my ($self, $type, $val) = @_; | |||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | 337 | ▶ | throw_exception( CannotGenerateInlineConstraint => parameterizable_type_object_name => $self->name, | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 57 | type_name => $type->name, | ||||||||||||||||||||||||||||
| 58 | value => $val, | ||||||||||||||||||||||||||||
| 59 | ) | ||||||||||||||||||||||||||||
| 60 | unless $self->has_inline_generator; | ||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | 336 | return '( do { ' . $self->inline_generator->( $self, $type, $val ) . ' } )'; | |||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | 133 | ▶ | sub _parse_type_parameter { | ||||||||||||||||||||||||||
|
Subroutine
_parse_type_parameter: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 66 | 133 | my ($self, $type_parameter) = @_; | |||||||||||||||||||||||||||
| 67 | 133 | return Moose::Util::TypeConstraints::find_or_create_isa_type_constraint($type_parameter); | |||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | 133 | ▶ | sub parameterize { | ||||||||||||||||||||||||||
|
Subroutine
parameterize: called CC=5 SCAR=16.3
Pod
undocumented
| |||||||||||||||||||||||||||||
| 71 | 133 | my ($self, $type_parameter) = @_; | |||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | 133 | my $contained_tc = $self->_parse_type_parameter($type_parameter); | |||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | ## The type parameter should be a subtype of the parent's type parameter | ||||||||||||||||||||||||||||
| 76 | ## if there is one. | ||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | 133 | ▶ | if(my $parent = $self->parent) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 79 | 133 | ▶ | if($parent->can('type_parameter')) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 80 | 3 | ▶ | unless ( $contained_tc->is_a_type_of($parent->type_parameter) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 81 | 2 | throw_exception( ParameterIsNotSubtypeOfParent => type_parameter => $type_parameter, | |||||||||||||||||||||||||||
| 82 | type_name => $self->name, | ||||||||||||||||||||||||||||
| 83 | ); | ||||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | 131 | ▶ | if ( $contained_tc->isa('Moose::Meta::TypeConstraint') ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 89 | 131 | my $tc_name = $self->name . '[' . $contained_tc->name . ']'; | |||||||||||||||||||||||||||
| 90 | 131 | return Moose::Meta::TypeConstraint::Parameterized->new( | |||||||||||||||||||||||||||
| 91 | name => $tc_name, | ||||||||||||||||||||||||||||
| 92 | parent => $self, | ||||||||||||||||||||||||||||
| 93 | type_parameter => $contained_tc, | ||||||||||||||||||||||||||||
| 94 | parameterized_from => $self, | ||||||||||||||||||||||||||||
| 95 | ); | ||||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||
| 97 | else { | ||||||||||||||||||||||||||||
| 98 | 0 | confess("The type parameter must be a Moose meta type"); | |||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | 398 | 1; | |||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | # ABSTRACT: Type constraints which can take a parameter (ArrayRef) | ||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | __END__ | ||||||||||||||||||||||||||||