From: Shawn M Moore Date: Sat, 17 May 2008 08:56:10 +0000 (+0000) Subject: Provide an "elements" for Collection::List X-Git-Tag: 0.18_01~41 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=6f60a71e0dfe359882422760bec03f9297025aba;p=gitmo%2FMooseX-AttributeHelpers.git Provide an "elements" for Collection::List --- diff --git a/lib/MooseX/AttributeHelpers/MethodProvider/List.pm b/lib/MooseX/AttributeHelpers/MethodProvider/List.pm index 949981a..7e3c6cb 100644 --- a/lib/MooseX/AttributeHelpers/MethodProvider/List.pm +++ b/lib/MooseX/AttributeHelpers/MethodProvider/List.pm @@ -45,6 +45,14 @@ sub grep : method { }; } +sub elements : method { + my ($attr, $reader, $writer) = @_; + return sub { + my ($instance, $f) = @_; + @{$reader->($instance)} + }; +} + 1; __END__ @@ -82,6 +90,8 @@ L. =item B +=item B + =back =head1 BUGS diff --git a/t/005_basic_list.t b/t/005_basic_list.t index bb8d10f..7b85a10 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 => 16; +use Test::More tests => 19; use Test::Exception; BEGIN { @@ -14,17 +14,19 @@ BEGIN { package Stuff; use Moose; - has 'options' => ( + has '_options' => ( metaclass => 'Collection::List', is => 'ro', isa => 'ArrayRef[Int]', + init_arg => 'options', default => sub { [] }, provides => { - 'count' => 'num_options', - 'empty' => 'has_options', - 'map' => 'map_options', - 'grep' => 'filter_options', - 'find' => 'find_option', + 'count' => 'num_options', + 'empty' => 'has_options', + 'map' => 'map_options', + 'grep' => 'filter_options', + 'find' => 'find_option', + 'elements' => 'options', } ); } @@ -33,14 +35,16 @@ my $stuff = Stuff->new(options => [ 1 .. 10 ]); isa_ok($stuff, 'Stuff'); can_ok($stuff, $_) for qw[ + _options num_options has_options map_options filter_options find_option + options ]; -is_deeply($stuff->options, [1 .. 10], '... got options'); +is_deeply($stuff->_options, [1 .. 10], '... got options'); ok($stuff->has_options, '... we have options'); is($stuff->num_options, 10, '... got 2 options'); @@ -59,17 +63,20 @@ is_deeply( is($stuff->find_option(sub { $_[0] % 2 == 0 }), 2, '.. found the right option'); +is_deeply([ $stuff->options ], [1 .. 10], '... got the list of options'); + ## test the meta -my $options = $stuff->meta->get_attribute('options'); +my $options = $stuff->meta->get_attribute('_options'); isa_ok($options, 'MooseX::AttributeHelpers::Collection::List'); is_deeply($options->provides, { - 'map' => 'map_options', - 'grep' => 'filter_options', - 'find' => 'find_option', - 'count' => 'num_options', - 'empty' => 'has_options', + 'map' => 'map_options', + 'grep' => 'filter_options', + 'find' => 'find_option', + 'count' => 'num_options', + 'empty' => 'has_options', + 'elements' => 'options', }, '... got the right provies mapping'); is($options->type_constraint->type_parameter, 'Int', '... got the right container type');