Don't test Native MethodProvider classes for pod coverage.
[gitmo/Moose.git] / lib / Moose / Meta / Attribute / Native / Trait / Array.pm
CommitLineData
e3c07b19 1
c466e58f 2package Moose::Meta::Attribute::Native::Trait::Array;
e3c07b19 3use Moose::Role;
4
122a129a 5our $VERSION = '0.89';
e3c07b19 6$VERSION = eval $VERSION;
7our $AUTHORITY = 'cpan:STEVAN';
8
c466e58f 9use Moose::Meta::Attribute::Native::MethodProvider::Array;
e3c07b19 10
c466e58f 11with 'Moose::Meta::Attribute::Native::Trait';
e3c07b19 12
13has 'method_provider' => (
14 is => 'ro',
15 isa => 'ClassName',
16 predicate => 'has_method_provider',
c466e58f 17 default => 'Moose::Meta::Attribute::Native::MethodProvider::Array'
e3c07b19 18);
19
2e069f5a 20sub _helper_type { 'ArrayRef' }
e3c07b19 21
22no Moose::Role;
23
e3c07b19 241;
25
26__END__
27
28=pod
29
30=head1 NAME
31
c466e58f 32Moose::Meta::Attribute::Native::Trait::Array
e3c07b19 33
34=head1 SYNOPSIS
35
33f819e1 36 package Stuff;
37 use Moose;
33f819e1 38
39 has 'options' => (
40 traits => ['Array'],
41 is => 'ro',
42 isa => 'ArrayRef[Str]',
43 default => sub { [] },
44 handles => {
45 all_options => 'elements',
46 map_options => 'map',
47 filter_options => 'grep',
48 find_option => 'find',
49 first_option => 'first',
50 last_option => 'last',
51 get_option => 'get',
52 join_options => 'join',
53 count_options => 'count',
af44c00c 54 has_no_options => 'empty',
33f819e1 55 sorted_options => 'sort',
56 }
57 );
58
59 no Moose;
60 1;
80683705 61
e3c07b19 62=head1 DESCRIPTION
63
64This module provides an Array attribute which provides a number of
80683705 65array operations.
33f819e1 66
67=head1 PROVIDED METHODS
68
69These methods are implemented in
70L<Moose::Meta::Attribute::Native::MethodProvider::Array>.
71
72=over 4
73
74=item B<count>
75
76Returns the number of elements in the array.
77
78 $stuff = Stuff->new;
79 $stuff->options(["foo", "bar", "baz", "boo"]);
80
81 my $count = $stuff->count_options;
82 print "$count\n"; # prints 4
83
84=item B<empty>
85
80683705 86Returns a boolean value indicating whether or not the array has any elements.
33f819e1 87
af44c00c 88 $stuff->has_no_options ? die "No options!\n" : print "Good boy.\n";
33f819e1 89
cd50e921 90=item B<elements>
91
92Returns all of the elements of the array.
93
94 my @option = $stuff->all_options;
95 print "@options\n"; # prints "foo bar baz boo"
96
97=item B<get($index)>
98
99Returns an element of the array by its index. You can also use negative index
100numbers, just as with Perl's core array handling.
101
102 my $option = $stuff->get_option(1);
103 print "$option\n"; # prints "bar"
104
105=item B<pop>
106
107=item B<push($value)>
108
109=item B<shift>
110
111=item B<unshift($value)>
112
113=item B<splice($offset, $length, @values)>
114
115These methods are all equivalent to the Perl core functions of the same name.
116
117=item B<find( sub { ... } )>
33f819e1 118
80683705 119This method returns the first item matching item in the array. The matching is
120done with a subroutine reference you pass to this method. The reference will
121be called against each element in the array until one matches or all elements
122have been checked.
33f819e1 123
124 my $found = $stuff->find_option( sub { $_[0] =~ /^b/ } );
125 print "$found\n"; # prints "bar"
126
cd50e921 127=item B<grep( sub { ... } )>
33f819e1 128
80683705 129This method returns every element matching a given criteria, just like Perl's
130core C<grep> function. This method requires a subroutine which implements the
131matching logic.
33f819e1 132
133 my @found = $stuff->filter_options( sub { $_[0] =~ /^b/ } );
134 print "@found\n"; # prints "bar baz boo"
135
cd50e921 136=item B<map( sub { ... } )>
33f819e1 137
80683705 138This method transforms every element in the array and returns a new array,
139just like Perl's core C<map> function. This method requires a subroutine which
140implements the transformation.
33f819e1 141
142 my @mod_options = $stuff->map_options( sub { $_[0] . "-tag" } );
143 print "@mod_options\n"; # prints "foo-tag bar-tag baz-tag boo-tag"
144
cd50e921 145=item B<sort( sub { ... } )>
33f819e1 146
80683705 147Returns a the array in sorted order.
33f819e1 148
80683705 149You can provide an optional subroutine reference to sort with (as you can with
150Perl's core C<sort> function). However, instead of using C<$a> and C<$b>, you
151will need to use C<$_[0]> and C<$_[1]> instead.
33f819e1 152
153 # ascending ASCIIbetical
154 my @sorted = $stuff->sort_options();
155
156 # Descending alphabetical order
157 my @sorted_options = $stuff->sort_options( sub { lc $_[1] cmp lc $_[0] } );
158 print "@sorted_options\n"; # prints "foo boo baz bar"
159
cd50e921 160=item B<sort_in_place>
33f819e1 161
cd50e921 162Sorts the array I<in place>, modifying the value of the attribute.
33f819e1 163
cd50e921 164You can provide an optional subroutine reference to sort with (as you can with
165Perl's core C<sort> function). However, instead of using C<$a> and C<$b>, you
166will need to use C<$_[0]> and C<$_[1]> instead.
33f819e1 167
cd50e921 168=item B<join($str)>
33f819e1 169
80683705 170Joins every element of the array using the separator given as argument, just
171like Perl's core C<join> function.
33f819e1 172
173 my $joined = $stuff->join_options( ':' );
174 print "$joined\n"; # prints "foo:bar:baz:boo"
175
cd50e921 176=item B<set($index, $value)>
33f819e1 177
cd50e921 178Given an index and a value, sets the specified array element's value.
33f819e1 179
cd50e921 180=item B<delete($index)>
181
182Removes the element at the given index from the array.
183
184=item B<insert($index, $value)>
185
186Inserts a new element into the array at the given index.
187
188=item B<clear>
189
190Empties the entire array, like C<@array = ()>.
33f819e1 191
192=item B<first>
193
194Returns the first element of the array.
195
196 my $first = $stuff->first_option;
197 print "$first\n"; # prints "foo"
198
199=item B<last>
200
201Returns the last element of the array.
202
203 my $last = $stuff->last_option;
204 print "$last\n"; # prints "boo"
205
33f819e1 206=item B<accessor>
207
80683705 208This method provides a get/set accessor for the array, based on array indexes.
209If passed one argument, it returns the value at the specified index. If
210passed two arguments, it sets the value of the specified index.
33f819e1 211
212=back
e3c07b19 213
214=head1 METHODS
215
216=over 4
217
218=item B<meta>
219
220=item B<method_provider>
221
222=item B<has_method_provider>
223
e3c07b19 224=back
225
226=head1 BUGS
227
228All complex software has bugs lurking in it, and this module is no
229exception. If you find a bug please either email me, or add the bug
230to cpan-RT.
231
232=head1 AUTHOR
233
234Stevan Little E<lt>stevan@iinteractive.comE<gt>
235
236=head1 COPYRIGHT AND LICENSE
237
238Copyright 2007-2009 by Infinity Interactive, Inc.
239
240L<http://www.iinteractive.com>
241
242This library is free software; you can redistribute it and/or modify
243it under the same terms as Perl itself.
244
245=cut