Commit | Line | Data |
8c651099 |
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); |
69dde336 |
28 | |
29 | # NOTE: |
30 | # I am not sure DWIM-ery is a good thing |
31 | # here, so i am going to err on the side |
32 | # of caution, and blow up if you have |
33 | # not made a type constraint for this yet. |
34 | # - SL |
35 | (defined $constraint) |
36 | || confess "You must predefine the '$container_type' constraint before you can use it as a container type"; |
8c651099 |
37 | |
38 | return $constraint; |
39 | } |
40 | ); |
41 | |
42 | before 'process_options_for_provides' => sub { |
43 | my ($self, $options) = @_; |
44 | |
45 | if (exists $options->{isa}) { |
46 | my $type = $options->{isa}; |
47 | if ($type =~ /^(.*)\[(.*)\]$/) { |
48 | my $core_type = $1; |
49 | my $container_type = $2; |
50 | $options->{isa} = $core_type; |
51 | $options->{container_type} = $container_type; |
52 | } |
53 | } |
54 | }; |
55 | |
56 | no Moose; |
c25a396f |
57 | no Moose::Util::TypeConstraints; |
8c651099 |
58 | |
59 | 1; |
60 | |
61 | __END__ |
62 | |
63 | =pod |
64 | |
65 | =head1 NAME |
66 | |
67 | =head1 SYNOPSIS |
68 | |
69 | =head1 DESCRIPTION |
70 | |
71 | =head1 METHODS |
72 | |
73 | =head1 BUGS |
74 | |
75 | All complex software has bugs lurking in it, and this module is no |
76 | exception. If you find a bug please either email me, or add the bug |
77 | to cpan-RT. |
78 | |
79 | =head1 AUTHOR |
80 | |
81 | Stevan Little E<lt>stevan@iinteractive.comE<gt> |
82 | |
83 | =head1 COPYRIGHT AND LICENSE |
84 | |
85 | Copyright 2007 by Infinity Interactive, Inc. |
86 | |
87 | L<http://www.iinteractive.com> |
88 | |
89 | This library is free software; you can redistribute it and/or modify |
90 | it under the same terms as Perl itself. |
91 | |
92 | =cut |