X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FMooseX%2FAttributeHelpers%2FMethodProvider%2FList.pm;fp=lib%2FMooseX%2FAttributeHelpers%2FMethodProvider%2FList.pm;h=154d39bc6bdf6c1aadff6138a5beb3dc221f19cb;hb=d57bba34fed2b34a47d455023cb4279d1ba136dd;hp=95648cc7cdcee835812261df9d87c7de476fe456;hpb=f80704da8c3e3295c9f678b96be1972a8afe4915;p=gitmo%2FMooseX-AttributeHelpers.git diff --git a/lib/MooseX/AttributeHelpers/MethodProvider/List.pm b/lib/MooseX/AttributeHelpers/MethodProvider/List.pm index 95648cc..154d39b 100644 --- a/lib/MooseX/AttributeHelpers/MethodProvider/List.pm +++ b/lib/MooseX/AttributeHelpers/MethodProvider/List.pm @@ -54,6 +54,17 @@ sub elements : method { }; } +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 @@ MooseX::AttributeHelpers::MethodProvider::List 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 @@ Returns an element of the list by its index. my $option = $stuff->get_option(1); print "$option\n"; # prints "bar" +=item B +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 Joins every element of the list using the separator given as argument.