some POD advances
[gitmo/MooseX-AttributeHelpers.git] / lib / MooseX / AttributeHelpers / Collection.pm
CommitLineData
8c651099 1
2package MooseX::AttributeHelpers::Collection;
3use Moose;
4use Moose::Util::TypeConstraints;
5
6our $VERSION = '0.01';
7our $AUTHORITY = 'cpan:STEVAN';
8
9extends 'MooseX::AttributeHelpers::Base';
10
11has 'container_type' => (
12 is => 'ro',
13 isa => 'Str',
14 predicate => 'has_container_type',
15);
16
17has 'container_type_constraint' => (
9810162d 18 is => 'rw',
19 isa => 'Moose::Meta::TypeConstraint',
8c651099 20);
21
22before 'process_options_for_provides' => sub {
9810162d 23 my ($self, $options) = @_;
8c651099 24
25 if (exists $options->{isa}) {
26 my $type = $options->{isa};
77d02b8b 27
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
32
8c651099 33 if ($type =~ /^(.*)\[(.*)\]$/) {
34 my $core_type = $1;
35 my $container_type = $2;
77d02b8b 36
8c651099 37 $options->{container_type} = $container_type;
77d02b8b 38
39 my $container_type_constraint = find_type_constraint($container_type);
40
41 # NOTE:
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.
46 # - SL
47 (defined $container_type_constraint)
48 || confess "You must predefine the '$container_type' constraint before you can use it as a container type";
49
50 $options->{container_type_constraint} = $container_type_constraint;
51
52 if ($core_type eq 'ArrayRef') {
53 $options->{isa} = subtype('ArrayRef' => where {
54 foreach my $x (@$_) { ($container_type_constraint->check($x)) || return } 1;
55 });
56 }
57 elsif ($core_type eq 'HashRef') {
58 $options->{isa} = subtype('HashRef' => where {
59 foreach my $x (values %$_) { ($container_type_constraint->check($x)) || return } 1;
60 });
61 }
62 else {
63 confess "Your isa must be either ArrayRef or HashRef (sorry no subtype support yet)";
64 }
8c651099 65 }
66 }
67};
68
69no Moose;
c25a396f 70no Moose::Util::TypeConstraints;
8c651099 71
721;
73
74__END__
75
76=pod
77
78=head1 NAME
79
80=head1 SYNOPSIS
81
82=head1 DESCRIPTION
83
84=head1 METHODS
85
86=head1 BUGS
87
88All complex software has bugs lurking in it, and this module is no
89exception. If you find a bug please either email me, or add the bug
90to cpan-RT.
91
92=head1 AUTHOR
93
94Stevan Little E<lt>stevan@iinteractive.comE<gt>
95
96=head1 COPYRIGHT AND LICENSE
97
98Copyright 2007 by Infinity Interactive, Inc.
99
100L<http://www.iinteractive.com>
101
102This library is free software; you can redistribute it and/or modify
103it under the same terms as Perl itself.
104
105=cut