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
38 use Moose::AttributeHelpers;
43 isa => 'ArrayRef[Str]',
44 default => sub { [] },
46 all_options => 'elements',
48 filter_options => 'grep',
49 find_option => 'find',
50 first_option => 'first',
51 last_option => 'last',
53 join_options => 'join',
54 count_options => 'count',
55 do_i_have_options => 'empty',
56 sorted_options => 'sort',
65 This module provides an Array attribute which provides a number of
68 =head1 PROVIDED METHODS
70 These methods are implemented in
71 L<Moose::Meta::Attribute::Native::MethodProvider::Array>.
77 Returns the number of elements in the array.
80 $stuff->options(["foo", "bar", "baz", "boo"]);
82 my $count = $stuff->count_options;
83 print "$count\n"; # prints 4
87 If the array is populated, returns true. Otherwise, returns false.
89 $stuff->do_i_have_options ? print "Good boy.\n" : die "No options!\n" ;
93 This method accepts a subroutine reference as its argument. That sub
94 will receive each element of the array in turn. If it returns true for
95 an element, that element will be returned by the C<find> method.
97 my $found = $stuff->find_option( sub { $_[0] =~ /^b/ } );
98 print "$found\n"; # prints "bar"
102 This method accepts a subroutine reference as its argument. This
103 method returns every element for which that subroutine reference
104 returns a true value.
106 my @found = $stuff->filter_options( sub { $_[0] =~ /^b/ } );
107 print "@found\n"; # prints "bar baz boo"
111 This method accepts a subroutine reference as its argument. The
112 subroutine will be executed for each element of the array. It is
113 expected to return a modified version of that element. The return
114 value of the method is a list of the modified options.
116 my @mod_options = $stuff->map_options( sub { $_[0] . "-tag" } );
117 print "@mod_options\n"; # prints "foo-tag bar-tag baz-tag boo-tag"
121 Sorts and returns the elements of the array.
123 You can provide an optional subroutine reference to sort with (as you
124 can with the core C<sort> function). However, instead of using C<$a>
125 and C<$b>, you will need to use C<$_[0]> and C<$_[1]> instead.
127 # ascending ASCIIbetical
128 my @sorted = $stuff->sort_options();
130 # Descending alphabetical order
131 my @sorted_options = $stuff->sort_options( sub { lc $_[1] cmp lc $_[0] } );
132 print "@sorted_options\n"; # prints "foo boo baz bar"
136 Returns all of the elements of the array
138 my @option = $stuff->all_options;
139 print "@options\n"; # prints "foo bar baz boo"
143 Joins every element of the array using the separator given as argument.
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.
152 my $option = $stuff->get_option(1);
153 print "$option\n"; # prints "bar"
157 Returns the first element of the array.
159 my $first = $stuff->first_option;
160 print "$first\n"; # prints "foo"
164 Returns the last element of the array.
166 my $last = $stuff->last_option;
167 print "$last\n"; # prints "boo"
187 =item B<sort_in_place>
189 Sorts the array I<in place>, modifying the value of the attribute.
191 You can provide an optional subroutine reference to sort with (as you
192 can with the core C<sort> function). However, instead of using C<$a>
193 and C<$b>, you will need to use C<$_[0]> and C<$_[1]> instead.
197 If passed one argument, returns the value of the requested element.
198 If passed two arguments, sets the value of the requested element.
208 =item B<method_provider>
210 =item B<has_method_provider>
216 All complex software has bugs lurking in it, and this module is no
217 exception. If you find a bug please either email me, or add the bug
222 Stevan Little E<lt>stevan@iinteractive.comE<gt>
224 =head1 COPYRIGHT AND LICENSE
226 Copyright 2007-2009 by Infinity Interactive, Inc.
228 L<http://www.iinteractive.com>
230 This library is free software; you can redistribute it and/or modify
231 it under the same terms as Perl itself.