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',
22 before 'process_options_for_provides' => sub {
23 my ($self, $options) = @_;
25 if (exists $options->{isa}) {
26 my $type = $options->{isa};
28 # ... we should check if the type exists already
29 # and then we should use it,.. however, this means
30 # we need to extract the container type constraint
31 # as well, which is a little trickier
33 if ($type =~ /^(.*)\[(.*)\]$/) {
35 my $container_type = $2;
37 $options->{container_type} = $container_type;
39 my $container_type_constraint = find_type_constraint($container_type);
42 # I am not sure DWIM-ery is a good thing
43 # here, so i am going to err on the side
44 # of caution, and blow up if you have
45 # not made a type constraint for this yet.
47 (defined $container_type_constraint)
48 || confess "You must predefine the '$container_type' constraint before you can use it as a container type";
50 $options->{container_type_constraint} = $container_type_constraint;
52 if ($core_type eq 'ArrayRef') {
53 $options->{isa} = subtype('ArrayRef' => where {
54 foreach my $x (@$_) { ($container_type_constraint->check($x)) || return } 1;
57 elsif ($core_type eq 'HashRef') {
58 $options->{isa} = subtype('HashRef' => where {
59 foreach my $x (values %$_) { ($container_type_constraint->check($x)) || return } 1;
63 confess "Your isa must be either ArrayRef or HashRef (sorry no subtype support yet)";
70 no Moose::Util::TypeConstraints;
88 All complex software has bugs lurking in it, and this module is no
89 exception. If you find a bug please either email me, or add the bug
94 Stevan Little E<lt>stevan@iinteractive.comE<gt>
96 =head1 COPYRIGHT AND LICENSE
98 Copyright 2007 by Infinity Interactive, Inc.
100 L<http://www.iinteractive.com>
102 This library is free software; you can redistribute it and/or modify
103 it under the same terms as Perl itself.