Commit | Line | Data |
e3c07b19 |
1 | |
2 | package Moose::AttributeHelpers; |
3 | |
96539d20 |
4 | our $VERSION = '0.87'; |
e3c07b19 |
5 | $VERSION = eval $VERSION; |
6 | our $AUTHORITY = 'cpan:STEVAN'; |
7 | |
8cf67c27 |
8 | use Moose (); |
e3c07b19 |
9 | |
a40b446a |
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; |
a40b446a |
14 | use Moose::Meta::Attribute::Trait::Native::Array; |
15 | use Moose::Meta::Attribute::Trait::Native::Hash; |
e3c07b19 |
16 | |
e3c07b19 |
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' => ( |
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 | |
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 |
5f3663b2 |
70 | C<trait> parameter. Available meta classes are below; see L</METHOD PROVIDERS>. |
e3c07b19 |
71 | |
87b4e821 |
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 | |
e3c07b19 |
77 | =head1 PARAMETERS |
78 | |
79 | =head2 handles |
80 | |
5f3663b2 |
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 |
87b4e821 |
83 | methods from the method providers (below). Currying with delegated methods |
84 | works 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 | |
92 | Common numerical operations. |
93 | |
a40b446a |
94 | =item L<String|Moose::Meta::Attribute::Trait::Native::String> |
e3c07b19 |
95 | |
96 | Common methods for string operations. |
97 | |
a40b446a |
98 | =item L<Counter|Moose::Meta::Attribute::Trait::Native::Counter> |
e3c07b19 |
99 | |
100 | Methods for incrementing and decrementing a counter attribute. |
101 | |
a40b446a |
102 | =item L<Bool|Moose::Meta::Attribute::Trait::Native::Bool> |
e3c07b19 |
103 | |
104 | Common methods for boolean values. |
105 | |
a40b446a |
106 | =item L<Hash|Moose::Meta::Attribute::Trait::Native::Hash> |
e3c07b19 |
107 | |
108 | Common methods for hash references. |
109 | |
a40b446a |
110 | =item L<ImmutableHash|Moose::Meta::Attribute::Trait::Native::ImmutableHash> |
e3c07b19 |
111 | |
112 | Common methods for inspecting hash references. |
113 | |
a40b446a |
114 | =item L<Array|Moose::Meta::Attribute::Trait::Native::Array> |
e3c07b19 |
115 | |
116 | Common methods for array references. |
117 | |
a40b446a |
118 | =item L<List|Moose::Meta::Attribute::Trait::Native::List> |
e3c07b19 |
119 | |
120 | Common list methods for array references. |
121 | |
122 | =back |
123 | |
e3c07b19 |
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 |