File Coverage

blib/lib/Starch/Plugin/Bundle.pm
Criterion Covered Total %
statement 50 50 100.0
branch 6 8 75.0
condition n/a
subroutine 12 12 100.0
pod n/a
total 68 70 97.1


line stmt bran cond sub pod time code
1             package Starch::Plugin::Bundle;
2             our $VERSION = '0.14';
3              
4             =encoding utf8
5              
6             =head1 NAME
7              
8             Starch::Plugin::Bundle - Base role for Starch plugin bundles.
9              
10             =head1 SYNOPSIS
11              
12             # Create a bundle.
13             package MyDevPlugins;
14             use Moo;
15             with 'Starch::Plugin::Bundle';
16             sub bundled_plugins {
17             return ['::Trace', 'MyApp::Starch::CustomPLugin'];
18             }
19              
20             # Use the bundle.
21             my $starch = Starch->new(
22             plugins => ['MyDevPlugin'],
23             ...,
24             );
25              
26             =head1 DESCRIPTION
27              
28             Plugin bundles package together any number of other plugins and plugin
29             bundles. To create a plugin bundle just make a new class that consumes
30             this role and defines the C method. This method
31             should return an array ref of plugin names (absolute or relative).
32              
33             See L for more information.
34              
35             =cut
36              
37 13     13   7595 use Starch::Util qw( load_prefixed_module );
  13         29  
  13         762  
38 13     13   78 use Types::Common::String -types;
  13         30  
  13         95  
39 13     13   18067 use Types::Standard -types;
  13         30  
  13         74  
40              
41 13     13   54269 use Moo::Role;
  13         32  
  13         105  
42 13     13   5753 use strictures 2;
  13         92  
  13         493  
43 13     13   2290 use namespace::clean;
  13         28  
  13         84  
44              
45             requires( 'bundled_plugins' );
46              
47             sub _roles_for {
48 164     164   385 my ($self, $prefix) = @_;
49              
50 164         556 my $for_role = "Starch::Plugin::For$prefix";
51              
52 164         285 my @roles;
53 164         263 foreach my $role (@{ $self->roles() }) {
  164         2603  
54 213 100       4097 next if !Moo::Role::does_role( $role, $for_role );
55 77         1314 push @roles, $role;
56             }
57              
58 164         4858 return \@roles;
59             }
60              
61             =head1 ATTRIBUTES
62              
63             =head2 plugins
64              
65             This returns the array ref of plugins provided by the
66             C method.
67              
68             =cut
69              
70             has plugins => (
71             is => 'lazy',
72             isa => ArrayRef[ Str ],
73             init_arg => undef,
74             builder => 'bundled_plugins',
75             );
76              
77             =head2 resolved_plugins
78              
79             This returns L with all relative plugin names made
80             absolute.
81              
82             =cut
83              
84             has resolved_plugins => (
85             is => 'lazy',
86             isa => ArrayRef[ NonEmptySimpleStr ],
87             init_arg => undef,
88             );
89             sub _build_resolved_plugins {
90 92     92   904 my ($self) = @_;
91              
92 92         144 my @plugins;
93 92         147 foreach my $plugin (@{ $self->plugins() }) {
  92         1452  
94 111         1797 push @plugins, load_prefixed_module(
95             'Starch::Plugin',
96             $plugin,
97             );
98             }
99              
100 92         2472 return \@plugins;
101             }
102              
103             =head2 roles
104              
105             Returns L with all plugin bundles expanded to
106             their roles.
107              
108             =cut
109              
110             has roles => (
111             is => 'lazy',
112             isa => ArrayRef[ NonEmptySimpleStr ],
113             init_arg => undef,
114             );
115             sub _build_roles {
116 92     92   939 my ($self) = @_;
117              
118 92         160 my @roles;
119              
120 92         154 foreach my $plugin (@{ $self->resolved_plugins() }) {
  92         1515  
121 111 100       3502 if (Moo::Role::does_role( $plugin, 'Starch::Plugin::Bundle')) {
122 22 50       633 die "Plugin bundle $plugin is not a class"
123             if !$plugin->can('new');
124              
125 22         366 my $bundle = $plugin->new();
126 22         4012 push @roles, @{ $bundle->roles() };
  22         385  
127             }
128             else {
129 89 50       2436 die "Plugin $plugin does not look like a role"
130             if $plugin->can('new');
131              
132 89         304 push @roles, $plugin;
133             }
134             }
135              
136 92         3436 return \@roles;
137             }
138              
139             =head2 manager_roles
140              
141             Of the L this returns the ones that consume the
142             L role.
143              
144             =cut
145              
146             has manager_roles => (
147             is => 'lazy',
148             isa => ArrayRef[ NonEmptySimpleStr ],
149             init_arg => undef,
150             );
151             sub _build_manager_roles {
152 70     70   835 my ($self) = @_;
153              
154 70         243 return $self->_roles_for('Manager');
155             }
156              
157             =head2 state_roles
158              
159             Of the L this returns the ones that consume the
160             L role.
161              
162             =cut
163              
164             has state_roles => (
165             is => 'lazy',
166             isa => ArrayRef[ NonEmptySimpleStr ],
167             init_arg => undef,
168             );
169             sub _build_state_roles {
170 24     24   290 my ($self) = @_;
171              
172 24         70 return $self->_roles_for('State');
173             }
174              
175             =head2 store_roles
176              
177             Of the L this returns the ones that consume the
178             L role.
179              
180             =cut
181              
182             has store_roles => (
183             is => 'lazy',
184             isa => ArrayRef[ NonEmptySimpleStr ],
185             init_arg => undef,
186             );
187             sub _build_store_roles {
188 70     70   769 my ($self) = @_;
189              
190 70         192 return $self->_roles_for('Store');
191             }
192              
193             1;
194             __END__