MVP/Sequence.pm

filter: Statementstmt 93.1% 54 / 58 Branchbran 50.0% 10 / 20 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 92.8% 13 / 14 Podpod 83.3% 5 / 6 totaltotal 83.6% 82 / 98 CC 11 SCAR 10.7
CC
11
Cov
82%
_set_assembler
6.0
add_section
5.1
assembler
3.2
CRAP 2.9
« MVP/Assembler.pm ↑ Summary MVP/Reader/Findable/ByExtension.pm »
1 package Config::MVP::Sequence 2.200015;
2 # ABSTRACT: an ordered set of named configuration sections
3  
4 4 ▶use Moose 0.91;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 #pod =head1 DESCRIPTION
7 #pod
8 #pod A Config::MVP::Sequence is an ordered set of configuration sections, each of
9 #pod which has a name unique within the sequence.
10 #pod
11 #pod For the most part, you can just consult L<Config::MVP> to understand what this
12 #pod class is and how it's used.
13 #pod
14 #pod =cut
15  
16 4 ▶use Tie::IxHash;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 4 ▶use Config::MVP::Error;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 4 ▶use Config::MVP::Section;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 4 ▶use Moose::Util::TypeConstraints ();
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 # This is a private attribute and should not be documented for futzing-with,
22 # most likely. -- rjbs, 2009-08-09
23 has sections => (
24 isa => 'HashRef[Config::MVP::Section]',
25 reader => '_sections',
26 init_arg => undef,
27 8 ▶ default => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
28 8 tie my %section, 'Tie::IxHash';
29 8 return \%section;
30 },
31 4 );
32  
33 4 has assembler => (
34 is => 'ro',
35 isa => Moose::Util::TypeConstraints::class_type('Config::MVP::Assembler'),
36 weak_ref => 1,
37 predicate => '_assembler_has_been_set',
38 reader => '_assembler',
39 writer => '__set_assembler',
40 );
41  
42 0 ▶sub _set_assembler {
Subroutine _set_assembler: not called CC=2 SCAR=17.9
43 0 my ($self, $assembler) = @_;
44  
45 0 ▶ Config::MVP::Error->throw("can't alter Config::MVP::Sequence's assembler")
Branch: 1 branch
BranchTrueFalseTotal
if $self->assembler 0 0 0
46 if $self->assembler;
47  
48 0 $self->__set_assembler($assembler);
49 }
50  
51 4 ▶sub assembler {
Subroutine assembler: called CC=3 SCAR=11.8
Pod undocumented
52 4 my ($self) = @_;
53 4 ▶ return undef unless $self->_assembler_has_been_set;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_assembler_has_been_set 0 4 4
54 4 my $assembler = $self->_assembler;
55  
56 4 ▶ unless (defined $assembler) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $assembler) 0 4 4
57 0 Config::MVP::Error->throw("can't access sequences's destroyed assembler")
58 }
59  
60 4 return $assembler;
61 }
62  
63 #pod =attr is_finalized
64 #pod
65 #pod This attribute is true if the sequence has been marked finalized, which will
66 #pod prevent any changes (via methods like C<add_section> or C<delete_section>). It
67 #pod can be set with the C<finalize> method.
68 #pod
69 #pod =cut
70  
71 4 has is_finalized => (
72 is => 'ro',
73 isa => 'Bool',
74 traits => [ 'Bool' ],
75 init_arg => undef,
76 default => 0,
77 handles => { finalize => 'set' },
78 );
79  
80 #pod =method add_section
81 #pod
82 #pod $sequence->add_section($section);
83 #pod
84 #pod This method adds the given section to the end of the sequence. If the sequence
85 #pod already contains a section with the same name as the new section, an exception
86 #pod will be raised.
87 #pod
88 #pod =cut
89  
90 31 ▶sub add_section {
Subroutine add_section: called CC=5 SCAR=16.4
Pod documented
91 31 my ($self, $section) = @_;
92  
93 31 ▶ Config::MVP::Error->throw("can't add sections to finalized sequence")
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_finalized 0 31 31
94 if $self->is_finalized;
95  
96 31 my $name = $section->name;
97 31 ▶ confess "already have a section named $name" if $self->_sections->{ $name };
Branch: 1 branch
BranchTrueFalseTotal
if $self->_sections->{$name} 0 31 31
98  
99 31 $section->_set_sequence($self);
100  
101 31 ▶ if (my @names = $self->section_names) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@names) = $self->section_names) 23 8 31
102 23 my $last_section = $self->section_named( $names[-1] );
103 23 ▶ $last_section->finalize unless $last_section->is_finalized;
Branch: 1 branch
BranchTrueFalseTotal
unless $last_section->is_finalized 0 23 23
104 }
105  
106 31 $self->_sections->{ $name } = $section;
107 }
108  
109 #pod =method delete_section
110 #pod
111 #pod my $deleted_section = $sequence->delete_section( $name );
112 #pod
113 #pod This method removes a section from the sequence and returns the removed
114 #pod section. If no section existed, the method returns false.
115 #pod
116 #pod =cut
117  
118 4 ▶sub delete_section {
Subroutine delete_section: called CC=3 SCAR=11.2
Pod documented
119 4 my ($self, $name) = @_;
120  
121 4 ▶ Config::MVP::Error->throw("can't delete sections from finalized sequence")
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_finalized 0 4 4
122 if $self->is_finalized;
123  
124 4 my $sections = $self->_sections;
125  
126 4 ▶ return unless exists $sections->{ $name };
Branch: 1 branch
BranchTrueFalseTotal
unless exists $sections->{$name} 0 4 4
127  
128 4 $sections->{ $name }->_clear_sequence;
129  
130 4 return delete $sections->{ $name };
131 }
132  
133 #pod =method section_named
134 #pod
135 #pod my $section = $sequence->section_named( $name );
136 #pod
137 #pod This method returns the section with the given name, if one exists in the
138 #pod sequence. If no such section exists, the method returns false.
139 #pod
140 #pod =cut
141  
142 23 ▶sub section_named {
Subroutine section_named: called CC=2 SCAR=7.0
Pod documented
143 23 my ($self, $name) = @_;
144 23 my $sections = $self->_sections;
145  
146 23 ▶ return unless exists $sections->{ $name };
Branch: 1 branch
BranchTrueFalseTotal
unless exists $sections->{$name} 0 23 23
147 23 return $sections->{ $name };
148 }
149  
150 #pod =method section_names
151 #pod
152 #pod my @names = $sequence->section_names;
153 #pod
154 #pod This method returns a list of the names of the sections, in order.
155 #pod
156 #pod =cut
157  
158 31 ▶sub section_names {
Subroutine section_names: called CC=1 SCAR=0.0
Pod documented
159 31 my ($self) = @_;
160 31 return keys %{ $self->_sections };
161 }
162  
163 #pod =method sections
164 #pod
165 #pod my @sections = $sequence->sections;
166 #pod
167 #pod This method returns the section objects, in order.
168 #pod
169 #pod =cut
170  
171 101 ▶sub sections {
Subroutine sections: called CC=1 SCAR=0.0
Pod documented
172 101 my ($self) = @_;
173 101 return values %{ $self->_sections };
174 }
175  
176 4 ▶no Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
177 4 1;
178  
179 __END__
« MVP/Assembler.pm ↑ Summary MVP/Reader/Findable/ByExtension.pm »