load_plugins withs all the plugins in one go
[gitmo/MooseX-Object-Pluggable.git] / lib / MooseX / Object / Pluggable.pm
index 0927281..ceaacb2 100644 (file)
@@ -5,7 +5,7 @@ use Moose::Role;
 use Class::MOP;
 use Module::Pluggable::Object;
 
-our $VERSION = '0.0006';
+our $VERSION = '0.0007';
 
 =head1 NAME
 
@@ -52,12 +52,12 @@ their consuming classes, so it is important to watch for load order as plugins c
 and will overload each other. You may also add attributes through has.
 
 Please note that when you load at runtime you lose the ability to wrap C<BUILD>
-and roles using C<has> will not go through comile time checks like C<required>
+and roles using C<has> will not go through compile time checks like C<required>
 and <default>.
 
 Even though C<override> will work , I STRONGLY discourage it's use
 and a warning will be thrown if you try to use it.
-This is closely linked to the way multiple roles being applies is handles and is not
+This is closely linked to the way multiple roles being applied is handled and is not
 likely to change. C<override> bevavior is closely linked to inheritance and thus will
 likely not work as you expect it in multiple inheritance situations. Point being,
 save yourself the headache.
@@ -144,6 +144,8 @@ has _plugin_locator => (is => 'rw', required => 1, lazy => 1,
 
 =head1 Public Methods
 
+=head2 load_plugins @plugins
+
 =head2 load_plugin $plugin
 
 Load the apropriate role for C<$plugin> as well as any extensions it provides
@@ -151,31 +153,33 @@ if extensions are enabled.
 
 =cut
 
-sub load_plugin{
-    my ($self, $plugin) = @_;
-    die("You must provide a plugin name") unless $plugin;
+sub load_plugins {
+    my ($self, @plugins) = @_;
+    die("You must provide a plugin name") unless @plugins;
 
     my $loaded = $self->_plugin_loaded;
-    return 1 if exists $loaded->{$plugin};
-
-    my $role = $self->_role_from_plugin($plugin);
 
-    $loaded->{$plugin} = $role      if $self->_load_and_apply_role($role);
-    $self->load_plugin_ext($plugin) if $self->_plugin_ext;
+    my @load = grep { not exists $loaded->{$_} } @plugins;
 
-    return exists $loaded->{$plugin};
-}
+    my @roles = map { $self->_role_from_plugin($_) } @load;
 
-=head2 load_plugins @plugins
+    if ( $self->_load_and_apply_role(@roles) ) {
+        @{ $loaded }{@load} = @roles;
 
-Load all C<@plugins>.
+        if ( $self->_plugin_ext ) {
+            $self->load_plugin_ext($_) for @load;
+        }
 
-=cut
+        return 1;
+    } else {
+        return;
+    }
+}
 
 
-sub load_plugins {
+sub load_plugin {
   my $self = shift;
-  $self->load_plugin($_) for @_;
+  $self->load_plugins(@_);
 }
 
 
@@ -259,7 +263,7 @@ sub _role_from_plugin{
     #Father, please forgive me for I have sinned.
     my @roles = grep{ /${o}$/ } $self->_plugin_locator->plugins;
 
-    die("Unable to locate plugin") unless @roles;
+    croak("Unable to locate plugin '$plugin'") unless @roles;
     return $roles[0] if @roles == 1;
 
     my $i = 0;
@@ -270,7 +274,7 @@ sub _role_from_plugin{
     return shift @roles;
 }
 
-=head2 _load_and_apply_role $role
+=head2 _load_and_apply_role @roles
 
 Require C<$role> if it is not already loaded and apply it. This is
 the meat of this module.
@@ -278,18 +282,19 @@ the meat of this module.
 =cut
 
 sub _load_and_apply_role{
-    my ($self, $role) = @_;
-    die("You must provide a role name") unless $role;
+    my ($self, @roles) = @_;
+    die("You must provide a role name") unless @roles;
 
-    #don't re-require...
-    unless( Class::MOP::is_class_loaded($role) ){
-        eval Class::MOP::load_class($role) || die("Failed to load role: $role");
-    }
+    foreach my $role ( @roles ) {
+        eval { Class::MOP::load_class($role) };
+        confess("Failed to load role: ${role} $@") if $@;
 
-    carp("Using 'override' is strongly discouraged and may not behave ".
-         "as you expect it to. Please use 'around'")
+        carp("Using 'override' is strongly discouraged and may not behave ".
+            "as you expect it to. Please use 'around'")
         if scalar keys %{ $role->meta->get_override_method_modifiers_map };
-    die("Failed to apply plugin: $role") unless $role->meta->apply( $self );
+    }
+
+    Moose::Util::apply_all_roles( $self, @roles );
 
     return 1;
 }