|
NAMEMooseX::Role::Matcher - generic object matching based on attributes and methodsVERSIONversion 0.05SYNOPSISpackage Person; use Moose; with 'MooseX::Role::Matcher' => { default_match => 'name' }; has name => (is => 'ro', isa => 'Str'); has age => (is => 'ro', isa => 'Num'); has phone => (is => 'ro', isa => 'Str'); package main; my @people = ( Person->new(name => 'James', age => 22, phone => '555-1914'), Person->new(name => 'Jesse', age => 22, phone => '555-6287'), Person->new(name => 'Eric', age => 21, phone => '555-7634'), ); # is James 22? $people[0]->match(age => 22); # which people are not 22? my @not_twenty_two = Person->grep_matches([@people], '!age' => 22); # do any of the 22-year-olds have a phone number ending in 4? Person->any_match([@people], age => 22, phone => qr/4$/); # does everyone's name start with either J or E? Person->all_match([@people], name => [qr/^J/, qr/^E/]); # find the first person whose name is 4 characters long (using the # default_match of name) my $four = Person->first_match([@people], sub { length == 4 }); DESCRIPTIONThis role adds flexible matching and searching capabilities to your Moose class. It provides a match method, which tests attributes and methods of your object against strings, regexes, or coderefs, and also provides several class methods for using match on lists of objects.PARAMETERSMooseX::Role::Matcher is a parameterized role (see MooseX::Role::Parameterized). The parameters it takes are:
METHODSfirst_matchmy $four = Person->first_match([@people], sub { length == 4 }); Class method which takes an arrayref of objects in the class that consumed this role, and calls "match" on each object in the arrayref, passing it the remaining arguments, and returns the first object for which match returns true. grep_matchesmy @not_twenty_two = Person->grep_matches([@people], '!age' => 22); Class method which takes an arrayref of objects in the class that consumed this role, and calls "match" on each object in the arrayref, passing it the remaining arguments, and returns the each object for which match returns true. any_matchPerson->any_match([@people], age => 22, number => qr/4$/); Class method which takes an arrayref of objects in the class that consumed this role, and calls "match" on each object in the arrayref, passing it the remaining arguments, and returns true if any "match" calls return true, otherwise returns false. all_matchPerson->all_match([@people], name => [qr/^J/, qr/^E/]); Class method which takes an arrayref of objects in the class that consumed this role, and calls "match" on each object in the arrayref, passing it the remaining arguments, and returns false if any "match" calls return false, otherwise returns true. match$person->match(age => 22); This method provides the majority of the functionality of this role. It accepts a hash of arguments, with keys being the methods (usually attributes) of the object to be tested, and values being things to test against them. Possible types of values are:
Method names can also be given with a leading '!', which inverts that test. The first key can be omitted from the argument list if it is the method name passed to the default_match parameter when composing this role. AUTHORJesse Luehrs <doy at tozt dot net> COPYRIGHT AND LICENSEThis software is copyright (c) 2008-2009 by Jesse Luehrs.This is free software; you can redistribute it and/or modify it under the same terms as perl itself. TODOBetter error handling/reportingSEE ALSOMooseMooseX::Role::Parameterized BUGSNo known bugs.Please report any bugs through RT: email "bug-moosex-role-matcher at rt.cpan.org", or browse to <http://rt.cpan.org/NoAuth/ReportBug.html?Queue=MooseX-Role-Matcher>. SUPPORTYou can find this documentation for this module with the perldoc command.perldoc MooseX::Role::Matcher You can also look for information at:
Visit the GSP FreeBSD Man Page Interface. |