bump version to 0.89_01
[gitmo/Moose.git] / lib / Moose / Meta / Attribute / Native.pm
CommitLineData
17e5e226 1package Moose::Meta::Attribute::Native;
e3c07b19 2
9039e8ec 3our $VERSION = '0.89_01';
e3c07b19 4$VERSION = eval $VERSION;
5our $AUTHORITY = 'cpan:STEVAN';
6
fafc8b9b 7my @trait_names = qw(Bool Counter Number String Array Hash);
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',
54 set_quantity => [ set => [ 'quantity' ] ],
55 },
56 );
57
58
59 # ...
60
61 my $obj = MyClass->new;
62 $obj->set_quantity(10); # quantity => 10
63 $obj->set_mapping(4, 'foo'); # 4 => 'foo'
64 $obj->set_mapping(5, 'bar'); # 5 => 'bar'
65 $obj->set_mapping(6, 'baz'); # 6 => 'baz'
66
67
68 # prints 'bar'
69 print $obj->get_mapping(5) if $obj->exists_in_mapping(5);
70
71 # prints '4, 5, 6'
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,
77writers, clearers and predicates, this library 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
5f3663b2 81C<trait> parameter. Available meta classes 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
86the L<MooseX::AttributeHelpers> extension.
87
e3c07b19 88=head1 PARAMETERS
89
90=head2 handles
91
5f3663b2 92This is like C<< handles >> in L<Moose/has>, but only HASH references are
93allowed. Keys are method names that you want installed locally, and values are
87b4e821 94methods from the method providers (below). Currying with delegated methods
95works normally for C<< handles >>.
e3c07b19 96
97=head1 METHOD PROVIDERS
98
99=over
100
c466e58f 101=item L<Number|Moose::Meta::Attribute::Native::Trait::Number>
e3c07b19 102
103Common numerical operations.
104
52e0d71f 105 has 'integer' => (
9958cbe1 106 traits => ['Number'],
52e0d71f 107 is => 'ro',
108 isa => 'Int',
109 default => 5,
9958cbe1 110 handles => {
52e0d71f 111 set => 'set',
112 add => 'add',
113 sub => 'sub',
114 mul => 'mul',
115 div => 'div',
116 mod => 'mod',
117 abs => 'abs',
118 }
119 );
120
c466e58f 121=item L<String|Moose::Meta::Attribute::Native::Trait::String>
e3c07b19 122
123Common methods for string operations.
124
52e0d71f 125 has 'text' => (
9958cbe1 126 traits => ['String'],
52e0d71f 127 is => 'rw',
128 isa => 'Str',
129 default => q{},
9958cbe1 130 handles => {
52e0d71f 131 add_text => 'append',
132 replace_text => 'replace',
133 }
134 );
135
c466e58f 136=item L<Counter|Moose::Meta::Attribute::Native::Trait::Counter>
e3c07b19 137
138Methods for incrementing and decrementing a counter attribute.
139
52e0d71f 140 has 'counter' => (
9958cbe1 141 traits => ['Counter'],
52e0d71f 142 is => 'ro',
143 isa => 'Num',
144 default => 0,
145 handles => {
146 inc_counter => 'inc',
147 dec_counter => 'dec',
148 reset_counter => 'reset',
149 }
150 );
151
c466e58f 152=item L<Bool|Moose::Meta::Attribute::Native::Trait::Bool>
e3c07b19 153
154Common methods for boolean values.
155
52e0d71f 156 has 'is_lit' => (
9958cbe1 157 traits => ['Bool'],
52e0d71f 158 is => 'rw',
159 isa => 'Bool',
160 default => 0,
161 handles => {
162 illuminate => 'set',
163 darken => 'unset',
164 flip_switch => 'toggle',
165 is_dark => 'not',
166 }
167 );
168
c466e58f 169=item L<Hash|Moose::Meta::Attribute::Native::Trait::Hash>
e3c07b19 170
171Common methods for hash references.
172
52e0d71f 173 has 'options' => (
174 traits => ['Hash'],
175 is => 'ro',
176 isa => 'HashRef[Str]',
177 default => sub { {} },
178 handles => {
9958cbe1 179 set_option => 'set',
180 get_option => 'get',
181 has_option => 'exists',
52e0d71f 182 }
183 );
e3c07b19 184
c466e58f 185=item L<Array|Moose::Meta::Attribute::Native::Trait::Array>
e3c07b19 186
187Common methods for array references.
188
52e0d71f 189 has 'queue' => (
190 traits => ['Array'],
191 is => 'ro',
192 isa => 'ArrayRef[Str]',
193 default => sub { [] },
9958cbe1 194 handles => {
195 add_item => 'push',
52e0d71f 196 next_item => 'shift',
197 }
198 );
e3c07b19 199
200=back
201
e3c07b19 202=head1 BUGS
203
204All complex software has bugs lurking in it, and this module is no
205exception. If you find a bug please either email me, or add the bug
206to cpan-RT.
207
208=head1 AUTHOR
209
210Stevan Little E<lt>stevan@iinteractive.comE<gt>
211
212B<with contributions from:>
213
214Robert (rlb3) Boone
215
216Paul (frodwith) Driver
217
218Shawn (Sartak) Moore
219
220Chris (perigrin) Prather
221
222Robert (phaylon) Sedlacek
223
224Tom (dec) Lanyon
225
226Yuval Kogman
227
228Jason May
229
230Cory (gphat) Watson
231
232Florian (rafl) Ragwitz
233
234Evan Carroll
235
236Jesse (doy) Luehrs
237
238=head1 COPYRIGHT AND LICENSE
239
240Copyright 2007-2009 by Infinity Interactive, Inc.
241
242L<http://www.iinteractive.com>
243
244This library is free software; you can redistribute it and/or modify
245it under the same terms as Perl itself.
246
247=cut