| 1 | package MooseX::Meta::TypeConstraint::Intersection; | ||||||||||
| 2 | { | ||||||||||
| 3 | 2 | $MooseX::Meta::TypeConstraint::Intersection::VERSION = '0.04'; | |||||||||
| 4 | } | ||||||||||
| 5 | # ABSTRACT: An intersection of Moose type constraints | ||||||||||
| 6 | |||||||||||
| 7 | 2 | ▶ | use Moose; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | 2 | ▶ | use MooseX::Types::Moose qw/ArrayRef/; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | 2 | ▶ | use Moose::Util::TypeConstraints 'find_type_constraint'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | 2 | ▶ | use aliased 'Moose::Meta::TypeConstraint'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 11 | 2 | ▶ | use namespace::autoclean -also => 'TypeConstraint'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 12 | |||||||||||
| 13 | |||||||||||
| 14 | 2 | extends TypeConstraint; | |||||||||
| 15 | |||||||||||
| 16 | |||||||||||
| 17 | has type_constraints => ( | ||||||||||
| 18 | is => 'ro', | ||||||||||
| 19 | isa => ArrayRef[TypeConstraint], | ||||||||||
| 20 | 0 | ▶ | default => sub { [] }, | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 21 | 2 | ); | |||||||||
| 22 | |||||||||||
| 23 | |||||||||||
| 24 | 5 | ▶ | around new => sub { | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 25 | 5 | my ($next, $class, %args) = @_; | |||||||||
| 26 | 13 | my $name = join '&' => sort { $a cmp $b } | |||||||||
| 27 | 5 | map { $_->name } @{ $args{type_constraints} }; | |||||||||
| 28 | 5 | return $class->$next(name => $name, %args); | |||||||||
| 29 | 2 | }; | |||||||||
| 30 | |||||||||||
| 31 | |||||||||||
| 32 | 5 | ▶ | sub _actually_compile_type_constraint { | ||||||||
|
Subroutine
_actually_compile_type_constraint: called CC=1 SCAR=0.0
| |||||||||||
| 33 | 5 | my ($self) = @_; | |||||||||
| 34 | 5 | my @type_constraints = @{ $self->type_constraints }; | |||||||||
| 35 | 6 | ▶ | return sub { | ||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||
| 36 | 6 | my ($value) = @_; | |||||||||
| 37 | |||||||||||
| 38 | 6 | for my $type (@type_constraints) { | |||||||||
| 39 | 11 | ▶ | return unless $type->check($value); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 40 | } | ||||||||||
| 41 | |||||||||||
| 42 | 2 | return 1; | |||||||||
| 43 | 5 | }; | |||||||||
| 44 | } | ||||||||||
| 45 | |||||||||||
| 46 | |||||||||||
| 47 | # this is stolen from TC::Union. meh | ||||||||||
| 48 | 3 | ▶ | sub equals { | ||||||||
|
Subroutine
equals: called CC=6 SCAR=18.1
Pod
documented
| |||||||||||
| 49 | 3 | my ($self, $type_or_name) = @_; | |||||||||
| 50 | 3 | my $other = find_type_constraint($type_or_name); | |||||||||
| 51 | |||||||||||
| 52 | 3 | ▶ | return unless $other->isa(__PACKAGE__); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 53 | |||||||||||
| 54 | 3 | my @self_constraints = @{ $self->type_constraints }; | |||||||||
| 55 | 3 | my @other_constraints = @{ $other->type_constraints }; | |||||||||
| 56 | |||||||||||
| 57 | 3 | ▶ | return unless @self_constraints == @other_constraints; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 58 | |||||||||||
| 59 | 3 | CONSTRAINT: for my $constraint (@self_constraints) { | |||||||||
| 60 | 9 | ▶ | for (my $i = 0; $i < @other_constraints; $i++) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 61 | 12 | ▶ | if ($constraint->equals($other_constraints[$i])) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 62 | 9 | splice @other_constraints, $i, 1; | |||||||||
| 63 | 9 | next CONSTRAINT; | |||||||||
| 64 | } | ||||||||||
| 65 | } | ||||||||||
| 66 | } | ||||||||||
| 67 | |||||||||||
| 68 | 3 | return @other_constraints == 0; | |||||||||
| 69 | } | ||||||||||
| 70 | |||||||||||
| 71 | |||||||||||
| 72 | # this too, although i'm not too sure what the point of it is | ||||||||||
| 73 | 0 | ▶ | sub parents { | ||||||||
|
Subroutine
parents: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 74 | 0 | my ($self) = @_; | |||||||||
| 75 | 0 | return $self->type_constraints; | |||||||||
| 76 | } | ||||||||||
| 77 | |||||||||||
| 78 | |||||||||||
| 79 | 6 | ▶ | sub validate { | ||||||||
|
Subroutine
validate: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 80 | 6 | my ($self, $value) = @_; | |||||||||
| 81 | 6 | my $msgs = $self->validate_all($value); | |||||||||
| 82 | 6 | ▶ | return undef unless defined $msgs; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 83 | 4 | return join(q{ and } => map { $_->[0] } @{ $msgs }) . ' in ' . $self->name; | |||||||||
| 84 | } | ||||||||||
| 85 | |||||||||||
| 86 | 2 | ▶ | sub get_message { | ||||||||
|
Subroutine
get_message: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 87 | 2 | my ($self, $value) = @_; | |||||||||
| 88 | 2 | return $self->validate($value); | |||||||||
| 89 | } | ||||||||||
| 90 | |||||||||||
| 91 | |||||||||||
| 92 | 9 | ▶ | sub validate_all { | ||||||||
|
Subroutine
validate_all: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||
| 93 | 9 | my ($self, $value) = @_; | |||||||||
| 94 | |||||||||||
| 95 | my @msgs = map { | ||||||||||
| 96 | 25 | my $err = $_->validate($value); | |||||||||
| 97 | 25 | ▶ | defined $err ? [ $err, $_ ] : (); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 98 | 9 | } @{ $self->type_constraints }; | |||||||||
| 99 | |||||||||||
| 100 | 9 | ▶ | return @msgs ? \@msgs : undef; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 101 | } | ||||||||||
| 102 | |||||||||||
| 103 | |||||||||||
| 104 | 0 | ▶ | sub is_subtype_of { | ||||||||
|
Subroutine
is_subtype_of: not called CC=6 SCAR=37.4
Pod
documented
| |||||||||||
| 105 | 0 | my ($self, $type_or_name) = @_; | |||||||||
| 106 | 0 | my $other = find_type_constraint($type_or_name); | |||||||||
| 107 | |||||||||||
| 108 | 0 | ▶ | return unless $other->isa(__PACKAGE__); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 109 | |||||||||||
| 110 | 0 | my @self_constraints = @{ $self->type_constraints }; | |||||||||
| 111 | 0 | my @other_constraints = @{ $other->type_constraints }; | |||||||||
| 112 | |||||||||||
| 113 | 0 | ▶ | return if @self_constraints < @other_constraints; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 114 | |||||||||||
| 115 | 0 | CONSTRAINT: for my $tc (@other_constraints) { | |||||||||
| 116 | 0 | ▶ | for (my $i = 0; $i < @self_constraints; $i++) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 117 | 0 | ▶ | if ($tc->is_subtype_of($self_constraints[$i])) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 118 | 0 | splice @self_constraints, $i, 1; | |||||||||
| 119 | 0 | next CONSTRAINT; | |||||||||
| 120 | } | ||||||||||
| 121 | } | ||||||||||
| 122 | } | ||||||||||
| 123 | |||||||||||
| 124 | 0 | return @self_constraints == 0; | |||||||||
| 125 | } | ||||||||||
| 126 | |||||||||||
| 127 | |||||||||||
| 128 | 2 | 1; | |||||||||
| 129 | |||||||||||
| 130 | __END__ | ||||||||||