No longer need to qualify the Bag type constraint with an "if we don't already have it"
[gitmo/MooseX-AttributeHelpers.git] / lib / MooseX / AttributeHelpers / Trait / Collection / Bag.pm
1
2 package MooseX::AttributeHelpers::Trait::Collection::Bag;
3 use Moose::Role;
4 use Moose::Util::TypeConstraints;
5
6 our $VERSION   = '0.01';
7 our $AUTHORITY = 'cpan:STEVAN';
8
9 use MooseX::AttributeHelpers::MethodProvider::Bag;
10
11 with 'MooseX::AttributeHelpers::Trait::Collection';
12
13 has 'method_provider' => (
14     is        => 'ro',
15     isa       => 'ClassName',
16     predicate => 'has_method_provider',
17     default   => 'MooseX::AttributeHelpers::MethodProvider::Bag'
18 );
19
20 subtype 'Bag' => as 'HashRef[Int]';
21
22 sub helper_type { 'Bag' }
23
24 before 'process_options_for_provides' => sub {
25     my ($self, $options, $name) = @_;
26
27     # Set some default attribute options here unless already defined
28     if ((my $type = $self->helper_type) && !exists $options->{isa}){
29         $options->{isa} = $type;
30     }
31     
32     $options->{default} = sub { +{} } unless exists $options->{default};
33 };
34
35 no Moose::Role;
36 no Moose::Util::TypeConstraints;
37
38 # register the alias ...
39 package # hide me from search.cpan.org
40     Moose::Meta::Attribute::Custom::Trait::Collection::Bag;
41 sub register_implementation {
42     'MooseX::AttributeHelpers::Trait::Collection::Bag'
43 }
44
45 1;
46
47 __END__
48
49 =pod
50
51 =head1 NAME
52
53 MooseX::AttributeHelpers::Collection::Bag
54
55 =head1 SYNOPSIS
56
57   package Stuff;
58   use Moose;
59   use MooseX::AttributeHelpers;
60   
61   has 'word_histogram' => (
62       metaclass => 'Collection::Bag',
63       is        => 'ro',
64       isa       => 'Bag', # optional ... as is defalt
65       provides  => {
66           'add'    => 'add_word',
67           'get'    => 'get_count_for',            
68           'empty'  => 'has_any_words',
69           'count'  => 'num_words',
70           'delete' => 'delete_word',
71       }
72   );
73   
74 =head1 DESCRIPTION
75
76 This module provides a Bag attribute which provides a number of 
77 bag-like operations. See L<MooseX::AttributeHelpers::MethodProvider::Bag>
78 for more details.
79
80 =head1 METHODS
81
82 =over 4
83
84 =item B<meta>
85
86 =item B<method_provider>
87
88 =item B<has_method_provider>
89
90 =item B<helper_type>
91
92 =item B<process_options_for_provides>
93
94 =back
95
96 =head1 BUGS
97
98 All complex software has bugs lurking in it, and this module is no 
99 exception. If you find a bug please either email me, or add the bug
100 to cpan-RT.
101
102 =head1 AUTHOR
103
104 Stevan Little E<lt>stevan@iinteractive.comE<gt>
105
106 =head1 COPYRIGHT AND LICENSE
107
108 Copyright 2007-2008 by Infinity Interactive, Inc.
109
110 L<http://www.iinteractive.com>
111
112 This library is free software; you can redistribute it and/or modify
113 it under the same terms as Perl itself.
114
115 =cut
116