2 package MooseX::AttributeHelpers::Collection;
4 use Moose::Util::TypeConstraints;
7 our $AUTHORITY = 'cpan:STEVAN';
9 extends 'MooseX::AttributeHelpers::Base';
11 has 'container_type' => (
14 predicate => 'has_container_type',
17 has 'container_type_constraint' => (
19 isa => 'Moose::Meta::TypeConstraint',
23 before 'process_options_for_provides' => sub {
24 my ($self, $options) = @_;
26 if (exists $options->{isa}) {
27 my $type = $options->{isa};
29 # ... we should check if the type exists already
30 # and then we should use it,.. however, this means
31 # we need to extract the container type constraint
32 # as well, which is a little trickier
34 if ($type =~ /^(.*)\[(.*)\]$/) {
36 my $container_type = $2;
38 $options->{container_type} = $container_type;
40 my $container_type_constraint = find_type_constraint($container_type);
43 # I am not sure DWIM-ery is a good thing
44 # here, so i am going to err on the side
45 # of caution, and blow up if you have
46 # not made a type constraint for this yet.
48 (defined $container_type_constraint)
49 || confess "You must predefine the '$container_type' constraint before you can use it as a container type";
51 $options->{container_type_constraint} = $container_type_constraint;
53 if ($core_type eq 'ArrayRef') {
54 $options->{isa} = subtype('ArrayRef' => where {
55 foreach my $x (@$_) { ($container_type_constraint->check($x)) || return } 1;
58 elsif ($core_type eq 'HashRef') {
59 $options->{isa} = subtype('HashRef' => where {
60 foreach my $x (values %$_) { ($container_type_constraint->check($x)) || return } 1;
64 confess "Your isa must be either ArrayRef or HashRef (sorry no subtype support yet)";
71 no Moose::Util::TypeConstraints;
89 All complex software has bugs lurking in it, and this module is no
90 exception. If you find a bug please either email me, or add the bug
95 Stevan Little E<lt>stevan@iinteractive.comE<gt>
97 =head1 COPYRIGHT AND LICENSE
99 Copyright 2007 by Infinity Interactive, Inc.
101 L<http://www.iinteractive.com>
103 This library is free software; you can redistribute it and/or modify
104 it under the same terms as Perl itself.