use Scalar::Util 'blessed';
use Class::MOP 0.60;
-our $VERSION = '0.73';
+our $VERSION = '0.75';
$VERSION = eval $VERSION;
our $AUTHORITY = 'cpan:STEVAN';
find_meta
does_role
search_class_by_role
+ ensure_all_roles
apply_all_roles
get_all_init_args
get_all_attribute_values
## some utils for the utils ...
-sub find_meta {
- return unless $_[0];
- return Class::MOP::get_metaclass_by_name(blessed($_[0]) || $_[0]);
-}
+sub find_meta { Class::MOP::class_of(@_) }
## the functions ...
return;
}
+# this can possibly behave in unexpected ways because the roles being composed
+# before being applied could differ from call to call; I'm not sure if or how
+# to document this possible quirk.
+sub ensure_all_roles {
+ my $applicant = shift;
+ _apply_all_roles($applicant, sub { !does_role($applicant, $_) }, @_);
+}
+
sub apply_all_roles {
my $applicant = shift;
+ _apply_all_roles($applicant, sub { 1 }, @_);
+}
+
+sub _apply_all_roles {
+ my $applicant = shift;
+ my $role_filter = shift;
unless (@_) {
require Moose;
my $roles = Data::OptList::mkopt( [@_] );
- my $meta = ( blessed $applicant ? $applicant : find_meta($applicant) );
-
- foreach my $role_spec (@$roles) {
- Class::MOP::load_class( $role_spec->[0] );
- }
-
foreach my $role (@$roles) {
- unless (Class::MOP::class_of($role->[0])->isa('Moose::Meta::Role') ) {
+ my $meta = Class::MOP::load_class( $role->[0] );
+ unless ($meta->isa('Moose::Meta::Role') ) {
require Moose;
Moose->throw_error( "You can only consume roles, "
. $role->[0]
}
}
+ @$roles = grep { local $_ = $_->[0]; $role_filter->() } @$roles;
+
+ return unless @$roles;
+
+ my $meta = ( blessed $applicant ? $applicant : find_meta($applicant) );
+
if ( scalar @$roles == 1 ) {
my ( $role, $params ) = @{ $roles->[0] };
my $role_meta = Class::MOP::class_of($role);
return +{
map { $_->name => $_->get_value($instance) }
grep { $_->has_value($instance) }
- $class->compute_all_applicable_attributes
+ $class->get_all_attributes
};
}
map { $_->init_arg => $_->get_value($instance) }
grep { $_->has_value($instance) }
grep { defined($_->init_arg) }
- $class->compute_all_applicable_attributes
+ $class->get_all_attributes
};
}
=item B<find_meta($class_or_obj)>
This method takes a class name or object and attempts to find a
-metaclass for the class, if one exists. It will not create one if it
+metaclass for the class, if one exists. It will B<not> create one if it
does not yet exist.
=item B<does_role($class_or_obj, $role_name)>
followed by an optional hash reference of options (C<exclude> and
C<alias>).
+=item B<ensure_all_roles($applicant, @roles)>
+
+This function is similar to L</apply_all_roles>, but only applies roles that
+C<$applicant> does not already consume.
+
=item B<get_all_attribute_values($meta, $instance)>
Returns a hash reference containing all of the C<$instance>'s