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