177bdaa1746a97da2223a78b8e36a376a1ebd82b
[gitmo/Moose.git] / lib / Moose / AttributeHelpers.pm
1
2 package Moose::AttributeHelpers;
3
4 our $VERSION   = '0.87';
5 $VERSION = eval $VERSION;
6 our $AUTHORITY = 'cpan:STEVAN';
7
8 use Moose;
9
10 use Moose::AttributeHelpers::Trait::Bool;
11 use Moose::AttributeHelpers::Trait::Counter;
12 use Moose::AttributeHelpers::Trait::Number;
13 use Moose::AttributeHelpers::Trait::String;
14 use Moose::AttributeHelpers::Trait::Collection::List;
15 use Moose::AttributeHelpers::Trait::Collection::Array;
16 use Moose::AttributeHelpers::Trait::Collection::Hash;
17 use Moose::AttributeHelpers::Trait::Collection::ImmutableHash;
18
19 1;
20
21 __END__
22
23 =pod
24
25 =head1 NAME
26
27 Moose::AttributeHelpers - Extend your attribute interfaces
28
29 =head1 SYNOPSIS
30
31   package MyClass;
32   use Moose;
33   use Moose::AttributeHelpers;
34
35   has 'mapping' => (
36       traits    => [ 'Collection::Hash' ],
37       is        => 'rw',
38       isa       => 'HashRef[Str]',
39       default   => sub { {} },
40       handles   => {
41           exists_in_mapping => 'exists',
42           ids_in_mapping    => 'keys',
43           get_mapping       => 'get',
44           set_mapping       => 'set',
45           set_quantity      => [ set => [ 'quantity' ] ],
46       },
47   );
48
49
50   # ...
51
52   my $obj = MyClass->new;
53   $obj->set_quantity(10);      # quantity => 10
54   $obj->set_mapping(4, 'foo'); # 4 => 'foo'
55   $obj->set_mapping(5, 'bar'); # 5 => 'bar'
56   $obj->set_mapping(6, 'baz'); # 6 => 'baz'
57
58
59   # prints 'bar'
60   print $obj->get_mapping(5) if $obj->exists_in_mapping(5);
61
62   # prints '4, 5, 6'
63   print join ', ', $obj->ids_in_mapping;
64
65 =head1 DESCRIPTION
66
67 While L<Moose> attributes provide you with a way to name your accessors,
68 readers, writers, clearers and predicates, this library provides commonly
69 used attribute helper methods for more specific types of data.
70
71 As seen in the L</SYNOPSIS>, you specify the extension via the
72 C<trait> parameter. Available meta classes are below; see L</METHOD PROVIDERS>.
73
74 This module used to exist as the L<MooseX::AttributeHelpers> extension. It was
75 very commonly used, so we moved it into core Moose. Since this gave us a chance
76 to change the interface, you will have to change your code or continue using
77 the L<MooseX::AttributeHelpers> extension.
78
79 =head1 PARAMETERS
80
81 =head2 handles
82
83 This is like C<< handles >> in L<Moose/has>, but only HASH references are
84 allowed.  Keys are method names that you want installed locally, and values are
85 methods from the method providers (below).  Currying with delegated methods
86 works normally for C<< handles >>.
87
88 =head1 METHOD PROVIDERS
89
90 =over
91
92 =item L<Number|Moose::AttributeHelpers::Trait::Number>
93
94 Common numerical operations.
95
96 =item L<String|Moose::AttributeHelpers::Trait::String>
97
98 Common methods for string operations.
99
100 =item L<Counter|Moose::AttributeHelpers::Trait::Counter>
101
102 Methods for incrementing and decrementing a counter attribute.
103
104 =item L<Bool|Moose::AttributeHelpers::Trait::Bool>
105
106 Common methods for boolean values.
107
108 =item L<Collection::Hash|Moose::AttributeHelpers::Trait::Collection::Hash>
109
110 Common methods for hash references.
111
112 =item L<Collection::ImmutableHash|Moose::AttributeHelpers::Trait::Collection::ImmutableHash>
113
114 Common methods for inspecting hash references.
115
116 =item L<Collection::Array|Moose::AttributeHelpers::Trait::Collection::Array>
117
118 Common methods for array references.
119
120 =item L<Collection::List|Moose::AttributeHelpers::Trait::Collection::List>
121
122 Common list methods for array references.
123
124 =back
125
126 =head1 BUGS
127
128 All complex software has bugs lurking in it, and this module is no
129 exception. If you find a bug please either email me, or add the bug
130 to cpan-RT.
131
132 =head1 AUTHOR
133
134 Stevan Little E<lt>stevan@iinteractive.comE<gt>
135
136 B<with contributions from:>
137
138 Robert (rlb3) Boone
139
140 Paul (frodwith) Driver
141
142 Shawn (Sartak) Moore
143
144 Chris (perigrin) Prather
145
146 Robert (phaylon) Sedlacek
147
148 Tom (dec) Lanyon
149
150 Yuval Kogman
151
152 Jason May
153
154 Cory (gphat) Watson
155
156 Florian (rafl) Ragwitz
157
158 Evan Carroll
159
160 Jesse (doy) Luehrs
161
162 =head1 COPYRIGHT AND LICENSE
163
164 Copyright 2007-2009 by Infinity Interactive, Inc.
165
166 L<http://www.iinteractive.com>
167
168 This library is free software; you can redistribute it and/or modify
169 it under the same terms as Perl itself.
170
171 =cut