7 use Scalar::Util 'blessed';
11 our $VERSION = '0.02';
12 our $AUTHORITY = 'cpan:STEVAN';
21 Sub::Exporter::setup_exporter({
23 groups => { all => \@exports }
26 ## some utils for the utils ...
30 return Class::MOP::get_metaclass_by_name(blessed($_[0]) || $_[0]);
36 my ($class_or_obj, $role) = @_;
38 my $meta = find_meta($class_or_obj);
40 return unless defined $meta;
42 return 1 if $meta->does_role($role);
46 sub search_class_by_role {
47 my ($class_or_obj, $role_name) = @_;
49 my $meta = find_meta($class_or_obj);
51 return unless defined $meta;
53 foreach my $class ($meta->class_precedence_list) {
55 my $_meta = find_meta($class);
57 next unless defined $_meta;
59 foreach my $role (@{ $_meta->roles || [] }) {
60 return $class if $role->name eq $role_name;
68 my $applicant = shift;
70 confess "Must specify at least one role to apply to $applicant" unless @_;
72 my $roles = Data::OptList::mkopt([ @_ ]);
78 if (blessed $applicant &&
79 ($applicant->isa('Class::MOP::Class') ||
80 $applicant->isa('Moose::Meta::Role')) ){
84 $meta = find_meta($applicant);
87 Class::MOP::load_class($_->[0]) for @$roles;
89 ($_->[0]->can('meta') && $_->[0]->meta->isa('Moose::Meta::Role'))
90 || confess "You can only consume roles, " . $_->[0] . " is not a Moose role"
93 if (scalar @$roles == 1) {
94 my ($role, $params) = @{$roles->[0]};
95 $role->meta->apply($meta, (defined $params ? %$params : ()));
98 Moose::Meta::Role->combine(
112 Moose::Util - Utilities for working with Moose classes
116 use Moose::Util qw/find_meta does_role search_class_by_role/;
118 my $meta = find_meta($object) || die "No metaclass found";
120 if (does_role($object, $role)) {
121 print "The object can do $role!\n";
124 my $class = search_class_by_role($object, 'FooRole');
125 print "Nearest class with 'FooRole' is $class\n";
129 This is a set of utility functions to help working with Moose classes. This
130 is an experimental module, and it's not 100% clear what purpose it will serve.
131 That said, ideas, suggestions and contributions to this collection are most
132 welcome. See the L<TODO> section below for a list of ideas for possible
135 =head1 EXPORTED FUNCTIONS
139 =item B<find_meta ($class_or_obj)>
141 This will attempt to locate a metaclass for the given C<$class_or_obj>
144 =item B<does_role ($class_or_obj, $role_name)>
146 Returns true if C<$class_or_obj> can do the role C<$role_name>.
148 =item B<search_class_by_role ($class_or_obj, $role_name)>
150 Returns first class in precedence list that consumed C<$role_name>.
152 =item B<apply_all_roles ($applicant, @roles)>
154 Given an C<$applicant> (which can somehow be turned into either a
155 metaclass or a metarole) and a list of C<@roles> this will do the
156 right thing to apply the C<@roles> to the C<$applicant>. This is
157 actually used internally by both L<Moose> and L<Moose::Role>.
163 Here is a list of possible functions to write
167 =item discovering original method from modified method
169 =item search for origin class of a method or attribute
175 All complex software has bugs lurking in it, and this module is no
176 exception. If you find a bug please either email me, or add the bug
181 Anders Nor Berle E<lt>debolaz@gmail.comE<gt>
183 B<with contributions from:>
185 Robert (phaylon) Sedlacek
189 =head1 COPYRIGHT AND LICENSE
191 Copyright 2007-2008 by Infinity Interactive, Inc.
193 L<http://www.iinteractive.com>
195 This library is free software; you can redistribute it and/or modify
196 it under the same terms as Perl itself.