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 do_i_have_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 If the array is populated, returns true. Otherwise, returns false.
88 $stuff->do_i_have_options ? print "Good boy.\n" : die "No options!\n" ;
92 This method accepts a subroutine reference as its argument. That sub
93 will receive each element of the array in turn. If it returns true for
94 an element, that element will be returned by the C<find> method.
96 my $found = $stuff->find_option( sub { $_[0] =~ /^b/ } );
97 print "$found\n"; # prints "bar"
101 This method accepts a subroutine reference as its argument. This
102 method returns every element for which that subroutine reference
103 returns a true value.
105 my @found = $stuff->filter_options( sub { $_[0] =~ /^b/ } );
106 print "@found\n"; # prints "bar baz boo"
110 This method accepts a subroutine reference as its argument. The
111 subroutine will be executed for each element of the array. It is
112 expected to return a modified version of that element. The return
113 value of the method is a list of the modified options.
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 Sorts and returns the elements of the array.
122 You can provide an optional subroutine reference to sort with (as you
123 can with the core C<sort> function). However, instead of using C<$a>
124 and C<$b>, you 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.
144 my $joined = $stuff->join_options( ':' );
145 print "$joined\n"; # prints "foo:bar:baz:boo"
149 Returns an element of the array by its index.
151 my $option = $stuff->get_option(1);
152 print "$option\n"; # prints "bar"
156 Returns the first element of the array.
158 my $first = $stuff->first_option;
159 print "$first\n"; # prints "foo"
163 Returns the last element of the array.
165 my $last = $stuff->last_option;
166 print "$last\n"; # prints "boo"
186 =item B<sort_in_place>
188 Sorts the array I<in place>, modifying the value of the attribute.
190 You can provide an optional subroutine reference to sort with (as you
191 can with the core C<sort> function). However, instead of using C<$a>
192 and C<$b>, you will need to use C<$_[0]> and C<$_[1]> instead.
196 If passed one argument, returns the value of the requested element.
197 If passed two arguments, sets the value of the requested element.
207 =item B<method_provider>
209 =item B<has_method_provider>
215 All complex software has bugs lurking in it, and this module is no
216 exception. If you find a bug please either email me, or add the bug
221 Stevan Little E<lt>stevan@iinteractive.comE<gt>
223 =head1 COPYRIGHT AND LICENSE
225 Copyright 2007-2009 by Infinity Interactive, Inc.
227 L<http://www.iinteractive.com>
229 This library is free software; you can redistribute it and/or modify
230 it under the same terms as Perl itself.