From: Shawn M Moore Date: Thu, 22 May 2008 23:28:43 +0000 (+0000) Subject: Add a "join" provided method to lists (I do have a use case, shaddap!) X-Git-Tag: 0.18_01~33 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=654096bc0e06f57608c77f16829ee7563704f31a;p=gitmo%2FMooseX-AttributeHelpers.git Add a "join" provided method to lists (I do have a use case, shaddap!) --- diff --git a/lib/MooseX/AttributeHelpers/MethodProvider/List.pm b/lib/MooseX/AttributeHelpers/MethodProvider/List.pm index 82a1274..c3ffff3 100644 --- a/lib/MooseX/AttributeHelpers/MethodProvider/List.pm +++ b/lib/MooseX/AttributeHelpers/MethodProvider/List.pm @@ -53,6 +53,14 @@ sub elements : method { }; } +sub join : method { + my ($attr, $reader, $writer) = @_; + return sub { + my ($instance, $separator) = @_; + join $separator, @{$reader->($instance)} + }; +} + 1; __END__ @@ -92,6 +100,8 @@ L. =item B +=item B + =back =head1 BUGS diff --git a/t/005_basic_list.t b/t/005_basic_list.t index 7b85a10..5757f1c 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 => 19; +use Test::More tests => 21; use Test::Exception; BEGIN { @@ -27,6 +27,7 @@ BEGIN { 'grep' => 'filter_options', 'find' => 'find_option', 'elements' => 'options', + 'join' => 'join_options', } ); } @@ -42,6 +43,7 @@ can_ok($stuff, $_) for qw[ filter_options find_option options + join_options ]; is_deeply($stuff->_options, [1 .. 10], '... got options'); @@ -65,6 +67,8 @@ is($stuff->find_option(sub { $_[0] % 2 == 0 }), 2, '.. found the right option'); is_deeply([ $stuff->options ], [1 .. 10], '... got the list of options'); +is($stuff->join_options(':'), '1:2:3:4:5:6:7:8:9:10', '... joined the list of options by :'); + ## test the meta my $options = $stuff->meta->get_attribute('_options'); @@ -77,6 +81,7 @@ is_deeply($options->provides, { 'count' => 'num_options', 'empty' => 'has_options', 'elements' => 'options', + 'join' => 'join_options', }, '... got the right provies mapping'); is($options->type_constraint->type_parameter, 'Int', '... got the right container type');