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([ @_ ]);
77 my $meta = (blessed $applicant ? $applicant : find_meta($applicant));
79 Class::MOP::load_class($_->[0]) for @$roles;
81 ($_->[0]->can('meta') && $_->[0]->meta->isa('Moose::Meta::Role'))
82 || confess "You can only consume roles, " . $_->[0] . " is not a Moose role"
85 if (scalar @$roles == 1) {
86 my ($role, $params) = @{$roles->[0]};
87 $role->meta->apply($meta, (defined $params ? %$params : ()));
90 Moose::Meta::Role->combine(
105 Moose::Util - Utilities for working with Moose classes
109 use Moose::Util qw/find_meta does_role search_class_by_role/;
111 my $meta = find_meta($object) || die "No metaclass found";
113 if (does_role($object, $role)) {
114 print "The object can do $role!\n";
117 my $class = search_class_by_role($object, 'FooRole');
118 print "Nearest class with 'FooRole' is $class\n";
122 This is a set of utility functions to help working with Moose classes. This
123 is an experimental module, and it's not 100% clear what purpose it will serve.
124 That said, ideas, suggestions and contributions to this collection are most
125 welcome. See the L<TODO> section below for a list of ideas for possible
128 =head1 EXPORTED FUNCTIONS
132 =item B<find_meta ($class_or_obj)>
134 This will attempt to locate a metaclass for the given C<$class_or_obj>
137 =item B<does_role ($class_or_obj, $role_name)>
139 Returns true if C<$class_or_obj> can do the role C<$role_name>.
141 =item B<search_class_by_role ($class_or_obj, $role_name)>
143 Returns first class in precedence list that consumed C<$role_name>.
145 =item B<apply_all_roles ($applicant, @roles)>
147 Given an C<$applicant> (which can somehow be turned into either a
148 metaclass or a metarole) and a list of C<@roles> this will do the
149 right thing to apply the C<@roles> to the C<$applicant>. This is
150 actually used internally by both L<Moose> and L<Moose::Role>, and the
151 C<@roles> will be pre-processed through L<Data::OptList::mkopt>
152 to allow for the additional arguments to be passed.
158 Here is a list of possible functions to write
162 =item discovering original method from modified method
164 =item search for origin class of a method or attribute
170 All complex software has bugs lurking in it, and this module is no
171 exception. If you find a bug please either email me, or add the bug
176 Anders Nor Berle E<lt>debolaz@gmail.comE<gt>
178 B<with contributions from:>
180 Robert (phaylon) Sedlacek
184 =head1 COPYRIGHT AND LICENSE
186 Copyright 2007-2008 by Infinity Interactive, Inc.
188 L<http://www.iinteractive.com>
190 This library is free software; you can redistribute it and/or modify
191 it under the same terms as Perl itself.