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