[Moose-commits] r7337 - in
MooseX-AttributeHelpers/branches/og-contains+docs:
lib/MooseX/AttributeHelpers/MethodProvider t
oliver at code2.0beta.co.uk
oliver at code2.0beta.co.uk
Sun Jan 18 03:44:21 GMT 2009
Author: oliver
Date: 2009-01-17 19:44:18 -0800 (Sat, 17 Jan 2009)
New Revision: 7337
Modified:
MooseX-AttributeHelpers/branches/og-contains+docs/lib/MooseX/AttributeHelpers/MethodProvider/List.pm
MooseX-AttributeHelpers/branches/og-contains+docs/t/005_basic_list.t
Log:
add contains method to MethodProvider/List
Modified: MooseX-AttributeHelpers/branches/og-contains+docs/lib/MooseX/AttributeHelpers/MethodProvider/List.pm
===================================================================
--- MooseX-AttributeHelpers/branches/og-contains+docs/lib/MooseX/AttributeHelpers/MethodProvider/List.pm 2009-01-18 02:57:30 UTC (rev 7336)
+++ MooseX-AttributeHelpers/branches/og-contains+docs/lib/MooseX/AttributeHelpers/MethodProvider/List.pm 2009-01-18 03:44:18 UTC (rev 7337)
@@ -54,6 +54,17 @@
};
}
+sub contains : method {
+ my ($attr, $reader, $writer) = @_;
+ return sub {
+ my ($instance, $item) = @_;
+ return scalar (defined $item ?
+ CORE::grep {defined && $_ eq $item} @{$reader->($instance)}
+ : CORE::grep {!defined} @{$reader->($instance)}
+ );
+ };
+}
+
sub join : method {
my ($attr, $reader, $writer) = @_;
return sub {
@@ -106,15 +117,16 @@
default => sub { [] },
auto_deref => 1,
provides => {
- map => 'map_options',
- grep => 'filter_options',
- find => 'find_option',
- first => 'first_option',
- last => 'last_option',
- get => 'get_option',
- join => 'join_options',
- count => 'count_options',
- empty => 'do_i_have_options',
+ map => 'map_options',
+ grep => 'filter_options',
+ find => 'find_option',
+ first => 'first_option',
+ last => 'last_option',
+ get => 'get_option',
+ join => 'join_options',
+ count => 'count_options',
+ empty => 'do_i_have_options',
+ contains => 'options_contains',
}
);
@@ -180,6 +192,16 @@
my $option = $stuff->get_option(1);
print "$option\n"; # prints "bar"
+=item B<contains>
+Returns a true value if the list contains the passed value, otherwise returns
+a false value. The return value is actually the number of times the passed
+value appears in the list (0, or more).
+
+ my $found = $stuff->contains( 'apple' );
+ print "Found apple in the list\n" if $found;
+
+Note that this works even when looking for the undefined value.
+
=item B<join>
Joins every element of the list using the separator given as argument.
Modified: MooseX-AttributeHelpers/branches/og-contains+docs/t/005_basic_list.t
===================================================================
--- MooseX-AttributeHelpers/branches/og-contains+docs/t/005_basic_list.t 2009-01-18 02:57:30 UTC (rev 7336)
+++ MooseX-AttributeHelpers/branches/og-contains+docs/t/005_basic_list.t 2009-01-18 03:44:18 UTC (rev 7337)
@@ -7,7 +7,7 @@
use Test::Exception;
BEGIN {
- plan tests => 29;
+ plan tests => 36;
}
BEGIN {
@@ -35,6 +35,7 @@
'get' => 'get_option_at',
'first' => 'get_first_option',
'last' => 'get_last_option',
+ 'contains' => 'options_contains',
},
curries => {
'grep' => {less_than_five => [ sub { $_ < 5 } ]},
@@ -72,6 +73,7 @@
options
join_options
get_option_at
+ options_contains
];
is_deeply($stuff->_options, [1 .. 10], '... got options');
@@ -100,6 +102,16 @@
is($stuff->join_options(':'), '1:2:3:4:5:6:7:8:9:10', '... joined the list of options by :');
+is($stuff->options_contains(),0,'... does not contain undef');
+is($stuff->options_contains(5),1,'... contains "5"');
+is($stuff->options_contains(11),0,'... does not contain "11"');
+push @{$stuff->_options}, undef;
+is($stuff->options_contains(undef),1,'... does contain undef');
+push @{$stuff->_options}, 5;
+is($stuff->options_contains(5),2,'... contains returns count');
+splice @{$stuff->_options}, -2;
+is_deeply($stuff->_options, [1 .. 10], '... reset list for regression');
+
# test the currying
is_deeply([ $stuff->less_than_five() ], [1 .. 4]);
@@ -131,7 +143,8 @@
'join' => 'join_options',
'get' => 'get_option_at',
'first' => 'get_first_option',
- 'last' => 'get_last_option'
+ 'last' => 'get_last_option',
+ 'contains' => 'options_contains',
}, '... got the right provies mapping');
is($options->type_constraint->type_parameter, 'Int', '... got the right container type');
More information about the Moose-commits
mailing list