more tests and tweaks
[gitmo/MooseX-AttributeHelpers.git] / lib / MooseX / AttributeHelpers / Collection.pm
1
2 package MooseX::AttributeHelpers::Collection;
3 use Moose;
4 use Moose::Util::TypeConstraints;
5
6 our $VERSION   = '0.01';
7 our $AUTHORITY = 'cpan:STEVAN';
8
9 extends 'MooseX::AttributeHelpers::Base';
10
11 has 'container_type' => (
12     is        => 'ro',
13     isa       => 'Str',
14     predicate => 'has_container_type',
15 );
16
17 has 'container_type_constraint' => (
18     is      => 'rw',
19     isa     => 'Moose::Meta::TypeConstraint',
20     lazy    => 1,
21     default => sub {
22         my $self = shift;
23         ($self->has_container_type)
24             || confess "You cannot create a container_type_constraint if you dont have a container type";
25
26         my $container_type = $self->container_type;
27         my $constraint     = find_type_constraint($container_type);
28         
29             $constraint = subtype(
30                 'Object', 
31                 sub { 
32                     $_->isa($container_type) || ($_->can('does') && $_->does($container_type))
33                 }
34             ) unless $constraint;            
35         
36         return $constraint;
37     }
38 );
39
40 before 'process_options_for_provides' => sub {
41     my ($self, $options) = @_;
42     
43     if (exists $options->{isa}) {
44         my $type = $options->{isa};
45         if ($type =~ /^(.*)\[(.*)\]$/) {
46             my $core_type      = $1;
47             my $container_type = $2;
48             $options->{isa}            = $core_type;
49             $options->{container_type} = $container_type;
50         }
51     }
52 };
53
54 no Moose;
55 no Moose::Util::TypeConstraints;
56
57 1;
58
59 __END__
60
61 =pod
62
63 =head1 NAME
64
65 =head1 SYNOPSIS
66
67 =head1 DESCRIPTION
68
69 =head1 METHODS
70
71 =head1 BUGS
72
73 All complex software has bugs lurking in it, and this module is no 
74 exception. If you find a bug please either email me, or add the bug
75 to cpan-RT.
76
77 =head1 AUTHOR
78
79 Stevan Little E<lt>stevan@iinteractive.comE<gt>
80
81 =head1 COPYRIGHT AND LICENSE
82
83 Copyright 2007 by Infinity Interactive, Inc.
84
85 L<http://www.iinteractive.com>
86
87 This library is free software; you can redistribute it and/or modify
88 it under the same terms as Perl itself.
89
90 =cut