From: Jesse Luehrs Date: Tue, 18 Aug 2009 05:47:58 +0000 (-0500) Subject: rename/add a few methods on the native Array trait X-Git-Tag: 0.89_02~60 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=391c761c658ecef029f5326f0782962237e150af;p=gitmo%2FMoose.git rename/add a few methods on the native Array trait first -> head, find -> first, and add tail for symmetry with head. --- diff --git a/lib/Moose/Meta/Attribute/Native/MethodProvider/Array.pm b/lib/Moose/Meta/Attribute/Native/MethodProvider/Array.pm index 54800c9..d98202b 100644 --- a/lib/Moose/Meta/Attribute/Native/MethodProvider/Array.pm +++ b/lib/Moose/Meta/Attribute/Native/MethodProvider/Array.pm @@ -19,7 +19,7 @@ sub empty : method { }; } -sub find : method { +sub first : method { my ( $attr, $reader, $writer ) = @_; return sub { my ( $instance, $predicate ) = @_; @@ -78,13 +78,21 @@ sub join : method { }; } -sub first : method { +sub head : method { my ( $attr, $reader, $writer ) = @_; return sub { $reader->( $_[0] )->[0]; }; } +sub tail : method { + my ( $attr, $reader, $writer ) = @_; + return sub { + my $arr = $reader->( $_[0] ); + return @{ $arr }[1..$#{ $arr }]; + }; +} + sub last : method { my ( $attr, $reader, $writer ) = @_; return sub { diff --git a/lib/Moose/Meta/Attribute/Native/Trait/Array.pm b/lib/Moose/Meta/Attribute/Native/Trait/Array.pm index bef24c5..68f6486 100644 --- a/lib/Moose/Meta/Attribute/Native/Trait/Array.pm +++ b/lib/Moose/Meta/Attribute/Native/Trait/Array.pm @@ -42,17 +42,18 @@ Moose::Meta::Attribute::Native::Trait::Array isa => 'ArrayRef[Str]', default => sub { [] }, handles => { - all_options => 'elements', - map_options => 'map', - filter_options => 'grep', - find_option => 'find', - first_option => 'first', - last_option => 'last', - get_option => 'get', - join_options => 'join', - count_options => 'count', - has_no_options => 'empty', - sorted_options => 'sort', + 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', } ); @@ -114,7 +115,7 @@ numbers, just as with Perl's core array handling. These methods are all equivalent to the Perl core functions of the same name. -=item B +=item B 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 @@ -189,13 +190,20 @@ Inserts a new element into the array at the given index. Empties the entire array, like C<@array = ()>. -=item B +=item B Returns the first element of the array. my $first = $stuff->first_option; print "$first\n"; # prints "foo" +=item B + +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 Returns the last element of the array. diff --git a/t/070_native_traits/205_trait_list.t b/t/070_native_traits/205_trait_list.t index 989ef15..74d01ab 100644 --- a/t/070_native_traits/205_trait_list.t +++ b/t/070_native_traits/205_trait_list.t @@ -3,7 +3,7 @@ use strict; use warnings; -use Test::More tests => 33; +use Test::More tests => 34; use Test::Exception; use Test::Moose 'does_ok'; @@ -21,19 +21,20 @@ my $up; init_arg => 'options', default => sub { [] }, handles => { - 'num_options' => 'count', - 'has_no_options' => 'empty', - 'map_options', => 'map', - 'filter_options' => 'grep', - 'find_option' => 'find', - 'options' => 'elements', - 'join_options' => 'join', - 'get_option_at' => 'get', - 'get_first_option' => 'first', - 'get_last_option' => 'last', - 'sorted_options' => 'sort', - 'less_than_five' => [ grep => [ $less = sub { $_ < 5 } ] ], - 'up_by_one' => [ map => [ $up = sub { $_ + 1 } ] ], + 'num_options' => 'count', + 'has_no_options' => 'empty', + 'map_options', => 'map', + 'filter_options' => 'grep', + 'find_option' => 'first', + 'options' => 'elements', + 'join_options' => 'join', + 'get_option_at' => 'get', + 'get_first_option' => 'head', + 'all_but_first_option' => 'tail', + 'get_last_option' => 'last', + 'sorted_options' => 'sort', + 'less_than_five' => [ grep => [ $less = sub { $_ < 5 } ] ], + 'up_by_one' => [ map => [ $up = sub { $_ + 1 } ] ], 'dashify' => [ join => ['-'] ], 'descending' => [ sort => [ $sort = sub { $_[1] <=> $_[0] } ] ], }, @@ -62,7 +63,8 @@ is_deeply( $stuff->_options, [ 1 .. 10 ], '... got options' ); ok( !$stuff->has_no_options, '... we have options' ); is( $stuff->num_options, 10, '... got 2 options' ); cmp_ok( $stuff->get_option_at(0), '==', 1, '... get option 0' ); -cmp_ok( $stuff->get_first_option, '==', 1, '... get first' ); +cmp_ok( $stuff->get_first_option, '==', 1, '... get head' ); +is_deeply( [ $stuff->all_but_first_option ], [ 2 .. 10 ], '... get tail' ); cmp_ok( $stuff->get_last_option, '==', 10, '... get last' ); is_deeply( @@ -116,21 +118,22 @@ does_ok( $options, 'Moose::Meta::Attribute::Native::Trait::Array' ); is_deeply( $options->handles, { - 'num_options' => 'count', - 'has_no_options' => 'empty', - 'map_options', => 'map', - 'filter_options' => 'grep', - 'find_option' => 'find', - 'options' => 'elements', - 'join_options' => 'join', - 'get_option_at' => 'get', - 'get_first_option' => 'first', - 'get_last_option' => 'last', - 'sorted_options' => 'sort', - 'less_than_five' => [ grep => [$less] ], - 'up_by_one' => [ map => [$up] ], - 'dashify' => [ join => ['-'] ], - 'descending' => [ sort => [$sort] ], + 'num_options' => 'count', + 'has_no_options' => 'empty', + 'map_options', => 'map', + 'filter_options' => 'grep', + 'find_option' => 'first', + 'options' => 'elements', + 'join_options' => 'join', + 'get_option_at' => 'get', + 'get_first_option' => 'head', + 'all_but_first_option' => 'tail', + 'get_last_option' => 'last', + 'sorted_options' => 'sort', + 'less_than_five' => [ grep => [$less] ], + 'up_by_one' => [ map => [$up] ], + 'dashify' => [ join => ['-'] ], + 'descending' => [ sort => [$sort] ], }, '... got the right handles mapping' );