* s/can_role/does_role/g.
[gitmo/Moose.git] / lib / Moose / Util.pm
CommitLineData
9a641848 1package Moose::Util;
2
3use Exporter qw/import/;
adf82331 4use Scalar::Util;
9a641848 5
6use strict;
7use warnings;
8
9our $VERSION = '0.01';
10
11our $AUTHORITY = 'cpan:BERLE';
12
adf82331 13our @EXPORT_OK = qw/does_role search_class_by_role/;
9a641848 14
adf82331 15sub does_role {
16 my ($class, $role) = @_;
9a641848 17
adf82331 18 return unless defined $class;
19
20 my $meta = Class::MOP::get_metaclass_by_name (ref $class || $class);
21
22 return unless defined $meta;
23
24 return $meta->does_role ($role);
9a641848 25}
26
1631b53f 27sub search_class_by_role {
28 my ($obj, $role_name) = @_;
29
30 for my $class ($obj->meta->class_precedence_list) {
31 for my $role (@{ $class->meta->roles || [] }) {
32 return $class if $role->name eq $role_name;
33 }
34 }
35
36 return undef;
37}
38
9a641848 391;
40
41__END__
42
43=pod
44
45=head1 NAME
46
47Moose::Util - Moose utilities
48
49=head1 SYNOPSIS
50
1631b53f 51 use Moose::Util qw/can_role search_class_by_role/;
9a641848 52
adf82331 53 if (does_role($object, $role)) {
54 print "The object can do $role!\n";
9a641848 55 }
56
1631b53f 57 my $class = search_class_by_role($object, 'FooRole');
58 print "Nearest class with 'FooRole' is $class\n";
59
9a641848 60=head1 FUNCTIONS
61
62=over 4
63
adf82331 64=item does_role
9a641848 65
adf82331 66 does_role($object, $rolename);
9a641848 67
68Returns true if $object can do the role $rolename.
69
1631b53f 70=item search_class_by_role
71
72 my $class = search_class_by_role($object, $rolename);
73
74Returns first class in precedence list that consumed C<$rolename>.
75
9a641848 76=back
77
78=head1 BUGS
79
80All complex software has bugs lurking in it, and this module is no
81exception. If you find a bug please either email me, or add the bug
82to cpan-RT.
83
84=head1 AUTHOR
85
86Anders Nor Berle E<lt>debolaz@gmail.comE<gt>
87
88=head1 COPYRIGHT AND LICENSE
89
90Copyright 2007 by Infinity Interactive, Inc.
91
92L<http://www.iinteractive.com>
93
94This library is free software; you can redistribute it and/or modify
95it under the same terms as Perl itself.
96
97=cut
98