Moose::Meta::TypeConstraint::Union and is itself
a subclass of Moose::Meta::TypeCoercion
- * Moose::Meta::TypeConstraint::Container
+ * Moose::Meta::TypeConstraint::Parameterized
- added this module (taken from MooseX::AttributeHelpers)
to help construct nested collection types
- added tests for this
'Moose::Meta::TypeConstraint',
'Moose::Meta::TypeConstraint::Union',
- 'Moose::Meta::TypeConstraint::Container',
+ 'Moose::Meta::TypeConstraint::Parameterized',
'Moose::Meta::TypeCoercion',
'Moose::Meta::Method',
-package Moose::Meta::TypeConstraint::Container;
+package Moose::Meta::TypeConstraint::Parameterized;
use strict;
use warnings;
use base 'Moose::Meta::TypeConstraint';
-__PACKAGE__->meta->add_attribute('container_type' => (
- accessor => 'container_type',
- predicate => 'has_container_type',
+__PACKAGE__->meta->add_attribute('type_parameter' => (
+ accessor => 'type_parameter',
+ predicate => 'has_type_parameter',
));
sub compile_type_constraint {
my $self = shift;
- ($self->has_container_type)
- || confess "You cannot create a Container type without one";
+ ($self->has_type_parameter)
+ || confess "You cannot create a Higher Order type without a type parameter";
- my $container_type = $self->container_type;
+ my $type_parameter = $self->type_parameter;
- (blessed $container_type && $container_type->isa('Moose::Meta::TypeConstraint'))
- || confess "The container type must be a Moose meta type";
+ (blessed $type_parameter && $type_parameter->isa('Moose::Meta::TypeConstraint'))
+ || confess "The type parameter must be a Moose meta type";
my $constraint;
if ($parent_name eq 'ArrayRef') {
$constraint = sub {
foreach my $x (@$_) {
- ($container_type->check($x)) || return
+ ($type_parameter->check($x)) || return
} 1;
};
}
elsif ($parent_name eq 'HashRef') {
$constraint = sub {
foreach my $x (values %$_) {
- ($container_type->check($x)) || return
+ ($type_parameter->check($x)) || return
} 1;
};
}
=head1 NAME
-Moose::Meta::TypeConstraint::Container
+Moose::Meta::TypeConstraint::Parameterized - Higher Order type constraints for Moose
=head1 DESCRIPTION
=item B<compile_type_constraint>
-=item B<container_type>
+=item B<type_parameter>
-=item B<has_container_type>
+=item B<has_type_parameter>
=item B<meta>
# compiled.
# creation and location
-sub find_type_constraint ($);
-sub find_or_create_type_constraint ($;$);
-sub create_type_constraint_union (@);
-sub create_container_type_constraint ($);
+sub find_type_constraint ($);
+sub find_or_create_type_constraint ($;$);
+sub create_type_constraint_union (@);
+sub create_parameterized_type_constraint ($);
# dah sugah!
sub type ($$;$$);
use Moose::Meta::TypeConstraint;
use Moose::Meta::TypeConstraint::Union;
-use Moose::Meta::TypeConstraint::Container;
+use Moose::Meta::TypeConstraint::Parameterized;
use Moose::Meta::TypeCoercion;
use Moose::Meta::TypeCoercion::Union;
use Moose::Meta::TypeConstraint::Registry;
);
}
-sub create_container_type_constraint ($) {
+sub create_parameterized_type_constraint ($) {
my $type_constraint_name = shift;
- my ($base_type, $container_type) = _parse_container_type_constraint($type_constraint_name);
+ my ($base_type, $type_parameter) = _parse_parameterized_type_constraint($type_constraint_name);
- (defined $base_type && defined $container_type)
+ (defined $base_type && defined $type_parameter)
|| confess "Could not parse type name ($type_constraint_name) correctly";
($REGISTRY->has_type_constraint($base_type))
|| confess "Could not locate the base type ($base_type)";
- return Moose::Meta::TypeConstraint::Container->new(
+ return Moose::Meta::TypeConstraint::Parameterized->new(
name => $type_constraint_name,
parent => $REGISTRY->get_type_constraint($base_type),
- container_type => find_or_create_type_constraint(
- $container_type => {
+ type_parameter => find_or_create_type_constraint(
+ $type_parameter => {
parent => $REGISTRY->get_type_constraint('Object'),
- constraint => sub { $_[0]->isa($container_type) }
+ constraint => sub { $_[0]->isa($type_parameter) }
}
),
);
if (_detect_type_constraint_union($type_constraint_name)) {
$constraint = create_type_constraint_union($type_constraint_name);
}
- elsif (_detect_container_type_constraint($type_constraint_name)) {
- $constraint = create_container_type_constraint($type_constraint_name);
+ elsif (_detect_parameterized_type_constraint($type_constraint_name)) {
+ $constraint = create_parameterized_type_constraint($type_constraint_name);
}
else {
# NOTE:
our $any = qr{ $type | $union }x;
- sub _parse_container_type_constraint {
+ sub _parse_parameterized_type_constraint {
$_[0] =~ m{ $type_capture_parts }x;
return ($1, $2);
}
- sub _detect_container_type_constraint {
+ sub _detect_parameterized_type_constraint {
$_[0] =~ m{ ^ $type_with_parameter $ }x;
}
Given string with C<$pipe_seperated_types> or a list of C<@type_constraint_names>,
this will return a L<Moose::Meta::TypeConstraint::Union> instance.
-=item B<create_container_type_constraint ($type_name)>
+=item B<create_parameterized_type_constraint ($type_name)>
Given a C<$type_name> in the form of:
BaseType[ContainerType]
this will extract the base type and container type and build an instance of
-L<Moose::Meta::TypeConstraint::Container> for it.
+L<Moose::Meta::TypeConstraint::Parameterized> for it.
=item B<find_or_create_type_constraint ($type_name, ?$options_for_anon_type)>
BEGIN {
use_ok('Moose::Util::TypeConstraints');
- use_ok('Moose::Meta::TypeConstraint::Container');
+ use_ok('Moose::Meta::TypeConstraint::Parameterized');
}
# Array of Ints
-my $array_of_ints = Moose::Meta::TypeConstraint::Container->new(
+my $array_of_ints = Moose::Meta::TypeConstraint::Parameterized->new(
name => 'ArrayRef[Int]',
parent => find_type_constraint('ArrayRef'),
- container_type => find_type_constraint('Int'),
+ type_parameter => find_type_constraint('Int'),
);
-isa_ok($array_of_ints, 'Moose::Meta::TypeConstraint::Container');
+isa_ok($array_of_ints, 'Moose::Meta::TypeConstraint::Parameterized');
isa_ok($array_of_ints, 'Moose::Meta::TypeConstraint');
ok($array_of_ints->check([ 1, 2, 3, 4 ]), '... [ 1, 2, 3, 4 ] passed successfully');
# Hash of Ints
-my $hash_of_ints = Moose::Meta::TypeConstraint::Container->new(
+my $hash_of_ints = Moose::Meta::TypeConstraint::Parameterized->new(
name => 'HashRef[Int]',
parent => find_type_constraint('HashRef'),
- container_type => find_type_constraint('Int'),
+ type_parameter => find_type_constraint('Int'),
);
-isa_ok($hash_of_ints, 'Moose::Meta::TypeConstraint::Container');
+isa_ok($hash_of_ints, 'Moose::Meta::TypeConstraint::Parameterized');
isa_ok($hash_of_ints, 'Moose::Meta::TypeConstraint');
ok($hash_of_ints->check({ one => 1, two => 2, three => 3 }), '... { one => 1, two => 2, three => 3 } passed successfully');
# Array of Array of Ints
-my $array_of_array_of_ints = Moose::Meta::TypeConstraint::Container->new(
+my $array_of_array_of_ints = Moose::Meta::TypeConstraint::Parameterized->new(
name => 'ArrayRef[ArrayRef[Int]]',
parent => find_type_constraint('ArrayRef'),
- container_type => $array_of_ints,
+ type_parameter => $array_of_ints,
);
-isa_ok($array_of_array_of_ints, 'Moose::Meta::TypeConstraint::Container');
+isa_ok($array_of_array_of_ints, 'Moose::Meta::TypeConstraint::Parameterized');
isa_ok($array_of_array_of_ints, 'Moose::Meta::TypeConstraint');
ok($array_of_array_of_ints->check(
BEGIN {
use_ok('Moose::Util::TypeConstraints');
- use_ok('Moose::Meta::TypeConstraint::Container');
+ use_ok('Moose::Meta::TypeConstraint::Parameterized');
}
my $r = Moose::Util::TypeConstraints->get_type_constraint_registry;
# Array of Ints
-my $array_of_ints = Moose::Meta::TypeConstraint::Container->new(
+my $array_of_ints = Moose::Meta::TypeConstraint::Parameterized->new(
name => 'ArrayRef[Int]',
parent => find_type_constraint('ArrayRef'),
- container_type => find_type_constraint('Int'),
+ type_parameter => find_type_constraint('Int'),
);
-isa_ok($array_of_ints, 'Moose::Meta::TypeConstraint::Container');
+isa_ok($array_of_ints, 'Moose::Meta::TypeConstraint::Parameterized');
isa_ok($array_of_ints, 'Moose::Meta::TypeConstraint');
$r->add_type_constraint($array_of_ints);
# Hash of Ints
-my $hash_of_ints = Moose::Meta::TypeConstraint::Container->new(
+my $hash_of_ints = Moose::Meta::TypeConstraint::Parameterized->new(
name => 'HashRef[Int]',
parent => find_type_constraint('HashRef'),
- container_type => find_type_constraint('Int'),
+ type_parameter => find_type_constraint('Int'),
);
-isa_ok($hash_of_ints, 'Moose::Meta::TypeConstraint::Container');
+isa_ok($hash_of_ints, 'Moose::Meta::TypeConstraint::Parameterized');
isa_ok($hash_of_ints, 'Moose::Meta::TypeConstraint');
$r->add_type_constraint($hash_of_ints);
BEGIN {
use_ok('Moose::Util::TypeConstraints');
- use_ok('Moose::Meta::TypeConstraint::Container');
+ use_ok('Moose::Meta::TypeConstraint::Parameterized');
}
my $r = Moose::Util::TypeConstraints->get_type_constraint_registry;
# Array of Ints or Strings
-my $array_of_ints_or_strings = Moose::Util::TypeConstraints::create_container_type_constraint('ArrayRef[Int | Str]');
-isa_ok($array_of_ints_or_strings, 'Moose::Meta::TypeConstraint::Container');
+my $array_of_ints_or_strings = Moose::Util::TypeConstraints::create_parameterized_type_constraint('ArrayRef[Int | Str]');
+isa_ok($array_of_ints_or_strings, 'Moose::Meta::TypeConstraint::Parameterized');
ok($array_of_ints_or_strings->check([ 1, 'two', 3 ]), '... this passed the type check');
ok($array_of_ints_or_strings->check([ 1, 2, 3 ]), '... this passed the type check');
# Array of Ints or HashRef
-my $array_of_ints_or_hash_ref = Moose::Util::TypeConstraints::create_container_type_constraint('ArrayRef[Int | HashRef]');
-isa_ok($array_of_ints_or_hash_ref, 'Moose::Meta::TypeConstraint::Container');
+my $array_of_ints_or_hash_ref = Moose::Util::TypeConstraints::create_parameterized_type_constraint('ArrayRef[Int | HashRef]');
+isa_ok($array_of_ints_or_hash_ref, 'Moose::Meta::TypeConstraint::Parameterized');
ok($array_of_ints_or_hash_ref->check([ 1, {}, 3 ]), '... this passed the type check');
ok($array_of_ints_or_hash_ref->check([ 1, 2, 3 ]), '... this passed the type check');
# Array of Ints
-my $array_of_ints = Moose::Util::TypeConstraints::create_container_type_constraint('ArrayRef[Int]');
-isa_ok($array_of_ints, 'Moose::Meta::TypeConstraint::Container');
+my $array_of_ints = Moose::Util::TypeConstraints::create_parameterized_type_constraint('ArrayRef[Int]');
+isa_ok($array_of_ints, 'Moose::Meta::TypeConstraint::Parameterized');
isa_ok($array_of_ints, 'Moose::Meta::TypeConstraint');
ok($array_of_ints->check([ 1, 2, 3, 4 ]), '... [ 1, 2, 3, 4 ] passed successfully');
# Array of Array of Ints
-my $array_of_array_of_ints = Moose::Util::TypeConstraints::create_container_type_constraint('ArrayRef[ArrayRef[Int]]');
-isa_ok($array_of_array_of_ints, 'Moose::Meta::TypeConstraint::Container');
+my $array_of_array_of_ints = Moose::Util::TypeConstraints::create_parameterized_type_constraint('ArrayRef[ArrayRef[Int]]');
+isa_ok($array_of_array_of_ints, 'Moose::Meta::TypeConstraint::Parameterized');
isa_ok($array_of_array_of_ints, 'Moose::Meta::TypeConstraint');
ok($array_of_array_of_ints->check(
# Array of Array of Array of Ints
-my $array_of_array_of_array_of_ints = Moose::Util::TypeConstraints::create_container_type_constraint('ArrayRef[ArrayRef[ArrayRef[Int]]]');
-isa_ok($array_of_array_of_array_of_ints, 'Moose::Meta::TypeConstraint::Container');
+my $array_of_array_of_array_of_ints = Moose::Util::TypeConstraints::create_parameterized_type_constraint('ArrayRef[ArrayRef[ArrayRef[Int]]]');
+isa_ok($array_of_array_of_array_of_ints, 'Moose::Meta::TypeConstraint::Parameterized');
isa_ok($array_of_array_of_array_of_ints, 'Moose::Meta::TypeConstraint');
ok($array_of_array_of_array_of_ints->check(
## check the containers
-ok(Moose::Util::TypeConstraints::_detect_container_type_constraint($_),
+ok(Moose::Util::TypeConstraints::_detect_parameterized_type_constraint($_),
'... this correctly detected a container (' . $_ . ')')
for (
'ArrayRef[Foo]',
'ArrayRef[ArrayRef[Int | Foo]]',
);
-ok(!Moose::Util::TypeConstraints::_detect_container_type_constraint($_),
+ok(!Moose::Util::TypeConstraints::_detect_parameterized_type_constraint($_),
'... this correctly detected a non-container (' . $_ . ')')
for (
'ArrayRef[]',
);
is_deeply(
- [ Moose::Util::TypeConstraints::_parse_container_type_constraint($_) ],
+ [ Moose::Util::TypeConstraints::_parse_parameterized_type_constraint($_) ],
$split_tests{$_},
'... this correctly split the container (' . $_ . ')'
) for keys %split_tests;