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             # ABSTRACT: an ordered set of named configuration sections
3             $Config::MVP::Sequence::VERSION = '2.200012';
4 4     4   36 use Moose 0.91;
  4         108  
  4         35  
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   30491 use Tie::IxHash;
  4         18009  
  4         132  
17 4     4   32 use Config::MVP::Error;
  4         62  
  4         92  
18 4     4   2189 use Config::MVP::Section;
  4         20  
  4         215  
19 4     4   38 use Moose::Util::TypeConstraints ();
  4         9  
  4         2426  
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       23 unless (defined $assembler) {
57 0         0 Config::MVP::Error->throw("can't access sequences's destroyed assembler")
58             }
59              
60 4         64 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 79 my ($self, $section) = @_;
92              
93 31 50       841 Config::MVP::Error->throw("can't add sections to finalized sequence")
94             if $self->is_finalized;
95              
96 31         772 my $name = $section->name;
97 31 50       950 confess "already have a section named $name" if $self->_sections->{ $name };
98              
99 31         377 $section->_set_sequence($self);
100              
101 31 100       94 if (my @names = $self->section_names) {
102 23         570 my $last_section = $self->section_named( $names[-1] );
103 23 50       755 $last_section->finalize unless $last_section->is_finalized;
104             }
105              
106 31         998 $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 25 my ($self, $name) = @_;
120              
121 4 50       108 Config::MVP::Error->throw("can't delete sections from finalized sequence")
122             if $self->is_finalized;
123              
124 4         150 my $sections = $self->_sections;
125              
126 4 50       21 return unless exists $sections->{ $name };
127              
128 4         35 $sections->{ $name }->_clear_sequence;
129              
130 4         23 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 60 my ($self, $name) = @_;
144 23         680 my $sections = $self->_sections;
145              
146 23 50       101 return unless exists $sections->{ $name };
147 23         203 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 80 my ($self) = @_;
160 31         46 return keys %{ $self->_sections };
  31         961  
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 628 my ($self) = @_;
173 101         135 return values %{ $self->_sections };
  101         2853  
174             }
175              
176 4     4   37 no Moose;
  4         10  
  4         34  
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.200012
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 ATTRIBUTES
202              
203             =head2 is_finalized
204              
205             This attribute is true if the sequence has been marked finalized, which will
206             prevent any changes (via methods like C<add_section> or C<delete_section>). It
207             can be set with the C<finalize> method.
208              
209             =head1 METHODS
210              
211             =head2 add_section
212              
213             $sequence->add_section($section);
214              
215             This method adds the given section to the end of the sequence. If the sequence
216             already contains a section with the same name as the new section, an exception
217             will be raised.
218              
219             =head2 delete_section
220              
221             my $deleted_section = $sequence->delete_section( $name );
222              
223             This method removes a section from the sequence and returns the removed
224             section. If no section existed, the method returns false.
225              
226             =head2 section_named
227              
228             my $section = $sequence->section_named( $name );
229              
230             This method returns the section with the given name, if one exists in the
231             sequence. If no such section exists, the method returns false.
232              
233             =head2 section_names
234              
235             my @names = $sequence->section_names;
236              
237             This method returns a list of the names of the sections, in order.
238              
239             =head2 sections
240              
241             my @sections = $sequence->sections;
242              
243             This method returns the section objects, in order.
244              
245             =head1 AUTHOR
246              
247             Ricardo Signes <rjbs@cpan.org>
248              
249             =head1 COPYRIGHT AND LICENSE
250              
251             This software is copyright (c) 2021 by Ricardo Signes.
252              
253             This is free software; you can redistribute it and/or modify it under
254             the same terms as the Perl 5 programming language system itself.
255              
256             =cut