From: Cory G Watson Date: Fri, 27 Jun 2008 15:09:05 +0000 (+0000) Subject: Move get to List and add first/last. X-Git-Tag: 0.16~38 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=b77cfe61151b25aecf807cbd755fdd7b44d78d5f;p=gitmo%2FMooseX-AttributeHelpers.git Move get to List and add first/last. --- diff --git a/ChangeLog b/ChangeLog index 58e731d..0bf5439 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,6 +1,8 @@ Revision history for Perl extension MooseX-AttributeHelpers 0.12 + - Move get from Array to List (gphat) + - Add first and last to List (gphat) - Doc fixes (gphat) 0.11 Thurs. Jun 26, 2008 - add the ability to curry method providers (thanks to jasonmay) diff --git a/lib/MooseX/AttributeHelpers/MethodProvider/List.pm b/lib/MooseX/AttributeHelpers/MethodProvider/List.pm index 10744d1..b848db5 100644 --- a/lib/MooseX/AttributeHelpers/MethodProvider/List.pm +++ b/lib/MooseX/AttributeHelpers/MethodProvider/List.pm @@ -61,6 +61,27 @@ sub join : method { }; } +sub get : method { + my ($attr, $reader, $writer) = @_; + return sub { + $reader->($_[0])->[$_[1]] + }; +} + +sub first : method { + my ($attr, $reader, $writer) = @_; + return sub { + $reader->($_[0])->[0] + }; +} + +sub last : method { + my ($attr, $reader, $writer) = @_; + return sub { + $reader->($_[0])->[-1] + }; +} + 1; __END__ @@ -102,6 +123,12 @@ L. =item B +=item B + +=item B + +=item B + =back =head1 BUGS diff --git a/t/005_basic_list.t b/t/005_basic_list.t index 8d16e9a..016c83b 100644 --- a/t/005_basic_list.t +++ b/t/005_basic_list.t @@ -3,7 +3,7 @@ use strict; use warnings; -use Test::More tests => 25; +use Test::More tests => 29; use Test::Exception; use DateTime; use DateTime::Format::Strptime; @@ -30,6 +30,9 @@ BEGIN { 'find' => 'find_option', 'elements' => 'options', 'join' => 'join_options', + 'get' => 'get_option_at', + 'first' => 'get_first_option', + 'last' => 'get_last_option', }, curries => { 'grep' => {less_than_five => [ sub { $_ < 5 } ]}, @@ -65,12 +68,16 @@ can_ok($stuff, $_) for qw[ find_option options join_options + get_option_at ]; is_deeply($stuff->_options, [1 .. 10], '... got options'); ok($stuff->has_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_last_option, '==', 10, '... get first'); is_deeply( [ $stuff->filter_options(sub { $_[0] % 2 == 0 }) ], @@ -121,6 +128,9 @@ is_deeply($options->provides, { 'empty' => 'has_options', 'elements' => 'options', 'join' => 'join_options', + 'get' => 'get_option_at', + 'first' => 'get_first_option', + 'last' => 'get_last_option' }, '... got the right provies mapping'); is($options->type_constraint->type_parameter, 'Int', '... got the right container type');