File Coverage

blib/lib/Resque/Pluggable.pm
Criterion Covered Total %
statement 23 42 54.7
branch 0 6 0.0
condition n/a
subroutine 7 10 70.0
pod 1 1 100.0
total 31 59 52.5


line stmt bran cond sub pod time code
1             package Resque::Pluggable;
2             # ABSTRACT: Role to load Resque plugin's and and apply roles.
3             $Resque::Pluggable::VERSION = '0.42';
4 9     9   5725 use Moose::Role;
  9         20  
  9         82  
5              
6 9     9   47232 use namespace::autoclean;
  9         27  
  9         114  
7 9     9   778 use Class::Load qw(load_class);
  9         19  
  9         591  
8 9     9   67 use Moose::Util qw(apply_all_roles);
  9         18  
  9         107  
9              
10             has plugins => (
11             is => 'rw',
12             isa => 'ArrayRef',
13             lazy => 1,
14             default => sub {[]}
15             );
16              
17             has worker_class => (
18             is => 'ro',
19             lazy => 1,
20             default => sub { $_[0]->_class_with_roles('Resque::Worker' => 'worker') }
21             );
22              
23             has job_class => (
24             is => 'ro',
25             lazy => 1,
26             default => sub { $_[0]->_class_with_roles('Resque::Job' => 'job') }
27             );
28              
29       2 1   sub BUILD {} # Ensure it exists!
30             after BUILD => sub {
31             my $self = shift;
32             $self->_load_plugins;
33             $self->meta->make_mutable;
34             if ( my @r = $self->_roles_for('resque') ) {
35             apply_all_roles( $self, @r );
36             }
37             $self->meta->make_immutable;
38             };
39              
40             # Build anon class based on the given one with optional roles applied
41             sub _class_with_roles {
42 0     0   0 my ( $self, $class, $kind ) = @_;
43 0         0 my @roles = $self->_roles_for($kind);
44 0         0 load_class($class);
45 0 0       0 return $class unless @roles;
46              
47 0         0 my $meta = Moose::Meta::Class->create_anon_class(
48             superclasses => [$class],
49             roles => [@roles]
50             );
51              
52             # ensure anon doesn't goes out of scope!
53             # http://www.nntp.perl.org/group/perl.moose/2008/03/msg132.html
54 0     0   0 $meta->add_method( meta => sub {$meta} );
  0         0  
55              
56 0         0 $meta->make_immutable;
57 0         0 return $meta->name;
58             }
59              
60             sub _load_plugins {
61 2     2   3 my $self = shift;
62              
63 2         4 my @plugins;
64 2         4 for my $name ( @{$self->plugins} ) {
  2         47  
65 0         0 $name = _expand_plugin($name);
66 0         0 load_class($name);
67 0         0 my $plugin = $name->new;
68 0         0 push @plugins, $plugin;
69             }
70 2         38 $self->plugins(\@plugins);
71             }
72              
73             sub _expand_plugin {
74 0     0   0 my $name = pop;
75 0 0       0 $name = $name =~ /^\+(.+)$/ ? $1 : "Resque::Plugin::$name";
76 0         0 return $name;
77             }
78              
79             sub _roles_for {
80 2     2   5 my ( $self, $kind ) = @_;
81 2         5 my $method_name = "${kind}_roles";
82              
83 2         5 my @roles;
84 2         4 for my $plugin ( @{$self->plugins} ) {
  2         65  
85 0 0       0 push @roles, map {$self->_expand_plugin($_)} @{$plugin->$method_name}
  0         0  
  0         0  
86             if $plugin->can($method_name);
87             }
88 2         11 return @roles;
89             }
90              
91             1;
92              
93             __END__
94              
95             =pod
96              
97             =encoding UTF-8
98              
99             =head1 NAME
100              
101             Resque::Pluggable - Role to load Resque plugin's and and apply roles.
102              
103             =head1 VERSION
104              
105             version 0.42
106              
107             =head1 ATTRIBUTES
108              
109             =head2 plugins
110              
111             List of plugins to be loaded into this L<Resque> system. See L<Resque::Plugin>.
112              
113             =head2 worker_class
114              
115             Worker class to be used for worker attribute.
116             This is L<Resque::Worker> with all plugin/roles applied to it.
117              
118             =head2 job_class
119              
120             Job class to be used by L<Resque::new_job>.
121             This is L<Resque::Job> with all plugin/roles applied to it.
122              
123             =head1 METHODS
124              
125             =head2 BUILD
126              
127             Apply pluggable roles after BUILD.
128              
129             =head1 AUTHOR
130              
131             Diego Kuperman <diego@freekeylabs.com>
132              
133             =head1 COPYRIGHT AND LICENSE
134              
135             This software is copyright (c) 2021 by Diego Kuperman.
136              
137             This is free software; you can redistribute it and/or modify it under
138             the same terms as the Perl 5 programming language system itself.
139              
140             =cut