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