Commit | Line | Data |
9a641848 |
1 | package Moose::Util; |
2 | |
9a641848 |
3 | use strict; |
4 | use warnings; |
5 | |
7125b244 |
6 | use Sub::Exporter; |
d7d8a8c7 |
7 | use Scalar::Util 'blessed'; |
8 | use Carp 'confess'; |
7125b244 |
9 | use Class::MOP (); |
9a641848 |
10 | |
c14746bc |
11 | our $VERSION = '0.03'; |
7125b244 |
12 | our $AUTHORITY = 'cpan:STEVAN'; |
9a641848 |
13 | |
7125b244 |
14 | my @exports = qw[ |
6532ca5a |
15 | find_meta |
16 | does_role |
7125b244 |
17 | search_class_by_role |
d7d8a8c7 |
18 | apply_all_roles |
ab76842e |
19 | get_all_init_args |
20 | get_all_attribute_values |
7125b244 |
21 | ]; |
9a641848 |
22 | |
7125b244 |
23 | Sub::Exporter::setup_exporter({ |
24 | exports => \@exports, |
11065d1f |
25 | groups => { all => \@exports } |
7125b244 |
26 | }); |
27 | |
28 | ## some utils for the utils ... |
29 | |
6532ca5a |
30 | sub find_meta { |
7125b244 |
31 | return unless $_[0]; |
d7d8a8c7 |
32 | return Class::MOP::get_metaclass_by_name(blessed($_[0]) || $_[0]); |
7125b244 |
33 | } |
9a641848 |
34 | |
7125b244 |
35 | ## the functions ... |
adf82331 |
36 | |
7125b244 |
37 | sub does_role { |
38 | my ($class_or_obj, $role) = @_; |
adf82331 |
39 | |
6532ca5a |
40 | my $meta = find_meta($class_or_obj); |
7125b244 |
41 | |
42 | return unless defined $meta; |
adf82331 |
43 | |
7125b244 |
44 | return 1 if $meta->does_role($role); |
45 | return; |
9a641848 |
46 | } |
47 | |
1631b53f |
48 | sub search_class_by_role { |
7125b244 |
49 | my ($class_or_obj, $role_name) = @_; |
50 | |
6532ca5a |
51 | my $meta = find_meta($class_or_obj); |
7125b244 |
52 | |
53 | return unless defined $meta; |
54 | |
55 | foreach my $class ($meta->class_precedence_list) { |
56 | |
6532ca5a |
57 | my $_meta = find_meta($class); |
1631b53f |
58 | |
7125b244 |
59 | next unless defined $_meta; |
60 | |
61 | foreach my $role (@{ $_meta->roles || [] }) { |
1631b53f |
62 | return $class if $role->name eq $role_name; |
63 | } |
64 | } |
65 | |
7125b244 |
66 | return; |
1631b53f |
67 | } |
68 | |
d7d8a8c7 |
69 | sub apply_all_roles { |
70 | my $applicant = shift; |
71 | |
72 | confess "Must specify at least one role to apply to $applicant" unless @_; |
73 | |
74 | my $roles = Data::OptList::mkopt([ @_ ]); |
75 | |
76 | #use Data::Dumper; |
77 | #warn Dumper $roles; |
78 | |
d9bb6c63 |
79 | my $meta = (blessed $applicant ? $applicant : find_meta($applicant)); |
d7d8a8c7 |
80 | |
81 | Class::MOP::load_class($_->[0]) for @$roles; |
82 | |
83 | ($_->[0]->can('meta') && $_->[0]->meta->isa('Moose::Meta::Role')) |
84 | || confess "You can only consume roles, " . $_->[0] . " is not a Moose role" |
85 | foreach @$roles; |
86 | |
87 | if (scalar @$roles == 1) { |
88 | my ($role, $params) = @{$roles->[0]}; |
89 | $role->meta->apply($meta, (defined $params ? %$params : ())); |
90 | } |
91 | else { |
92 | Moose::Meta::Role->combine( |
93 | @$roles |
94 | )->apply($meta); |
95 | } |
96 | } |
97 | |
ab76842e |
98 | # instance deconstruction ... |
99 | |
100 | sub get_all_attribute_values { |
101 | my ($class, $instance) = @_; |
102 | return +{ |
103 | map { $_->name => $_->get_value($instance) } |
104 | grep { $_->has_value($instance) } |
105 | $class->compute_all_applicable_attributes |
106 | }; |
107 | } |
108 | |
109 | sub get_all_init_args { |
110 | my ($class, $instance) = @_; |
111 | return +{ |
112 | map { $_->init_arg => $_->get_value($instance) } |
113 | grep { $_->has_value($instance) } |
114 | grep { defined($_->init_arg) } |
115 | $class->compute_all_applicable_attributes |
116 | }; |
117 | } |
118 | |
d9bb6c63 |
119 | |
9a641848 |
120 | 1; |
121 | |
122 | __END__ |
123 | |
124 | =pod |
125 | |
126 | =head1 NAME |
127 | |
7125b244 |
128 | Moose::Util - Utilities for working with Moose classes |
9a641848 |
129 | |
130 | =head1 SYNOPSIS |
131 | |
6532ca5a |
132 | use Moose::Util qw/find_meta does_role search_class_by_role/; |
133 | |
134 | my $meta = find_meta($object) || die "No metaclass found"; |
9a641848 |
135 | |
adf82331 |
136 | if (does_role($object, $role)) { |
137 | print "The object can do $role!\n"; |
9a641848 |
138 | } |
139 | |
1631b53f |
140 | my $class = search_class_by_role($object, 'FooRole'); |
141 | print "Nearest class with 'FooRole' is $class\n"; |
142 | |
7125b244 |
143 | =head1 DESCRIPTION |
144 | |
145 | This is a set of utility functions to help working with Moose classes. This |
146 | is an experimental module, and it's not 100% clear what purpose it will serve. |
147 | That said, ideas, suggestions and contributions to this collection are most |
148 | welcome. See the L<TODO> section below for a list of ideas for possible |
149 | functions to write. |
150 | |
151 | =head1 EXPORTED FUNCTIONS |
9a641848 |
152 | |
153 | =over 4 |
154 | |
6532ca5a |
155 | =item B<find_meta ($class_or_obj)> |
156 | |
157 | This will attempt to locate a metaclass for the given C<$class_or_obj> |
158 | and return it. |
159 | |
7125b244 |
160 | =item B<does_role ($class_or_obj, $role_name)> |
161 | |
162 | Returns true if C<$class_or_obj> can do the role C<$role_name>. |
163 | |
164 | =item B<search_class_by_role ($class_or_obj, $role_name)> |
165 | |
166 | Returns first class in precedence list that consumed C<$role_name>. |
167 | |
d7d8a8c7 |
168 | =item B<apply_all_roles ($applicant, @roles)> |
169 | |
170 | Given an C<$applicant> (which can somehow be turned into either a |
171 | metaclass or a metarole) and a list of C<@roles> this will do the |
172 | right thing to apply the C<@roles> to the C<$applicant>. This is |
3bb22459 |
173 | actually used internally by both L<Moose> and L<Moose::Role>, and the |
174 | C<@roles> will be pre-processed through L<Data::OptList::mkopt> |
175 | to allow for the additional arguments to be passed. |
d7d8a8c7 |
176 | |
ab76842e |
177 | =item B<get_all_attribute_values($meta, $instance)> |
178 | |
179 | Returns the values of the C<$instance>'s fields keyed by the attribute names. |
180 | |
181 | =item B<get_all_init_args($meta, $instance)> |
182 | |
183 | Returns a hash reference where the keys are all the attributes' C<init_arg>s |
184 | and the values are the instance's fields. Attributes without an C<init_arg> |
185 | will be skipped. |
186 | |
7125b244 |
187 | =back |
9a641848 |
188 | |
7125b244 |
189 | =head1 TODO |
9a641848 |
190 | |
7125b244 |
191 | Here is a list of possible functions to write |
9a641848 |
192 | |
7125b244 |
193 | =over 4 |
1631b53f |
194 | |
7125b244 |
195 | =item discovering original method from modified method |
1631b53f |
196 | |
7125b244 |
197 | =item search for origin class of a method or attribute |
1631b53f |
198 | |
9a641848 |
199 | =back |
200 | |
201 | =head1 BUGS |
202 | |
203 | All complex software has bugs lurking in it, and this module is no |
204 | exception. If you find a bug please either email me, or add the bug |
205 | to cpan-RT. |
206 | |
207 | =head1 AUTHOR |
208 | |
209 | Anders Nor Berle E<lt>debolaz@gmail.comE<gt> |
210 | |
7125b244 |
211 | B<with contributions from:> |
212 | |
213 | Robert (phaylon) Sedlacek |
214 | |
215 | Stevan Little |
216 | |
9a641848 |
217 | =head1 COPYRIGHT AND LICENSE |
218 | |
778db3ac |
219 | Copyright 2007-2008 by Infinity Interactive, Inc. |
9a641848 |
220 | |
221 | L<http://www.iinteractive.com> |
222 | |
223 | This library is free software; you can redistribute it and/or modify |
224 | it under the same terms as Perl itself. |
225 | |
226 | =cut |
227 | |