package Moose::Meta::Attribute::Native::Trait::Array;
use Moose::Role;
-our $VERSION = '0.89';
+our $VERSION = '0.95';
$VERSION = eval $VERSION;
our $AUTHORITY = 'cpan:STEVAN';
=head1 NAME
-Moose::Meta::Attribute::Native::Trait::Array
+Moose::Meta::Attribute::Native::Trait::Array - Helper trait for ArrayRef attributes
=head1 SYNOPSIS
is => 'ro',
isa => 'ArrayRef[Str]',
default => sub { [] },
- handles => {
- all_options => 'elements',
- map_options => 'map',
- filter_options => 'grep',
- find_option => 'first',
- first_option => 'head',
- all_but_first_option => 'tail',
- last_option => 'last',
- get_option => 'get',
- join_options => 'join',
- count_options => 'count',
- has_no_options => 'empty',
- sorted_options => 'sort',
- }
+ handles => {
+ all_options => 'elements',
+ add_option => 'push',
+ map_options => 'map',
+ filter_options => 'grep',
+ find_option => 'first',
+ get_option => 'get',
+ join_options => 'join',
+ count_options => 'count',
+ has_options => 'count',
+ has_no_options => 'is_empty',
+ sorted_options => 'sort',
+ },
);
no Moose;
my $count = $stuff->count_options;
print "$count\n"; # prints 4
-=item B<empty>
+=item B<is_empty>
-Returns a boolean value indicating whether or not the array has any elements.
+Returns a boolean value that is true when the array has no elements.
$stuff->has_no_options ? die "No options!\n" : print "Good boy.\n";
=item B<pop>
-=item B<push($value)>
+=item B<push($value1, $value2, value3 ...)>
=item B<shift>
-=item B<unshift($value)>
+=item B<unshift($value1, $value2, value3 ...)>
=item B<splice($offset, $length, @values)>
=item B<first( sub { ... } )>
-This method returns the first item matching item in the array. The matching is
-done with a subroutine reference you pass to this method. The reference will
-be called against each element in the array until one matches or all elements
-have been checked.
+This method returns the first item matching item in the array, just like
+L<List::Util>'s C<first> function. The matching is done with a subroutine
+reference you pass to this method. The reference will be called against each
+element in the array until one matches or all elements have been checked.
my $found = $stuff->find_option( sub { /^b/ } );
print "$found\n"; # prints "bar"
my @mod_options = $stuff->map_options( sub { $_ . "-tag" } );
print "@mod_options\n"; # prints "foo-tag bar-tag baz-tag boo-tag"
+=item B<reduce( sub { ... } )>
+
+This method condenses an array into a single value, by passing a function the
+value so far and the next value in the array, just like L<List::Util>'s
+C<reduce> function. The reducing is done with a subroutine reference you pass
+to this method.
+
+ my $found = $stuff->reduce_options( sub { $_[0] . $_[1] } );
+ print "$found\n"; # prints "foobarbazboo"
+
=item B<sort( sub { ... } )>
Returns a the array in sorted order.
Perl's core C<sort> function). However, instead of using C<$a> and C<$b>, you
will need to use C<$_[0]> and C<$_[1]> instead.
+=item B<shuffle>
+
+Returns the array, with indices in random order, like C<shuffle> from
+L<List::Util>.
+
+=item B<uniq>
+
+Returns the array, with all duplicate elements removed, like C<uniq> from
+L<List::MoreUtils>.
+
=item B<join($str)>
Joins every element of the array using the separator given as argument, just
Empties the entire array, like C<@array = ()>.
-=item B<head>
-
-Returns the first element of the array.
-
- my $first = $stuff->first_option;
- print "$first\n"; # prints "foo"
-
-=item B<tail>
-
-Returns all elements of the array after the first.
-
- my @tail = $stuff->all_but_first_option;
- print join(', ', @tail), "\n"; # prints "bar, baz, boo"
-
-=item B<last>
-
-Returns the last element of the array.
-
- my $last = $stuff->last_option;
- print "$last\n"; # prints "boo"
-
=item B<accessor>
This method provides a get/set accessor for the array, based on array indexes.
If passed one argument, it returns the value at the specified index. If
passed two arguments, it sets the value of the specified index.
+=item B<natatime($n, $code)>
+
+This method returns an iterator which, on each call, returns C<$n> more items
+from the array, in order, like C<natatime> from L<List::MoreUtils>. A coderef
+can optionally be provided; it will be called on each group of C<$n> elements
+in the array.
+
=back
=head1 METHODS
=head1 BUGS
-All complex software has bugs lurking in it, and this module is no
-exception. If you find a bug please either email me, or add the bug
-to cpan-RT.
+See L<Moose/BUGS> for details on reporting bugs.
=head1 AUTHOR