sub compile_type_coercion {
my $self = shift;
my $type_constraint = $self->type_constraint;
-
+
(blessed $type_constraint && $type_constraint->isa('Moose::Meta::TypeConstraint::Intersection'))
|| Moose->throw_error("You can only a Moose::Meta::TypeCoercion::Intersection for a " .
"Moose::Meta::TypeConstraint::Intersection, not a $type_constraint");
-
+
$self->_compiled_type_coercion(sub {
my $value = shift;
- # go through all the type constraints
+ # go through all the type constraints
# in the intersection, and check em ...
foreach my $type (@{$type_constraint->type_constraints}) {
# if they have a coercion first
- if ($type->has_coercion) {
+ if ($type->has_coercion) {
# then try to coerce them ...
my $temp = $type->coerce($value);
- # and if they get something
+ # and if they get something
# make sure it still fits within
# the intersection type ...
return $temp if $type_constraint->check($temp);
}
}
- return undef;
+ return undef;
});
}
=head1 DESCRIPTION
-For the most part, the only time you will ever encounter an
-instance of this class is if you are doing some serious deep
-introspection. This API should not be considered final, but
-it is B<highly unlikely> that this will matter to a regular
+For the most part, the only time you will ever encounter an
+instance of this class is if you are doing some serious deep
+introspection. This API should not be considered final, but
+it is B<highly unlikely> that this will matter to a regular
Moose user.
-If you wish to use features at this depth, please come to the
+If you wish to use features at this depth, please come to the
#moose IRC channel on irc.perl.org and we can talk :)
=head1 METHODS
=head1 BUGS
-All complex software has bugs lurking in it, and this module is no
+All complex software has bugs lurking in it, and this module is no
exception. If you find a bug please either email me, or add the bug
to cpan-RT.
L<http://www.iinteractive.com>
This library is free software; you can redistribute it and/or modify
-it under the same terms as Perl itself.
+it under the same terms as Perl itself.
=cut
default => sub { [] }
));
-sub new {
+sub new {
my ($class, %options) = @_;
my $self = $class->SUPER::new(
name => (join '&' => sort {$a cmp $b}
$message .= ($message ? ' and ' : '') . $err
if defined $err;
}
- return ($message . ' in (' . $self->name . ')') ;
+ return ($message . ' in (' . $self->name . ')') ;
}
sub is_a_type_of {
foreach my $type (@{$self->type_constraints}) {
return 1 if $type->is_a_type_of($type_name);
}
- return 0;
+ return 0;
}
sub is_subtype_of {
=head1 DESCRIPTION
-This metaclass represents an intersection of Moose type constraints. More
+This metaclass represents an intersection of Moose type constraints. More
details to be explained later (possibly in a Cookbook recipe).
-This actually used to be part of Moose::Meta::TypeConstraint, but it
-is now better off in it's own file.
+This actually used to be part of Moose::Meta::TypeConstraint, but it
+is now better off in it's own file.
=head1 METHODS
-This class is not a subclass of Moose::Meta::TypeConstraint,
+This class is not a subclass of Moose::Meta::TypeConstraint,
but it does provide the same API
=over 4
=back
-=head2 Overridden methods
+=head2 Overridden methods
=over 4
=head2 Empty or Stub methods
-These methods tend to not be very relevant in
-the context of an intersection. Either that or they are
-just difficult to specify and not very useful
+These methods tend to not be very relevant in
+the context of an intersection. Either that or they are
+just difficult to specify and not very useful
anyway. They are here for completeness.
=over 4
=head1 BUGS
-All complex software has bugs lurking in it, and this module is no
+All complex software has bugs lurking in it, and this module is no
exception. If you find a bug please either email me, or add the bug
to cpan-RT.
else {
@type_constraint_names = @_;
}
-
+
(scalar @type_constraint_names >= 2)
|| __PACKAGE__->_throw_error("You must pass in at least 2 type names to make an intersection");
use Test::Exception;
BEGIN {
- use_ok('Moose::Util::TypeConstraints');
+ use_ok('Moose::Util::TypeConstraints');
}
my $Str = find_type_constraint('Str');
ok(!defined($RefAndArray->validate([])), '... (ArrayRef & Ref) can accept []');
ok(defined($RefAndArray->validate(undef)), '... (ArrayRef & Ref) cannot accept undef');
-like($RefAndArray->validate(undef),
+like($RefAndArray->validate(undef),
qr/Validation failed for \'ArrayRef\' with value undef and Validation failed for \'Ref\' with value undef in \(ArrayRef&Ref\)/,
'... (ArrayRef & Ref) cannot accept undef');
}
lives_ok {
- subtype 'MyCollectionsExtended'
+ subtype 'MyCollectionsExtended'
=> as 'ArrayRef&Ref'
=> where {
if (ref($_) eq 'ARRAY') {
is($p->name, 'ArrayRef&Ref', '... parent name is correct');
ok(!$t->check([]), '... validated it correctly');
- ok($t->check([1, 2]), '... validated it correctly');
-
- ok($t->check([ one => 1, two => 2 ]), '... validated it correctly');
-
+ ok($t->check([1, 2]), '... validated it correctly');
+
+ ok($t->check([ one => 1, two => 2 ]), '... validated it correctly');
+
ok(!$t->check(1), '... validated it correctly');
}
## now for the intersections
-ok(Moose::Util::TypeConstraints::_detect_type_constraint_intersection($_),
+ok(Moose::Util::TypeConstraints::_detect_type_constraint_intersection($_),
'... this correctly detected intersection (' . $_ . ')')
for (
'Int & Str',
- 'Int&Str',
+ 'Int&Str',
'ArrayRef[Foo] & Int',
- 'ArrayRef[Foo]&Int',
+ 'ArrayRef[Foo]&Int',
'Int & ArrayRef[Foo]',
- 'Int&ArrayRef[Foo]',
+ 'Int&ArrayRef[Foo]',
'ArrayRef[Foo | Int] & Str',
- 'ArrayRef[Foo|Int]&Str',
- 'Str & ArrayRef[Foo | Int]',
- 'Str&ArrayRef[Foo|Int]',
- 'Some&Silly&Name&With&Pipes & Int',
- 'Some&Silly&Name&With&Pipes&Int',
+ 'ArrayRef[Foo|Int]&Str',
+ 'Str & ArrayRef[Foo | Int]',
+ 'Str&ArrayRef[Foo|Int]',
+ 'Some&Silly&Name&With&Pipes & Int',
+ 'Some&Silly&Name&With&Pipes&Int',
);
-ok(!Moose::Util::TypeConstraints::_detect_type_constraint_intersection($_),
+ok(!Moose::Util::TypeConstraints::_detect_type_constraint_intersection($_),
'... this correctly detected a non-intersection (' . $_ . ')')
for (
'Int',
'ArrayRef[Foo | Int]',
- 'ArrayRef[Foo|Int]',
+ 'ArrayRef[Foo|Int]',
);
{
my %split_tests = (
'Int & Str' => [ 'Int', 'Str' ],
- 'Int&Str' => [ 'Int', 'Str' ],
+ 'Int&Str' => [ 'Int', 'Str' ],
'ArrayRef[Foo] & Int' => [ 'ArrayRef[Foo]', 'Int' ],
- 'ArrayRef[Foo]&Int' => [ 'ArrayRef[Foo]', 'Int' ],
+ 'ArrayRef[Foo]&Int' => [ 'ArrayRef[Foo]', 'Int' ],
'Int & ArrayRef[Foo]' => [ 'Int', 'ArrayRef[Foo]' ],
- 'Int&ArrayRef[Foo]' => [ 'Int', 'ArrayRef[Foo]' ],
+ 'Int&ArrayRef[Foo]' => [ 'Int', 'ArrayRef[Foo]' ],
'ArrayRef[Foo | Int] & Str' => [ 'ArrayRef[Foo | Int]', 'Str' ],
- 'ArrayRef[Foo|Int]&Str' => [ 'ArrayRef[Foo|Int]', 'Str' ],
- 'Str & ArrayRef[Foo | Int]' => [ 'Str', 'ArrayRef[Foo | Int]' ],
- 'Str&ArrayRef[Foo|Int]' => [ 'Str', 'ArrayRef[Foo|Int]' ],
- 'Some&Silly&Name&With&Pipes & Int' => [ 'Some', 'Silly', 'Name', 'With', 'Pipes', 'Int' ],
- 'Some&Silly&Name&With&Pipes&Int' => [ 'Some', 'Silly', 'Name', 'With', 'Pipes', 'Int' ],
+ 'ArrayRef[Foo|Int]&Str' => [ 'ArrayRef[Foo|Int]', 'Str' ],
+ 'Str & ArrayRef[Foo | Int]' => [ 'Str', 'ArrayRef[Foo | Int]' ],
+ 'Str&ArrayRef[Foo|Int]' => [ 'Str', 'ArrayRef[Foo|Int]' ],
+ 'Some&Silly&Name&With&Pipes & Int' => [ 'Some', 'Silly', 'Name', 'With', 'Pipes', 'Int' ],
+ 'Some&Silly&Name&With&Pipes&Int' => [ 'Some', 'Silly', 'Name', 'With', 'Pipes', 'Int' ],
);
is_deeply(