NAME

MooseX::Role::Matcher - generic object matching based on attributes and methods

VERSION

version 0.05

SYNOPSIS

  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 });

DESCRIPTION

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.

PARAMETERS

MooseX::Role::Matcher is a parameterized role (see MooseX::Role::Parameterized). The parameters it takes are:

default_match

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').

allow_missing_methods

If set to true, matching against a method that doesn't exist is treated as though matching against undef. Otherwise, the match call dies.

METHODS

first_match

  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.

grep_matches

  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.

any_match

  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.

all_match

  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.

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:

SCALAR

Returns true if the result of the method is equal to (eq) the value of the scalar, otherwise returns false.

REGEXP

Returns true if the result of the method matches the regexp, otherwise returns false.

CODEREF

Calls the coderef with $_ set to the result of the method, returning true if the coderef returns true, and false otherwise.

UNDEF

Returns true if the method returns undef, or if the object doesn't have a method by this name, otherwise returns false.

ARRAYREF

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.

HASHREF

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.

AUTHOR

  Jesse Luehrs <doy at tozt dot net>

COPYRIGHT AND LICENSE

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.

TODO

Better error handling/reporting

SEE ALSO

Moose

MooseX::Role::Parameterized

BUGS

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.

SUPPORT

You can find this documentation for this module with the perldoc command.

    perldoc MooseX::Role::Matcher

You can also look for information at: