some reworkings of the Test::Moose and Moose::Util code
[gitmo/Moose.git] / lib / Moose / Util.pm
1 package Moose::Util;
2
3 use strict;
4 use warnings;
5
6 use Sub::Exporter;
7 use Scalar::Util ();
8 use Class::MOP   ();
9
10 our $VERSION   = '0.01';
11 our $AUTHORITY = 'cpan:STEVAN';
12
13 my @exports = qw[
14     does_role 
15     search_class_by_role   
16 ];
17
18 Sub::Exporter::setup_exporter({
19     exports => \@exports,
20     groups  => { default => \@exports }
21 });
22
23 ## some utils for the utils ...
24
25 sub _get_meta { 
26     return unless $_[0];
27     return Class::MOP::get_metaclass_by_name(ref($_[0]) || $_[0]);
28 }
29
30 ## the functions ...
31
32 sub does_role {
33     my ($class_or_obj, $role) = @_;
34
35     my $meta = _get_meta($class_or_obj);
36     
37     return unless defined $meta;
38
39     return 1 if $meta->does_role($role);
40     return;
41 }
42
43 sub search_class_by_role {
44     my ($class_or_obj, $role_name) = @_;
45     
46     my $meta = _get_meta($class_or_obj);
47
48     return unless defined $meta;
49
50     foreach my $class ($meta->class_precedence_list) {
51         
52         my $_meta = _get_meta($class);        
53
54         next unless defined $_meta;
55
56         foreach my $role (@{ $_meta->roles || [] }) {
57             return $class if $role->name eq $role_name;
58         }
59     }
60
61     return;
62 }
63
64 1;
65
66 __END__
67
68 =pod
69
70 =head1 NAME
71
72 Moose::Util - Utilities for working with Moose classes
73
74 =head1 SYNOPSIS
75
76   use Moose::Util qw/does_role search_class_by_role/;
77
78   if (does_role($object, $role)) {
79     print "The object can do $role!\n";
80   }
81
82   my $class = search_class_by_role($object, 'FooRole');
83   print "Nearest class with 'FooRole' is $class\n";
84
85 =head1 DESCRIPTION
86
87 This is a set of utility functions to help working with Moose classes. This 
88 is an experimental module, and it's not 100% clear what purpose it will serve. 
89 That said, ideas, suggestions and contributions to this collection are most 
90 welcome. See the L<TODO> section below for a list of ideas for possible 
91 functions to write.
92
93 =head1 EXPORTED FUNCTIONS
94
95 =over 4
96
97 =item B<does_role ($class_or_obj, $role_name)>
98
99 Returns true if C<$class_or_obj> can do the role C<$role_name>.
100
101 =item B<search_class_by_role ($class_or_obj, $role_name)>
102
103 Returns first class in precedence list that consumed C<$role_name>.
104
105 =back
106
107 =head1 TODO
108
109 Here is a list of possible functions to write
110
111 =over 4
112
113 =item discovering original method from modified method
114
115 =item search for origin class of a method or attribute
116
117 =back
118
119 =head1 BUGS
120
121 All complex software has bugs lurking in it, and this module is no 
122 exception. If you find a bug please either email me, or add the bug
123 to cpan-RT.
124
125 =head1 AUTHOR
126
127 Anders Nor Berle E<lt>debolaz@gmail.comE<gt>
128
129 B<with contributions from:>
130
131 Robert (phaylon) Sedlacek
132
133 Stevan Little
134
135 =head1 COPYRIGHT AND LICENSE
136
137 Copyright 2007 by Infinity Interactive, Inc.
138
139 L<http://www.iinteractive.com>
140
141 This library is free software; you can redistribute it and/or modify
142 it under the same terms as Perl itself. 
143
144 =cut
145