Run/Base/PlugHelpers.pm

filter: Statementstmt 100% 39 / 39 Branchbran 100% 4 / 4 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 10 / 10 Podpod 100% 3 / 3 totaltotal 100% 56 / 56 CC 4 SCAR 4.8
CC
4
Cov
100%
register_pluggable_helper
3.0
create_pluggable_helper_obj
2.0
BEGIN
1.0
CRAP 1.6
« Run/Plugin/CmdLine/Output.pm ↑ Summary Run/Obj/TotObj.pm »
1 package Test::Run::Base::PlugHelpers;
2  
3 18 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 18 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 =head1 NAME
7  
8 Test::Run::Base::PlugHelpers - base class for Test::Run's classes with
9 pluggable helpers.
10  
11 =cut
12  
13 18 ▶use MRO::Compat;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 18 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 18 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 18 extends('Test::Run::Base');
20  
21  
22 18 ▶use Test::Run::Base::Plugger;
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 has '_plug_helpers' => (is => "rw", isa => "HashRef",
25 54 ▶ lazy => 1, default => sub { +{} },
Subroutine __ANON__: called CC=1 SCAR=0.0
26 18 );
27  
28 =head2 $self->register_pluggable_helper( { %args } )
29  
30 Registers a pluggable helper class (commonly done during initialisation).
31 %args contain the following keys:
32  
33 =over 4
34  
35 =item * 'id'
36  
37 The 'id' identifying this class type.
38  
39 =item * 'base'
40  
41 The base class to use as the ultimate primary class of the plugin-based class.
42  
43 =item * 'collect_plugins_method'
44  
45 The method from which to collect the plugins. It should be defined for every
46 base class in the hierarchy of the main class (that instantiates the helpers)
47 and is traversed there.
48  
49 =back
50  
51 =cut
52  
53 sub register_pluggable_helper
54 265 ▶{
Subroutine register_pluggable_helper: called CC=3 SCAR=11.0
Pod documented
55 265 my ($self, $args) = @_;
56  
57 265 my %plug_helper_struct;
58  
59 265 foreach my $key (qw(id base collect_plugins_method))
60 {
61 792 ▶ my $value = $args->{$key}
Branch: 1 branch
BranchTrueFalseTotal
my $value = $args->{$key} or confess(qq["$key" not specified for register_pluggable_helper]) 789 3 792
62 or confess "\"$key\" not specified for register_pluggable_helper";
63  
64 789 $plug_helper_struct{$key} = $value;
65 }
66  
67 262 $self->_plug_helpers()->{$plug_helper_struct{'id'}}
68 = \%plug_helper_struct;
69  
70 262 return;
71 }
72  
73 =head2 $self->calc_helpers_namespace($id)
74  
75 Calc the namespace to put the helper with the ID C<$id> in.
76  
77 =cut
78  
79 sub calc_helpers_namespace
80 203 ▶{
Subroutine calc_helpers_namespace: called CC=1 SCAR=0.0
Pod documented
81 203 my ($self, $id) = @_;
82  
83 return
84 203 $self->helpers_base_namespace() . "::Helpers::" . ucfirst($id)
85 ;
86 }
87  
88 =head2 $self->create_pluggable_helper_obj({ id => $id, args => $args })
89  
90 Instantiates a new pluggable helper object of the ID $id and with $args
91 passed to the constructor.
92  
93 =cut
94  
95 sub create_pluggable_helper_obj
96 203 ▶{
Subroutine create_pluggable_helper_obj: called CC=2 SCAR=6.9
Pod documented
97 203 my ($self, $args) = @_;
98  
99 203 my $id = $args->{id};
100  
101 203 my $plug_struct = $self->_plug_helpers()->{$id};
102 203 ▶ if (!defined($plug_struct))
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $plug_struct) 1 202 203
103 {
104 1 confess "Unknown Pluggable Helper ID \"$id\"!";
105 }
106  
107 my $plugger = Test::Run::Base::Plugger->new(
108 {
109 base => $plug_struct->{base},
110 202 into => $self->calc_helpers_namespace($id),
111 }
112 );
113  
114 $plugger->add_plugins(
115 $self->accum_array(
116 {
117 method => $plug_struct->{collect_plugins_method}
118 }
119 )
120 202 );
121  
122 return
123 $plugger->create_new(
124 $args->{args},
125 202 );
126 }
127  
128 =head2 $self->helpers_base_namespace()
129  
130 B<TO OVERRIDE>: this method determines the base namespace used as the
131 base for the pluggable helpers classes.
132  
133 =cut
134  
135  
136 18 1;
137  
138 __END__
« Run/Plugin/CmdLine/Output.pm ↑ Summary Run/Obj/TotObj.pm »