File Coverage

blib/lib/Config/MVP/Sequence.pm
Criterion Covered Total %
statement 48 52 92.3
branch 10 20 50.0
condition n/a
subroutine 12 13 92.3
pod 5 6 83.3
total 75 91 82.4


line stmt bran cond sub pod time code
1             package Config::MVP::Sequence 2.200013;
2             # ABSTRACT: an ordered set of named configuration sections
3              
4 4     4   29 use Moose 0.91;
  4         78  
  4         30  
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     4   27553 use Tie::IxHash;
  4         15966  
  4         118  
17 4     4   29 use Config::MVP::Error;
  4         9  
  4         149  
18 4     4   1834 use Config::MVP::Section;
  4         16  
  4         154  
19 4     4   35 use Moose::Util::TypeConstraints ();
  4         9  
  4         2195  
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             default => sub {
28             tie my %section, 'Tie::IxHash';
29             return \%section;
30             },
31             );
32              
33             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             sub _set_assembler {
43 0     0   0 my ($self, $assembler) = @_;
44              
45 0 0       0 Config::MVP::Error->throw("can't alter Config::MVP::Sequence's assembler")
46             if $self->assembler;
47              
48 0         0 $self->__set_assembler($assembler);
49             }
50              
51             sub assembler {
52 4     4 0 10 my ($self) = @_;
53 4 50       128 return undef unless $self->_assembler_has_been_set;
54 4         120 my $assembler = $self->_assembler;
55              
56 4 50       24 unless (defined $assembler) {
57 0         0 Config::MVP::Error->throw("can't access sequences's destroyed assembler")
58             }
59              
60 4         68 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             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             sub add_section {
91 31     31 1 63 my ($self, $section) = @_;
92              
93 31 50       761 Config::MVP::Error->throw("can't add sections to finalized sequence")
94             if $self->is_finalized;
95              
96 31         704 my $name = $section->name;
97 31 50       794 confess "already have a section named $name" if $self->_sections->{ $name };
98              
99 31         281 $section->_set_sequence($self);
100              
101 31 100       101 if (my @names = $self->section_names) {
102 23         471 my $last_section = $self->section_named( $names[-1] );
103 23 50       691 $last_section->finalize unless $last_section->is_finalized;
104             }
105              
106 31         889 $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             sub delete_section {
119 4     4 1 12 my ($self, $name) = @_;
120              
121 4 50       88 Config::MVP::Error->throw("can't delete sections from finalized sequence")
122             if $self->is_finalized;
123              
124 4         98 my $sections = $self->_sections;
125              
126 4 50       48 return unless exists $sections->{ $name };
127              
128 4         35 $sections->{ $name }->_clear_sequence;
129              
130 4         20 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             sub section_named {
143 23     23 1 51 my ($self, $name) = @_;
144 23         601 my $sections = $self->_sections;
145              
146 23 50       82 return unless exists $sections->{ $name };
147 23         149 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             sub section_names {
159 31     31 1 58 my ($self) = @_;
160 31         41 return keys %{ $self->_sections };
  31         799  
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             sub sections {
172 101     101 1 470 my ($self) = @_;
173 101         133 return values %{ $self->_sections };
  101         2533  
174             }
175              
176 4     4   33 no Moose;
  4         10  
  4         31  
177             1;
178              
179             __END__
180              
181             =pod
182              
183             =encoding UTF-8
184              
185             =head1 NAME
186              
187             Config::MVP::Sequence - an ordered set of named configuration sections
188              
189             =head1 VERSION
190              
191             version 2.200013
192              
193             =head1 DESCRIPTION
194              
195             A Config::MVP::Sequence is an ordered set of configuration sections, each of
196             which has a name unique within the sequence.
197              
198             For the most part, you can just consult L<Config::MVP> to understand what this
199             class is and how it's used.
200              
201             =head1 PERL VERSION
202              
203             This module should work on any version of perl still receiving updates from
204             the Perl 5 Porters. This means it should work on any version of perl released
205             in the last two to three years. (That is, if the most recently released
206             version is v5.40, then this module should work on both v5.40 and v5.38.)
207              
208             Although it may work on older versions of perl, no guarantee is made that the
209             minimum required version will not be increased. The version may be increased
210             for any reason, and there is no promise that patches will be accepted to lower
211             the minimum required perl.
212              
213             =head1 ATTRIBUTES
214              
215             =head2 is_finalized
216              
217             This attribute is true if the sequence has been marked finalized, which will
218             prevent any changes (via methods like C<add_section> or C<delete_section>). It
219             can be set with the C<finalize> method.
220              
221             =head1 METHODS
222              
223             =head2 add_section
224              
225             $sequence->add_section($section);
226              
227             This method adds the given section to the end of the sequence. If the sequence
228             already contains a section with the same name as the new section, an exception
229             will be raised.
230              
231             =head2 delete_section
232              
233             my $deleted_section = $sequence->delete_section( $name );
234              
235             This method removes a section from the sequence and returns the removed
236             section. If no section existed, the method returns false.
237              
238             =head2 section_named
239              
240             my $section = $sequence->section_named( $name );
241              
242             This method returns the section with the given name, if one exists in the
243             sequence. If no such section exists, the method returns false.
244              
245             =head2 section_names
246              
247             my @names = $sequence->section_names;
248              
249             This method returns a list of the names of the sections, in order.
250              
251             =head2 sections
252              
253             my @sections = $sequence->sections;
254              
255             This method returns the section objects, in order.
256              
257             =head1 AUTHOR
258              
259             Ricardo Signes <cpan@semiotic.systems>
260              
261             =head1 COPYRIGHT AND LICENSE
262              
263             This software is copyright (c) 2022 by Ricardo Signes.
264              
265             This is free software; you can redistribute it and/or modify it under
266             the same terms as the Perl 5 programming language system itself.
267              
268             =cut