MVP/Section.pm

filter: Statementstmt 97.0% 65 / 67 Branchbran 69.2% 18 / 26 Conditioncond 87.5% 7 / 8 MC/DCmcdc 60.0% 3 / 5 Subroutinesub 100% 16 / 16 Podpod 80.0% 4 / 5 totaltotal 88.9% 113 / 127 CC 17 SCAR 10.9
CC
17
Cov
89%
add_value
6.2
__ANON__
3.5
sequence
3.0
CRAP 3.0
« MVP/Reader.pm ↑ Summary MVP/Assembler.pm »
1 package Config::MVP::Section 2.200015;
2 # ABSTRACT: one section of an MVP configuration sequence
3  
4 4 ▶use Moose 0.91;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 4 ▶use Class::Load 0.17 ();
Subroutine BEGIN: called CC=1 SCAR=0.0
7 4 ▶use Config::MVP::Error;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 4 ▶use Params::SomeUtil ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 #pod =head1 DESCRIPTION
11 #pod
12 #pod For the most part, you can just consult L<Config::MVP> to understand what this
13 #pod class is and how it's used.
14 #pod
15 #pod =attr name
16 #pod
17 #pod This is the section's name. It's a string, and it must be provided.
18 #pod
19 #pod =cut
20  
21 4 has name => (
22 is => 'ro',
23 isa => 'Str',
24 required => 1
25 );
26  
27 #pod =attr package
28 #pod
29 #pod This is the (Perl) package with which the section is associated. It is
30 #pod optional. When the section is instantiated, it will ensure that this package
31 #pod is loaded.
32 #pod
33 #pod =cut
34  
35 4 has package => (
36 is => 'ro',
37 isa => 'Str', # should be class-like string, but can't be ClassName
38 required => 0,
39 predicate => 'has_package',
40 );
41  
42 #pod =attr multivalue_args
43 #pod
44 #pod This attribute is an arrayref of value names that should be considered
45 #pod multivalue properties in the section. When added to the section, they will
46 #pod always be wrapped in an arrayref, and they may be added to the section more
47 #pod than once.
48 #pod
49 #pod If this attribute is not given during construction, it will default to the
50 #pod result of calling section's package's C<mvp_multivalue_args> method. If the
51 #pod section has no associated package or if the package doesn't provide that
52 #pod method, it default to an empty arrayref.
53 #pod
54 #pod =cut
55  
56 has multivalue_args => (
57 is => 'ro',
58 isa => 'ArrayRef',
59 lazy => 1,
60 31 ▶ default => sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
61 31 my ($self) = @_;
62  
63 31 ▶ return []
Branch: 1 branch
BranchTrueFalseTotal
unless $self->has_package and $self->package->can("mvp_multivalue_args") 21 10 31
Condition100%3/3
$self->has_package and $self->package->can("mvp_multivalue_args")
!ll&&!rl&&r
12010
MC/DC100%2/2
$self->has_package and $self->package->can("mvp_multivalue_args")
$self->has_package $self->package->can("mvp_multivalue_args")
Truth table100%3/3
A and B
A= $self->has_packageB= $self->package->can("mvp_multivalue_args")
ABresult
0X0
100
111
64 unless $self->has_package and $self->package->can('mvp_multivalue_args');
65  
66 10 return [ $self->package->mvp_multivalue_args ];
67 },
68 4 );
69  
70 #pod =attr aliases
71 #pod
72 #pod This attribute is a hashref of name remappings. For example, if it contains
73 #pod this hashref:
74 #pod
75 #pod {
76 #pod file => 'files',
77 #pod path => 'files',
78 #pod }
79 #pod
80 #pod Then attempting to set either the "file" or "path" setting for the section
81 #pod would actually set the "files" setting.
82 #pod
83 #pod If this attribute is not given during construction, it will default to the
84 #pod result of calling section's package's C<mvp_aliases> method. If the
85 #pod section has no associated package or if the package doesn't provide that
86 #pod method, it default to an empty hashref.
87 #pod
88 #pod =cut
89  
90 has aliases => (
91 is => 'ro',
92 isa => 'HashRef',
93 lazy => 1,
94 31 ▶ default => sub {
Subroutine __ANON__: called CC=3 SCAR=12.5
95 31 my ($self) = @_;
96  
97 31 ▶ return {} unless $self->has_package and $self->package->can('mvp_aliases');
Branch: 1 branch
BranchTrueFalseTotal
unless $self->has_package and $self->package->can("mvp_aliases") 31 0 31
Condition66%2/3
$self->has_package and $self->package->can("mvp_aliases")
!ll&&!rl&&r
1300
MC/DC0%0/2
$self->has_package and $self->package->can("mvp_aliases")
$self->has_package $self->package->can("mvp_aliases")
Truth table66%2/3
A and B
A= $self->has_packageB= $self->package->can("mvp_aliases")
ABresult
0X0
100
111
98  
99 0 return $self->package->mvp_aliases;
100 },
101 4 );
102  
103 #pod =attr payload
104 #pod
105 #pod This is the storage into which properties are set. It is a hashref of names
106 #pod and values. You should probably not alter the contents of the payload, and
107 #pod should read its contents only.
108 #pod
109 #pod =cut
110  
111 has payload => (
112 is => 'ro',
113 isa => 'HashRef',
114 init_arg => undef,
115 33 ▶ default => sub { {} },
Subroutine __ANON__: called CC=1 SCAR=0.0
116 4 );
117  
118 #pod =attr is_finalized
119 #pod
120 #pod This attribute is true if the section has been marked finalized, which will
121 #pod prevent any new values from being added to it. It can be set with the
122 #pod C<finalize> method.
123 #pod
124 #pod =cut
125  
126 4 has is_finalized => (
127 is => 'ro',
128 isa => 'Bool',
129 traits => [ 'Bool' ],
130 init_arg => undef,
131 default => 0,
132 handles => { finalize => 'set' },
133 );
134  
135 30 ▶before finalize => sub {
Subroutine __ANON__: called CC=2 SCAR=7.2
136 30 my ($self) = @_;
137  
138 30 ▶ Config::MVP::Error->throw("can't finalize unsequenced Config::MVP::Section")
Branch: 1 branch
BranchTrueFalseTotal
unless $self->sequence 0 30 30
139 unless $self->sequence;
140 4 };
141  
142 #pod =attr sequence
143 #pod
144 #pod This attributes points to the sequence into which the section has been
145 #pod assembled. It may be unset if the section has been created but not yet placed
146 #pod in a sequence.
147 #pod
148 #pod =cut
149  
150 4 has sequence => (
151 is => 'ro',
152 isa => 'Config::MVP::Sequence',
153 weak_ref => 1,
154 predicate => '_sequence_has_been_set',
155 reader => '_sequence',
156 writer => '__set_sequence',
157 clearer => '_clear_sequence',
158 );
159  
160 31 ▶sub _set_sequence {
Subroutine _set_sequence: called CC=2 SCAR=7.1
161 31 my ($self, $seq) = @_;
162  
163 31 ▶ Config::MVP::Error->throw("Config::MVP::Section cannot be resequenced")
Branch: 1 branch
BranchTrueFalseTotal
if $self->sequence 0 31 31
164 if $self->sequence;
165  
166 31 $self->__set_sequence($seq);
167 }
168  
169 65 ▶sub sequence {
Subroutine sequence: called CC=3 SCAR=11.0
Pod documented
170 65 my ($self) = @_;
171 65 ▶ return undef unless $self->_sequence_has_been_set;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_sequence_has_been_set 31 34 65
172 34 my $seq = $self->_sequence;
173  
174 34 ▶ Config::MVP::Error->throw("can't access section's destroyed sequence")
Branch: 1 branch
BranchTrueFalseTotal
unless defined $seq 0 34 34
175 unless defined $seq;
176  
177 34 return $seq;
178 }
179  
180 #pod =method add_value
181 #pod
182 #pod $section->add_value( $name => $value );
183 #pod
184 #pod This method sets the value for the named property to the given value. If the
185 #pod property is a multivalue property, the new value will be pushed onto the end of
186 #pod an arrayref that will store all values for that property.
187 #pod
188 #pod Attempting to add a value for a non-multivalue property whose value was already
189 #pod added will result in an exception.
190 #pod
191 #pod =cut
192  
193 58 ▶sub add_value {
Subroutine add_value: called CC=6 SCAR=18.2
Pod documented
194 58 my ($self, $name, $value) = @_;
195  
196 58 ▶ confess "can't add values to finalized section " . $self->name
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_finalized 0 58 58
197 if $self->is_finalized;
198  
199 58 my $alias = $self->aliases->{ $name };
200 58 ▶ $name = $alias if defined $alias;
Branch: 1 branch
BranchTrueFalseTotal
if defined $alias 0 58 58
201  
202 58 my $mva = $self->multivalue_args;
203  
204 58 ▶ if (grep { $_ eq $name } @$mva) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep {$_ eq $name;} @$mva) 22 36 58
205 22 ▶ my $array = $self->payload->{$name} ||= [];
Condition100%2/2
$self->payload->{$name} ||= []
l!l
139
MC/DC100%1/1
$self->payload->{$name} ||= []
$self->payload->{$name}
Truth table100%2/2
A
A= $self->payload->{$name}
Aresult
11
00
206 22 push @$array, $value;
207 22 return;
208 }
209  
210 36 ▶ if (exists $self->payload->{$name}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->payload->{$name}) 0 36 36
211 0 Carp::croak "multiple values given for property $name in section "
212 . $self->name;
213 }
214  
215 36 $self->payload->{$name} = $value;
216 }
217  
218 #pod =method load_package
219 #pod
220 #pod $section->load_package($package, $section_name);
221 #pod
222 #pod This method is used to ensure that the given C<$package> is loaded, and is
223 #pod called whenever a section with a package is created. By default, it delegates
224 #pod to L<Class::Load>. If the package can't be found, it calls the
225 #pod L<missing_package> method. Errors in compilation are not suppressed.
226 #pod
227 #pod =cut
228  
229 32 ▶sub load_package {
Subroutine load_package: called CC=2 SCAR=6.9
Pod documented
230 32 my ($self, $package, $section_name) = @_;
231  
232 32 ▶ Class::Load::load_optional_class($package)
Branch: 1 branch
BranchTrueFalseTotal
Class::Load::load_optional_class($package) or $self->missing_package($package, $section_name) 30 1 31
233 or $self->missing_package($package, $section_name);
234 }
235  
236 #pod =method missing_package
237 #pod
238 #pod $section->missing_package($package, $section_name);
239 #pod
240 #pod This method is called when C<load_package> encounters a package that is not
241 #pod installed. By default, it throws an exception.
242 #pod
243 #pod =cut
244  
245 1 ▶sub missing_package {
Subroutine missing_package: called CC=1 SCAR=0.0
Pod documented
246 1 my ($self, $package, $section_name) = @_ ;
247  
248 1 my $class = Moose::Meta::Class->create_anon_class(
249 superclasses => [ 'Config::MVP::Error' ],
250 cached => 1,
251 attributes => [
252 Moose::Meta::Attribute->new(package => (
253 is => 'ro',
254 required => 1,
255 )),
256 Moose::Meta::Attribute->new(section_name => (
257 is => 'ro',
258 required => 1,
259 )),
260 ],
261 );
262  
263 1 $class->name->throw({
264 ident => 'package not installed',
265 message => "$package (for section $section_name) does not appear to be installed",
266 package => $package,
267 section_name => $section_name,
268 });
269 }
270  
271 33 ▶sub _BUILD_package_settings {
Subroutine _BUILD_package_settings: called CC=3 SCAR=11.0
272 33 my ($self) = @_;
273  
274 33 ▶ return unless defined (my $pkg = $self->package);
Branch: 1 branch
BranchTrueFalseTotal
unless defined(my $pkg = $self->package) 1 32 33
275  
276 32 ▶ confess "illegal package name $pkg" unless Params::SomeUtil::_CLASS($pkg);
Branch: 1 branch
BranchTrueFalseTotal
unless &Params::SomeUtil::_CLASS($pkg) 0 32 32
277  
278 32 $self->load_package($pkg, $self->name);
279  
280 # We call these accessors for lazy attrs to ensure they're initialized from
281 # defaults if needed. Crash early! -- rjbs, 2009-08-09
282 30 $self->multivalue_args;
283 30 $self->aliases;
284 }
285  
286 33 ▶sub BUILD {
Subroutine BUILD: called CC=1 SCAR=0.0
Pod undocumented
287 33 my ($self) = @_;
288 33 $self->_BUILD_package_settings;
289 }
290  
291 4 ▶no Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
292 4 1;
293  
294 __END__
« MVP/Reader.pm ↑ Summary MVP/Assembler.pm »