MooseX::Role::Matcher - generic object matching based on attributes and methods
version 0.05
package 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 });
This 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.
MooseX::Role::Matcher is a parameterized role (see MooseX::Role::Parameterized). The parameters it takes are:
Which attribute/method to test against by default, if none are specified explicitly. Setting default_match to 'foo' allows using $obj->match('bar') rather than $obj->match(foo => 'bar').
$obj->match('bar')
$obj->match(foo => 'bar')
If set to true, matching against a method that doesn't exist is treated as though matching against undef. Otherwise, the match call dies.
my $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.
match
my @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.
Person->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.
Person->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.
$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:
Returns true if the result of the method is equal to (eq) the value of the scalar, otherwise returns false.
eq
Returns true if the result of the method matches the regexp, otherwise returns false.
Calls the coderef with $_ set to the result of the method, returning true if the coderef returns true, and false otherwise.
$_
Returns true if the method returns undef, or if the object doesn't have a method by this name, otherwise returns false.
Matches the result of the method against each element in the arrayref as described above, returning true if any of the submatches return true, and false otherwise.
If the method does not return an object which does MooseX::Role::Matcher, returns false. Otherwise, returns the result of calling match on the returned object, with the contents of the hashref as arguments.
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.
Jesse Luehrs <doy at tozt dot net>
This 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.
Better error handling/reporting
Moose
MooseX::Role::Parameterized
No 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.
bug-moosex-role-matcher at rt.cpan.org
You can find this documentation for this module with the perldoc command.
perldoc MooseX::Role::Matcher
You can also look for information at:
AnnoCPAN: Annotated CPAN documentation
http://annocpan.org/dist/MooseX-Role-Matcher
CPAN Ratings
http://cpanratings.perl.org/d/MooseX-Role-Matcher
RT: CPAN's request tracker
http://rt.cpan.org/NoAuth/Bugs.html?Dist=MooseX-Role-Matcher
Search CPAN
http://search.cpan.org/dist/MooseX-Role-Matcher
To install MooseX::Role::Matcher, copy and paste the appropriate command in to your terminal.
cpanm
cpanm MooseX::Role::Matcher
CPAN shell
perl -MCPAN -e shell install MooseX::Role::Matcher
For more information on module installation, please visit the detailed CPAN module installation guide.