lib/App/Followme/ConfiguredObject.pm

filter: Statementstmt 93.0% 80 / 86 Branchbran 81.2% 26 / 32 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 92.8% 13 / 14 Podpod 33.3% 3 / 9 totaltotal 86.5% 122 / 141 CC 22 SCAR 15.3
CC
22
Cov
90%
initialize
7.0
add_subpackages
6.2
filter_configuration
6.1
CRAP 4.6
« lib/App/Followme/FIO.pm ↑ Summary lib/App/Followme/Template.pm »
1 package App::Followme::ConfiguredObject;
2  
3 25 ▶use 5.008005;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 25 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 25 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 25 ▶use Cwd;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 25 our $VERSION = "2.03";
10  
11 #----------------------------------------------------------------------
12 # Create object that returns files in a directory tree
13  
14 188 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
15 188 my ($pkg, %configuration) = @_;
16  
17 188 my $self = {};
18 188 my $cycle = {};
19 188 initialize($pkg, $self, $cycle, %configuration);
20  
21 188 return $self;
22 }
23  
24 #----------------------------------------------------------------------
25 # Read the default parameter values
26  
27 940 ▶sub parameters {
Subroutine parameters: called CC=1 SCAR=0.0
Pod documented
28 940 my ($pkg) = @_;
29  
30 return (
31 940 quick_update => 0,
32 case_sensitivity => 0,
33 top_directory => getcwd(),
34 base_directory => getcwd(),
35 );
36 }
37  
38 #----------------------------------------------------------------------
39 # Update an object's fields from the configuration hash
40  
41 684 ▶sub add_configurations {
Subroutine add_configurations: called CC=2 SCAR=6.9
Pod undocumented
42 684 my ($self, $pkg, %configuration) = @_;
43  
44 684 foreach my $field ($self->all_fields(\%configuration)) {
45 600 $self->{$field} = $configuration{$field};
46 }
47  
48 684 return;
49 }
50  
51 #----------------------------------------------------------------------
52 # Create subobjects for any parameter ending in _pkg
53  
54 684 ▶sub add_subpackages {
Subroutine add_subpackages: called CC=6 SCAR=18.2
Pod undocumented
55 684 my ($self, %configuration) = @_;
56  
57 684 foreach my $field ($self->all_fields($self)) {
58 2253 my $subpkg = $self->{$field};
59 2253 ▶ next unless $field =~ s/_pkg$//;
Branch: 1 branch
BranchTrueFalseTotal
unless $field =~ s/_pkg$// 2119 134 2253
60  
61 134 ▶ eval "require $subpkg" or die "Module not found: $subpkg\n";
Branch: 1 branch
BranchTrueFalseTotal
eval "require $subpkg" or die "Module not found: $subpkg\n" 134 0 134
62  
63 134 ▶ if ($subpkg->isa('App::Followme::ConfiguredObject')) {
Branch: 2 branches
BranchTrueFalseTotal
if ($subpkg->isa('App::Followme::ConfiguredObject')) { } 127 7 134
elsif ($subpkg->can('new')) { } 7 0 7
64 127 $self->{$field} = $subpkg->new(%configuration);
65 } elsif ($subpkg->can('new')) {
66 7 $self->{$field} = $subpkg->new();
67 } else {
68 0 $self->{$field} = $subpkg;
69 }
70 }
71  
72 684 return;
73 }
74  
75 #----------------------------------------------------------------------
76 # Get the configuration fields that apply to this package
77  
78 1368 ▶sub all_fields {
Subroutine all_fields: called CC=5 SCAR=16.1
Pod undocumented
79 1368 my ($self, $configuration) = @_;
80  
81 1368 my @fields = ();
82 1368 ▶ if (defined $configuration) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $configuration) 1368 0 1368
83 1368 my $pkg = ref $self;
84 1368 my %parameters = $pkg->parameters();
85  
86 1368 foreach my $field (keys %$configuration) {
87 8562 ▶ next if ref $configuration->{$field};
Branch: 1 branch
BranchTrueFalseTotal
if ref $configuration->{$field} 354 8208 8562
88 8208 ▶ push(@fields, $field) if exists $parameters{$field};;
Branch: 1 branch
BranchTrueFalseTotal
if exists $parameters{$field} 2853 5355 8208
89 }
90 }
91  
92 1368 return @fields;
93 }
94  
95 #----------------------------------------------------------------------
96 # Remove prefixes from config fields that match the package name
97  
98 684 ▶sub filter_configuration {
Subroutine filter_configuration: called CC=6 SCAR=18.1
Pod undocumented
99 684 my ($pkg, %configuration) = @_;
100  
101 684 my %parameters = $pkg->parameters();
102 684 my @config_keys = keys %configuration;
103  
104 684 for my $field (@config_keys) {
105 2424 my @configuration_fields = split('::', $field);
106 2424 my $config_field = pop(@configuration_fields);
107 2424 ▶ next unless @configuration_fields;
Branch: 1 branch
BranchTrueFalseTotal
unless @configuration_fields 2420 4 2424
108  
109 4 my @pkg_fields = split('::', $pkg);
110  
111 4 my $match = 1;
112 4 ▶ while (@configuration_fields) {
Branch: 1 branch
BranchTrueFalseTotal
if (@configuration_fields) 7 4 11
113 7 my $subfield = pop(@configuration_fields);
114 7 my $pkg_field = pop(@pkg_fields);
115  
116 7 ▶ if ($pkg_field ne $subfield) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pkg_field ne $subfield) 0 7 7
117 0 $match = 0;
118 0 last;
119 }
120 }
121  
122 4 ▶ if ($match) {
Branch: 1 branch
BranchTrueFalseTotal
if ($match) 4 0 4
123 4 $configuration{$config_field} = $configuration{$field};
124 4 delete $configuration{$field};
125 }
126 }
127  
128 684 return %configuration;
129 }
130  
131 #----------------------------------------------------------------------
132 # Extract the package field name from the configuration field name
133  
134 0 ▶sub get_pkg_field {
Subroutine get_pkg_field: not called CC=1 SCAR=6.9
Pod undocumented
135 0 my ($self, $field) = @_;
136 0 my @configuration_fields = split('::', $field);
137 0 return pop(@configuration_fields);
138 }
139  
140 #----------------------------------------------------------------------
141 # Initialize the object by populating its hash
142  
143 684 ▶sub initialize {
Subroutine initialize: called CC=7 SCAR=19.5
Pod undocumented
144 684 my ($pkg, $self, $cycle, %configuration) = @_;
145 684 ▶ %configuration = () unless %configuration;
Branch: 1 branch
BranchTrueFalseTotal
unless %configuration 63 621 684
146 684 ▶ return if $cycle->{$pkg};
Branch: 1 branch
BranchTrueFalseTotal
if $cycle->{$pkg} 0 684 684
147  
148 25 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
149 684 %configuration = $pkg->filter_configuration(%configuration);
150 684 initialize($_, $self, $cycle, %configuration) foreach @{"${pkg}::ISA"};
151 684 $cycle->{$pkg} = 1;
152  
153 684 my %parameters = $pkg->parameters();
154 684 ▶ while (my ($key, $value) = each(%parameters)) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %parameters) 2796 684 3480
155 2796 ▶ $self->{$key} = $value if length $value;
Branch: 1 branch
BranchTrueFalseTotal
if length $value 2112 684 2796
156 }
157  
158 684 $self = bless($self, $pkg);
159  
160 684 $self->add_configurations($pkg, %configuration);
161 684 $self->add_subpackages(%configuration);
162  
163 684 ▶ $self->setup(%configuration) if defined &{"${pkg}::setup"};
Branch: 1 branch
BranchTrueFalseTotal
if defined &{"${pkg}::setup";} 460 224 684
164 684 return;
165 }
166  
167 #----------------------------------------------------------------------
168 # Set up object fields (stub)
169  
170 188 ▶sub setup {
Subroutine setup: called CC=1 SCAR=0.0
Pod documented
171 188 my ($self) = @_;
172 188 return;
173 }
174  
175 25 1;
176 __END__
« lib/App/Followme/FIO.pm ↑ Summary lib/App/Followme/Template.pm »