2 package Moose::Meta::Attribute::Native::Trait::Array;
6 $VERSION = eval $VERSION;
7 our $AUTHORITY = 'cpan:STEVAN';
9 use Moose::Meta::Attribute::Native::MethodProvider::Array;
11 with 'Moose::Meta::Attribute::Native::Trait';
13 has 'method_provider' => (
16 predicate => 'has_method_provider',
17 default => 'Moose::Meta::Attribute::Native::MethodProvider::Array'
20 sub _helper_type { 'ArrayRef' }
32 Moose::Meta::Attribute::Native::Trait::Array
42 isa => 'ArrayRef[Str]',
43 default => sub { [] },
45 all_options => 'elements',
47 filter_options => 'grep',
48 find_option => 'find',
49 first_option => 'first',
50 last_option => 'last',
52 join_options => 'join',
53 count_options => 'count',
54 has_no_options => 'empty',
55 sorted_options => 'sort',
64 This module provides an Array attribute which provides a number of
67 =head1 PROVIDED METHODS
69 These methods are implemented in
70 L<Moose::Meta::Attribute::Native::MethodProvider::Array>.
76 Returns the number of elements in the array.
79 $stuff->options(["foo", "bar", "baz", "boo"]);
81 my $count = $stuff->count_options;
82 print "$count\n"; # prints 4
86 Returns a boolean value indicating whether or not the array has any elements.
88 $stuff->has_no_options ? die "No options!\n" : print "Good boy.\n";
92 This method returns the first item matching item in the array. The matching is
93 done with a subroutine reference you pass to this method. The reference will
94 be called against each element in the array until one matches or all elements
97 my $found = $stuff->find_option( sub { $_[0] =~ /^b/ } );
98 print "$found\n"; # prints "bar"
102 This method returns every element matching a given criteria, just like Perl's
103 core C<grep> function. This method requires a subroutine which implements the
106 my @found = $stuff->filter_options( sub { $_[0] =~ /^b/ } );
107 print "@found\n"; # prints "bar baz boo"
111 This method transforms every element in the array and returns a new array,
112 just like Perl's core C<map> function. This method requires a subroutine which
113 implements the transformation.
115 my @mod_options = $stuff->map_options( sub { $_[0] . "-tag" } );
116 print "@mod_options\n"; # prints "foo-tag bar-tag baz-tag boo-tag"
120 Returns a the array in sorted order.
122 You can provide an optional subroutine reference to sort with (as you can with
123 Perl's core C<sort> function). However, instead of using C<$a> and C<$b>, you
124 will need to use C<$_[0]> and C<$_[1]> instead.
126 # ascending ASCIIbetical
127 my @sorted = $stuff->sort_options();
129 # Descending alphabetical order
130 my @sorted_options = $stuff->sort_options( sub { lc $_[1] cmp lc $_[0] } );
131 print "@sorted_options\n"; # prints "foo boo baz bar"
135 Returns all of the elements of the array.
137 my @option = $stuff->all_options;
138 print "@options\n"; # prints "foo bar baz boo"
142 Joins every element of the array using the separator given as argument, just
143 like Perl's core C<join> function.
145 my $joined = $stuff->join_options( ':' );
146 print "$joined\n"; # prints "foo:bar:baz:boo"
150 Returns an element of the array by its index. You can also use negative index
151 numbers, just as with Perl's core array handling.
153 my $option = $stuff->get_option(1);
154 print "$option\n"; # prints "bar"
158 Returns the first element of the array.
160 my $first = $stuff->first_option;
161 print "$first\n"; # prints "foo"
165 Returns the last element of the array.
167 my $last = $stuff->last_option;
168 print "$last\n"; # prints "boo"
188 =item B<sort_in_place>
190 Sorts the array I<in place>, modifying the value of the attribute.
192 You can provide an optional subroutine reference to sort with (as you can with
193 Perl's core C<sort> function). However, instead of using C<$a> and C<$b>, you
194 will need to use C<$_[0]> and C<$_[1]> instead.
198 This method provides a get/set accessor for the array, based on array indexes.
199 If passed one argument, it returns the value at the specified index. If
200 passed two arguments, it sets the value of the specified index.
210 =item B<method_provider>
212 =item B<has_method_provider>
218 All complex software has bugs lurking in it, and this module is no
219 exception. If you find a bug please either email me, or add the bug
224 Stevan Little E<lt>stevan@iinteractive.comE<gt>
226 =head1 COPYRIGHT AND LICENSE
228 Copyright 2007-2009 by Infinity Interactive, Inc.
230 L<http://www.iinteractive.com>
232 This library is free software; you can redistribute it and/or modify
233 it under the same terms as Perl itself.