blib/lib/Dist/Zilla/Role/PluginBundle/Easy.pm

filter: Statementstmt 65.5% 38 / 58 Branchbran 4.5% 1 / 22 Conditioncond 0.0% 0 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 81.8% 9 / 11 Podpod 75.0% 3 / 4 totaltotal 52.0% 51 / 98 CC 15 SCAR 29.1
CC
15
Cov
48%
config_slice
42.0
add_bundle
20.0
add_plugins
16.7
CRAP 18.3
« blib/lib/Dist/Zilla/Plugin/PkgVersion.pm ↑ Summary blib/lib/Dist/Zilla/Role/PrereqScanner.pm »
1 package Dist::Zilla::Role::PluginBundle::Easy 6.040;
2 # ABSTRACT: something that bundles a bunch of plugins easily
3 # This plugin was originally contributed by Christopher J. Madsen
4  
5 11 ▶use Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 11 with 'Dist::Zilla::Role::PluginBundle';
7  
8 11 ▶use Dist::Zilla::Pragmas;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 11 ▶use Module::Runtime 'use_module';
Subroutine BEGIN: called CC=1 SCAR=0.0
11 11 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 #pod =head1 SYNOPSIS
14 #pod
15 #pod package Dist::Zilla::PluginBundle::Example;
16 #pod use Moose;
17 #pod with 'Dist::Zilla::Role::PluginBundle::Easy';
18 #pod
19 #pod sub configure {
20 #pod my $self = shift;
21 #pod
22 #pod $self->add_plugins('VersionFromModule');
23 #pod $self->add_bundle('Basic');
24 #pod }
25 #pod
26 #pod =head1 DESCRIPTION
27 #pod
28 #pod This role builds upon the PluginBundle role, adding methods to take most of the
29 #pod grunt work out of creating a bundle. It supplies the C<bundle_config> method
30 #pod for you. In exchange, you must supply a C<configure> method, which will store
31 #pod the bundle's configuration in the C<plugins> attribute by calling
32 #pod C<add_plugins> and/or C<add_bundle>.
33 #pod
34 #pod =cut
35  
36 11 ▶use MooseX::Types::Moose qw(Str ArrayRef HashRef);
Subroutine BEGIN: called CC=1 SCAR=0.0
37  
38 11 ▶use String::RewritePrefix 0.005
Subroutine BEGIN: called CC=1 SCAR=0.0
39 11 rewrite => {
40 -as => '_plugin_class',
41 prefixes => {
42 '=' => '',
43 '%' => 'Dist::Zilla::Stash::',
44 '' => 'Dist::Zilla::Plugin::',
45 },
46 },
47 rewrite => {
48 -as => '_bundle_class',
49 prefixes => {
50 '' => 'Dist::Zilla::PluginBundle::',
51 '@' => 'Dist::Zilla::PluginBundle::',
52 '=' => ''
53 },
54 11 };
55  
56 11 requires 'configure';
57  
58 #pod =attr name
59 #pod
60 #pod This is the bundle name, taken from the Section passed to
61 #pod C<bundle_config>.
62 #pod
63 #pod =cut
64  
65 11 has name => (
66 is => 'ro',
67 isa => Str,
68 required => 1,
69 );
70  
71 #pod =attr payload
72 #pod
73 #pod This hashref contains the bundle's parameters (if any), taken from the
74 #pod Section passed to C<bundle_config>.
75 #pod
76 #pod =cut
77  
78 11 has payload => (
79 is => 'ro',
80 isa => HashRef,
81 required => 1,
82 );
83  
84 #pod =attr plugins
85 #pod
86 #pod This arrayref contains the configuration that will be returned by
87 #pod C<bundle_config>. You normally modify this by using the
88 #pod C<add_plugins> and C<add_bundle> methods.
89 #pod
90 #pod =cut
91  
92 has plugins => (
93 is => 'ro',
94 isa => ArrayRef,
95 12 ▶ default => sub { [] },
Subroutine __ANON__: called CC=1 SCAR=0.0
96 11 );
97  
98 12 ▶sub bundle_config {
Subroutine bundle_config: called CC=1 SCAR=0.0
Pod undocumented
99 12 my ($class, $section) = @_;
100  
101 12 my $self = $class->new($section);
102  
103 12 $self->configure;
104  
105 12 return $self->plugins->@*;
106 }
107  
108 #pod =method add_plugins
109 #pod
110 #pod $self->add_plugins('Plugin1', [ Plugin2 => \%plugin2config ])
111 #pod
112 #pod Use this method to add plugins to your bundle.
113 #pod
114 #pod It is passed a list of plugin specifiers, which can be one of a few things:
115 #pod
116 #pod =for :list
117 #pod * a plugin moniker (like you might provide in your config file)
118 #pod * an arrayref of: C<< [ $moniker, $plugin_name, \%plugin_config ] >>
119 #pod
120 #pod In the case of an arrayref, both C<$plugin_name> and C<\%plugin_config> are
121 #pod optional.
122 #pod
123 #pod The plugins are added to the config in the order given.
124 #pod
125 #pod =cut
126  
127 12 ▶sub add_plugins {
Subroutine add_plugins: called CC=7 SCAR=28.2
Pod documented
128 12 my ($self, @plugin_specs) = @_;
129  
130 12 my $prefix = $self->name . '/';
131 12 my $plugins = $self->plugins;
132  
133 12 foreach my $this_spec (@plugin_specs) {
134 185 my $moniker;
135 my $name;
136 185 my $payload;
137  
138 185 ▶ if (! ref $this_spec) {
Branch: 3 branches
BranchTrueFalseTotal
if (not ref $this_spec) { } 185 0 185
elsif (@$this_spec == 1) { } 0 0 0
elsif (@$this_spec == 2) { } 0 0 0
139 185 ($moniker, $name, $payload) = ($this_spec, $this_spec, {});
140 } elsif (@$this_spec == 1) {
141 0 ($moniker, $name, $payload) = ($this_spec->[0], $this_spec->[0], {});
142 } elsif (@$this_spec == 2) {
143 0 $moniker = $this_spec->[0];
144 0 ▶ $name = ref $this_spec->[1] ? $moniker : $this_spec->[1];
Branch: 1 branch
BranchTrueFalseTotal
ref $this_spec->[1] ? : 0 0 0
145 0 ▶ $payload = ref $this_spec->[1] ? $this_spec->[1] : {};
Branch: 1 branch
BranchTrueFalseTotal
ref $this_spec->[1] ? : 0 0 0
146 } else {
147 0 ($moniker, $name, $payload) = @$this_spec;
148 }
149  
150 185 push @$plugins, [ $prefix . $name => _plugin_class($moniker) => $payload ];
151 }
152 }
153  
154 #pod =method add_bundle
155 #pod
156 #pod $self->add_bundle(BundleName => \%bundle_config)
157 #pod
158 #pod Use this method to add all the plugins from another bundle to your bundle. If
159 #pod you omit C<%bundle_config>, an empty hashref will be supplied.
160 #pod
161 #pod =cut
162  
163 0 ▶sub add_bundle {
Subroutine add_bundle: not called CC=4 SCAR=30.0
Pod documented
164 0 my ($self, $bundle, $payload) = @_;
165  
166 0 my $package = _bundle_class($bundle);
167 0 ▶ $payload ||= {};
Condition0%0/2
$payload ||= {}
l!l
00
MC/DC0%0/1
$payload ||= {}
$payload
Truth table0%0/2
A
A= $payload
Aresult
11
00
168  
169 &use_module(
170 $package,
171 0 ▶ $payload->{':version'} ? $payload->{':version'} : (),
Branch: 1 branch
BranchTrueFalseTotal
$payload->{':version'} ? : 0 0 0
172 );
173  
174 0 ▶ $bundle = "\@$bundle" unless $bundle =~ /^@/;
Branch: 1 branch
BranchTrueFalseTotal
unless $bundle =~ /^@/u 0 0 0
175  
176 0 push $self->plugins->@*,
177 $package->bundle_config({
178 name => $self->name . '/' . $bundle,
179 package => $package,
180 payload => $payload,
181 });
182 }
183  
184 #pod =method config_slice
185 #pod
186 #pod $hash_ref = $self->config_slice(arg1, { arg2 => 'plugin_arg2' })
187 #pod
188 #pod Use this method to extract parameters from your bundle's C<payload> so
189 #pod that you can pass them to a plugin or subsidiary bundle. It supports
190 #pod easy renaming of parameters, since a plugin may expect a parameter
191 #pod name that's too generic to be suitable for a bundle.
192 #pod
193 #pod Each arg is either a key in C<payload>, or a hashref that maps keys in
194 #pod C<payload> to keys in the hash being constructed. If any specified
195 #pod key does not exist in C<payload>, then it is omitted from the result.
196 #pod
197 #pod =cut
198  
199 0 ▶sub config_slice {
Subroutine config_slice: not called CC=6 SCAR=37.4
Pod documented
200 0 my $self = shift;
201  
202 0 my $payload = $self->payload;
203  
204 0 my %arg;
205  
206 0 foreach my $arg (@_) {
207 0 ▶ if (ref $arg) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $arg) { } 0 0 0
208 0 ▶ while (my ($in, $out) = each %$arg) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$arg) 0 0 0
209 0 ▶ $arg{$out} = $payload->{$in} if exists $payload->{$in};
Branch: 1 branch
BranchTrueFalseTotal
if exists $payload->{$in} 0 0 0
210 }
211 } else {
212 0 ▶ $arg{$arg} = $payload->{$arg} if exists $payload->{$arg};
Branch: 1 branch
BranchTrueFalseTotal
if exists $payload->{$arg} 0 0 0
213 }
214 }
215  
216 0 return \%arg;
217 }
218  
219 11 1;
220  
221 __END__
« blib/lib/Dist/Zilla/Plugin/PkgVersion.pm ↑ Summary blib/lib/Dist/Zilla/Role/PrereqScanner.pm »