bump version to 0.93_03
[gitmo/Moose.git] / lib / Moose / Meta / Attribute / Native.pm
CommitLineData
17e5e226 1package Moose::Meta::Attribute::Native;
e3c07b19 2
7a10df4d 3our $VERSION = '0.93_03';
e3c07b19 4$VERSION = eval $VERSION;
5our $AUTHORITY = 'cpan:STEVAN';
6
cdf3cae6 7my @trait_names = qw(Bool Counter Number String Array Hash Code);
fafc8b9b 8
9for my $trait_name (@trait_names) {
c466e58f 10 my $trait_class = "Moose::Meta::Attribute::Native::Trait::$trait_name";
fafc8b9b 11 my $meta = Class::MOP::Class->initialize(
12 "Moose::Meta::Attribute::Custom::Trait::$trait_name"
13 );
14 if ($meta->find_method_by_name('register_implementation')) {
15 my $class = $meta->name->register_implementation;
16 Moose->throw_error(
17 "An implementation for $trait_name already exists " .
18 "(found '$class' when trying to register '$trait_class')"
19 );
20 }
21 $meta->add_method(register_implementation => sub {
22 # resolve_metatrait_alias will load classes anyway, but throws away
23 # their error message; we WANT to die if there's a problem
24 Class::MOP::load_class($trait_class);
25 return $trait_class;
26 });
27}
e3c07b19 28
e3c07b19 291;
30
31__END__
32
33=pod
34
35=head1 NAME
36
17e5e226 37Moose::Meta::Attribute::Native - Extend your attribute interfaces
e3c07b19 38
39=head1 SYNOPSIS
40
41 package MyClass;
42 use Moose;
e3c07b19 43
44 has 'mapping' => (
a40b446a 45 traits => [ 'Hash' ],
e3c07b19 46 is => 'rw',
47 isa => 'HashRef[Str]',
48 default => sub { {} },
49 handles => {
50 exists_in_mapping => 'exists',
51 ids_in_mapping => 'keys',
52 get_mapping => 'get',
53 set_mapping => 'set',
cb562ad2 54 set_quantity => [ set => 'quantity' ],
e3c07b19 55 },
56 );
57
58
59 # ...
60
61 my $obj = MyClass->new;
62 $obj->set_quantity(10); # quantity => 10
2420461c 63 $obj->set_mapping('foo', 4); # foo => 4
64 $obj->set_mapping('bar', 5); # bar => 5
65 $obj->set_mapping('baz', 6); # baz => 6
e3c07b19 66
67
2420461c 68 # prints 5
69 print $obj->get_mapping('bar') if $obj->exists_in_mapping('bar');
e3c07b19 70
2420461c 71 # prints 'quantity, foo, bar, baz'
e3c07b19 72 print join ', ', $obj->ids_in_mapping;
73
74=head1 DESCRIPTION
75
9958cbe1 76While L<Moose> attributes provide a way to name your accessors, readers,
2420461c 77writers, clearers and predicates, this set of traits provides commonly
e3c07b19 78used attribute helper methods for more specific types of data.
79
9958cbe1 80As seen in the L</SYNOPSIS>, you specify the data structure via the
2420461c 81C<trait> parameter. Available traits are below; see L</METHOD PROVIDERS>.
e3c07b19 82
87b4e821 83This module used to exist as the L<MooseX::AttributeHelpers> extension. It was
84very commonly used, so we moved it into core Moose. Since this gave us a chance
85to change the interface, you will have to change your code or continue using
2420461c 86the L<MooseX::AttributeHelpers> extension. L<MooseX::AttributeHelpers> should
87continue to work.
87b4e821 88
e3c07b19 89=head1 PARAMETERS
90
91=head2 handles
92
5f3663b2 93This is like C<< handles >> in L<Moose/has>, but only HASH references are
94allowed. Keys are method names that you want installed locally, and values are
87b4e821 95methods from the method providers (below). Currying with delegated methods
96works normally for C<< handles >>.
e3c07b19 97
98=head1 METHOD PROVIDERS
99
100=over
101
c466e58f 102=item L<Number|Moose::Meta::Attribute::Native::Trait::Number>
e3c07b19 103
104Common numerical operations.
105
52e0d71f 106 has 'integer' => (
9958cbe1 107 traits => ['Number'],
52e0d71f 108 is => 'ro',
109 isa => 'Int',
110 default => 5,
9958cbe1 111 handles => {
52e0d71f 112 set => 'set',
113 add => 'add',
114 sub => 'sub',
115 mul => 'mul',
116 div => 'div',
117 mod => 'mod',
118 abs => 'abs',
119 }
120 );
121
c466e58f 122=item L<String|Moose::Meta::Attribute::Native::Trait::String>
e3c07b19 123
124Common methods for string operations.
125
52e0d71f 126 has 'text' => (
9958cbe1 127 traits => ['String'],
52e0d71f 128 is => 'rw',
129 isa => 'Str',
130 default => q{},
9958cbe1 131 handles => {
52e0d71f 132 add_text => 'append',
133 replace_text => 'replace',
134 }
135 );
136
c466e58f 137=item L<Counter|Moose::Meta::Attribute::Native::Trait::Counter>
e3c07b19 138
139Methods for incrementing and decrementing a counter attribute.
140
52e0d71f 141 has 'counter' => (
9958cbe1 142 traits => ['Counter'],
52e0d71f 143 is => 'ro',
144 isa => 'Num',
145 default => 0,
146 handles => {
147 inc_counter => 'inc',
148 dec_counter => 'dec',
149 reset_counter => 'reset',
150 }
151 );
152
c466e58f 153=item L<Bool|Moose::Meta::Attribute::Native::Trait::Bool>
e3c07b19 154
155Common methods for boolean values.
156
52e0d71f 157 has 'is_lit' => (
9958cbe1 158 traits => ['Bool'],
52e0d71f 159 is => 'rw',
160 isa => 'Bool',
161 default => 0,
162 handles => {
163 illuminate => 'set',
164 darken => 'unset',
165 flip_switch => 'toggle',
166 is_dark => 'not',
167 }
168 );
169
c466e58f 170=item L<Hash|Moose::Meta::Attribute::Native::Trait::Hash>
e3c07b19 171
172Common methods for hash references.
173
52e0d71f 174 has 'options' => (
175 traits => ['Hash'],
176 is => 'ro',
177 isa => 'HashRef[Str]',
178 default => sub { {} },
179 handles => {
9958cbe1 180 set_option => 'set',
181 get_option => 'get',
182 has_option => 'exists',
52e0d71f 183 }
184 );
e3c07b19 185
c466e58f 186=item L<Array|Moose::Meta::Attribute::Native::Trait::Array>
e3c07b19 187
188Common methods for array references.
189
52e0d71f 190 has 'queue' => (
191 traits => ['Array'],
192 is => 'ro',
193 isa => 'ArrayRef[Str]',
194 default => sub { [] },
9958cbe1 195 handles => {
196 add_item => 'push',
52e0d71f 197 next_item => 'shift',
198 }
199 );
e3c07b19 200
b86a4688 201=item L<Code|Moose::Meta::Attribute::Native::Trait::Code>
202
203Common methods for code references.
204
205 has 'callback' => (
206 traits => ['Code'],
207 is => 'ro',
208 isa => 'CodeRef',
209 default => sub { sub { 'called' } },
210 handles => {
211 call => 'execute',
212 }
213 );
214
e3c07b19 215=back
216
e3c07b19 217=head1 BUGS
218
d4048ef3 219See L<Moose/BUGS> for details on reporting bugs.
e3c07b19 220
221=head1 AUTHOR
222
223Stevan Little E<lt>stevan@iinteractive.comE<gt>
224
225B<with contributions from:>
226
227Robert (rlb3) Boone
228
229Paul (frodwith) Driver
230
231Shawn (Sartak) Moore
232
233Chris (perigrin) Prather
234
235Robert (phaylon) Sedlacek
236
237Tom (dec) Lanyon
238
239Yuval Kogman
240
241Jason May
242
243Cory (gphat) Watson
244
245Florian (rafl) Ragwitz
246
247Evan Carroll
248
249Jesse (doy) Luehrs
250
52a98907 251Jay Hannah
252
253Robert Buels
254
e3c07b19 255=head1 COPYRIGHT AND LICENSE
256
257Copyright 2007-2009 by Infinity Interactive, Inc.
258
259L<http://www.iinteractive.com>
260
261This library is free software; you can redistribute it and/or modify
262it under the same terms as Perl itself.
263
264=cut