File Coverage

blib/lib/Starch/Plugin/Bundle.pm
Criterion Covered Total %
statement 52 52 100.0
branch 6 8 75.0
condition n/a
subroutine 13 13 100.0
pod n/a
total 71 73 97.2


line stmt bran cond sub pod time code
1             package Starch::Plugin::Bundle;
2 13     13   7492 use 5.008001;
  13         53  
3 13     13   78 use strictures 2;
  13         109  
  13         513  
4             our $VERSION = '0.12';
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   2958 use Types::Standard -types;
  13         43  
  13         130  
38 13     13   61375 use Types::Common::String -types;
  13         32  
  13         101  
39 13     13   18710 use Starch::Util qw( load_prefixed_module );
  13         35  
  13         738  
40              
41 13     13   83 use Moo::Role;
  13         171  
  13         104  
42 13     13   5811 use namespace::clean;
  13         28  
  13         81  
43              
44             requires( 'bundled_plugins' );
45              
46             sub _roles_for {
47 164     164   386 my ($self, $prefix) = @_;
48              
49 164         453 my $for_role = "Starch::Plugin::For$prefix";
50              
51 164         283 my @roles;
52 164         277 foreach my $role (@{ $self->roles() }) {
  164         2718  
53 213 100       4419 next if !Moo::Role::does_role( $role, $for_role );
54 77         1296 push @roles, $role;
55             }
56              
57 164         4891 return \@roles;
58             }
59              
60             =head1 ATTRIBUTES
61              
62             =head2 plugins
63              
64             This returns the array ref of plugins provided by the
65             C method.
66              
67             =cut
68              
69             has plugins => (
70             is => 'lazy',
71             isa => ArrayRef[ Str ],
72             init_arg => undef,
73             builder => 'bundled_plugins',
74             );
75              
76             =head2 resolved_plugins
77              
78             This returns L with all relative plugin names made
79             absolute.
80              
81             =cut
82              
83             has resolved_plugins => (
84             is => 'lazy',
85             isa => ArrayRef[ NonEmptySimpleStr ],
86             init_arg => undef,
87             );
88             sub _build_resolved_plugins {
89 92     92   930 my ($self) = @_;
90              
91 92         163 my @plugins;
92 92         163 foreach my $plugin (@{ $self->plugins() }) {
  92         1557  
93 111         1911 push @plugins, load_prefixed_module(
94             'Starch::Plugin',
95             $plugin,
96             );
97             }
98              
99 92         2433 return \@plugins;
100             }
101              
102             =head2 roles
103              
104             Returns L with all plugin bundles expanded to
105             their roles.
106              
107             =cut
108              
109             has roles => (
110             is => 'lazy',
111             isa => ArrayRef[ NonEmptySimpleStr ],
112             init_arg => undef,
113             );
114             sub _build_roles {
115 92     92   977 my ($self) = @_;
116              
117 92         164 my @roles;
118              
119 92         159 foreach my $plugin (@{ $self->resolved_plugins() }) {
  92         1560  
120 111 100       3702 if (Moo::Role::does_role( $plugin, 'Starch::Plugin::Bundle')) {
121 22 50       665 die "Plugin bundle $plugin is not a class"
122             if !$plugin->can('new');
123              
124 22         383 my $bundle = $plugin->new();
125 22         4559 push @roles, @{ $bundle->roles() };
  22         409  
126             }
127             else {
128 89 50       2500 die "Plugin $plugin does not look like a role"
129             if $plugin->can('new');
130              
131 89         247 push @roles, $plugin;
132             }
133             }
134              
135 92         3542 return \@roles;
136             }
137              
138             =head2 manager_roles
139              
140             Of the L this returns the ones that consume the
141             L role.
142              
143             =cut
144              
145             has manager_roles => (
146             is => 'lazy',
147             isa => ArrayRef[ NonEmptySimpleStr ],
148             init_arg => undef,
149             );
150             sub _build_manager_roles {
151 70     70   897 my ($self) = @_;
152              
153 70         240 return $self->_roles_for('Manager');
154             }
155              
156             =head2 state_roles
157              
158             Of the L this returns the ones that consume the
159             L role.
160              
161             =cut
162              
163             has state_roles => (
164             is => 'lazy',
165             isa => ArrayRef[ NonEmptySimpleStr ],
166             init_arg => undef,
167             );
168             sub _build_state_roles {
169 24     24   306 my ($self) = @_;
170              
171 24         112 return $self->_roles_for('State');
172             }
173              
174             =head2 store_roles
175              
176             Of the L this returns the ones that consume the
177             L role.
178              
179             =cut
180              
181             has store_roles => (
182             is => 'lazy',
183             isa => ArrayRef[ NonEmptySimpleStr ],
184             init_arg => undef,
185             );
186             sub _build_store_roles {
187 70     70   775 my ($self) = @_;
188              
189 70         184 return $self->_roles_for('Store');
190             }
191              
192             1;
193             __END__