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