| 1 | package Config::MVP::Assembler 2.200015; | ||||||||||||||||||||||||||||
| 2 | # ABSTRACT: multivalue-property config-loading state machine | ||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||
| 4 | 4 | ▶ | use Moose; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 4 | ▶ | use Config::MVP::Error; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 4 | ▶ | use Config::MVP::Sequence; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 4 | ▶ | use Config::MVP::Section; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | #pod =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 11 | #pod | ||||||||||||||||||||||||||||
| 12 | #pod First, you should probably read the L<example of using | ||||||||||||||||||||||||||||
| 13 | #pod Config::MVP|Config::MVP/EXAMPLE>. If you already know how it works, keep | ||||||||||||||||||||||||||||
| 14 | #pod going. | ||||||||||||||||||||||||||||
| 15 | #pod | ||||||||||||||||||||||||||||
| 16 | #pod Config::MVP::Assembler is a helper for constructing a Config::MVP::Sequence | ||||||||||||||||||||||||||||
| 17 | #pod object. It's a very simple state machine that lets you signal what kind of | ||||||||||||||||||||||||||||
| 18 | #pod events you've encountered while reading configuration. | ||||||||||||||||||||||||||||
| 19 | #pod | ||||||||||||||||||||||||||||
| 20 | #pod =head1 TYPICAL USE | ||||||||||||||||||||||||||||
| 21 | #pod | ||||||||||||||||||||||||||||
| 22 | #pod my $assembler = Config::MVP::Assembler->new; | ||||||||||||||||||||||||||||
| 23 | #pod | ||||||||||||||||||||||||||||
| 24 | #pod # Maybe you want a starting section: | ||||||||||||||||||||||||||||
| 25 | #pod my $starting_section = $assembler->section_class->new({ name => '_' }); | ||||||||||||||||||||||||||||
| 26 | #pod $assembler->sequence->add_section($section_starting); | ||||||||||||||||||||||||||||
| 27 | #pod | ||||||||||||||||||||||||||||
| 28 | #pod # We'll add some values, which will go to the starting section: | ||||||||||||||||||||||||||||
| 29 | #pod $assembler->add_value(x => 10); | ||||||||||||||||||||||||||||
| 30 | #pod $assembler->add_value(y => 20); | ||||||||||||||||||||||||||||
| 31 | #pod | ||||||||||||||||||||||||||||
| 32 | #pod # Change to a new section... | ||||||||||||||||||||||||||||
| 33 | #pod $assembler->change_section($moniker); | ||||||||||||||||||||||||||||
| 34 | #pod | ||||||||||||||||||||||||||||
| 35 | #pod # ...and add values to that section. | ||||||||||||||||||||||||||||
| 36 | #pod $assembler->add_value(x => 100); | ||||||||||||||||||||||||||||
| 37 | #pod $assembler->add_value(y => 200); | ||||||||||||||||||||||||||||
| 38 | #pod | ||||||||||||||||||||||||||||
| 39 | #pod The code above creates an assembler and populates it step by step. In the end, | ||||||||||||||||||||||||||||
| 40 | #pod to get values, you could do something like this: | ||||||||||||||||||||||||||||
| 41 | #pod | ||||||||||||||||||||||||||||
| 42 | #pod my @output; | ||||||||||||||||||||||||||||
| 43 | #pod | ||||||||||||||||||||||||||||
| 44 | #pod for my $section ($assembler->sequence->sections) { | ||||||||||||||||||||||||||||
| 45 | #pod push @output, [ $section->name, $section->package, $section->payload ]; | ||||||||||||||||||||||||||||
| 46 | #pod } | ||||||||||||||||||||||||||||
| 47 | #pod | ||||||||||||||||||||||||||||
| 48 | #pod When changing sections, the given section "moniker" is used for the new section | ||||||||||||||||||||||||||||
| 49 | #pod name. The result of passing that moniker to the assembler's | ||||||||||||||||||||||||||||
| 50 | #pod C<L</expand_package>> method is used as the section's package name. (By | ||||||||||||||||||||||||||||
| 51 | #pod default, this method does nothing.) The new section's C<multivalue_args> and | ||||||||||||||||||||||||||||
| 52 | #pod C<aliases> are determined by calling the C<mvp_multivalue_args> and | ||||||||||||||||||||||||||||
| 53 | #pod C<mvp_aliases> methods on the package. | ||||||||||||||||||||||||||||
| 54 | #pod | ||||||||||||||||||||||||||||
| 55 | #pod =attr sequence_class | ||||||||||||||||||||||||||||
| 56 | #pod | ||||||||||||||||||||||||||||
| 57 | #pod This attribute stores the name of the class to be used for the assembler's | ||||||||||||||||||||||||||||
| 58 | #pod sequence. It defaults to Config::MVP::Sequence. | ||||||||||||||||||||||||||||
| 59 | #pod | ||||||||||||||||||||||||||||
| 60 | #pod =cut | ||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | 4 | has sequence_class => ( | |||||||||||||||||||||||||||
| 63 | is => 'ro', | ||||||||||||||||||||||||||||
| 64 | isa => 'ClassName', | ||||||||||||||||||||||||||||
| 65 | lazy => 1, | ||||||||||||||||||||||||||||
| 66 | default => 'Config::MVP::Sequence', | ||||||||||||||||||||||||||||
| 67 | ); | ||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | #pod =attr section_class | ||||||||||||||||||||||||||||
| 70 | #pod | ||||||||||||||||||||||||||||
| 71 | #pod This attribute stores the name of the class to be used for sections created by | ||||||||||||||||||||||||||||
| 72 | #pod the assembler. It defaults to Config::MVP::Section. | ||||||||||||||||||||||||||||
| 73 | #pod | ||||||||||||||||||||||||||||
| 74 | #pod =cut | ||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | 4 | has section_class => ( | |||||||||||||||||||||||||||
| 77 | is => 'ro', | ||||||||||||||||||||||||||||
| 78 | isa => 'ClassName', | ||||||||||||||||||||||||||||
| 79 | lazy => 1, | ||||||||||||||||||||||||||||
| 80 | default => 'Config::MVP::Section', | ||||||||||||||||||||||||||||
| 81 | ); | ||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | #pod =attr sequence | ||||||||||||||||||||||||||||
| 84 | #pod | ||||||||||||||||||||||||||||
| 85 | #pod This is the sequence that the assembler is assembling. It defaults to a new | ||||||||||||||||||||||||||||
| 86 | #pod instance of the assembler's C<sequence_class>. | ||||||||||||||||||||||||||||
| 87 | #pod | ||||||||||||||||||||||||||||
| 88 | #pod =cut | ||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | has sequence => ( | ||||||||||||||||||||||||||||
| 91 | is => 'ro', | ||||||||||||||||||||||||||||
| 92 | isa => 'Config::MVP::Sequence', | ||||||||||||||||||||||||||||
| 93 | 8 | ▶ | default => sub { $_[0]->sequence_class->new({ assembler => $_[0] }) }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 94 | 4 | init_arg => undef, | |||||||||||||||||||||||||||
| 95 | handles => [ qw(is_finalized finalize) ], | ||||||||||||||||||||||||||||
| 96 | ); | ||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | 2 | ▶ | before finalize => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.2
| |||||||||||||||||||||||||||||
| 99 | 2 | my ($self) = @_; | |||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | 2 | ▶ | $self->end_section if $self->current_section; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 102 | 4 | }; | |||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | #pod =method begin_section | ||||||||||||||||||||||||||||
| 105 | #pod | ||||||||||||||||||||||||||||
| 106 | #pod $assembler->begin_section($package_moniker, $name); | ||||||||||||||||||||||||||||
| 107 | #pod | ||||||||||||||||||||||||||||
| 108 | #pod $assembler->begin_section($package_moniker); | ||||||||||||||||||||||||||||
| 109 | #pod | ||||||||||||||||||||||||||||
| 110 | #pod $assembler->begin_section( \$package ); | ||||||||||||||||||||||||||||
| 111 | #pod | ||||||||||||||||||||||||||||
| 112 | #pod This method tells the assembler that it should begin work on a new section with | ||||||||||||||||||||||||||||
| 113 | #pod the given identifier. If it is already working on a section, an error will be | ||||||||||||||||||||||||||||
| 114 | #pod raised. See C<L</change_section>> for a method to begin a new section, ending | ||||||||||||||||||||||||||||
| 115 | #pod the current one if needed. | ||||||||||||||||||||||||||||
| 116 | #pod | ||||||||||||||||||||||||||||
| 117 | #pod The package moniker is expanded by the C<L</expand_package>> method. The name, | ||||||||||||||||||||||||||||
| 118 | #pod if not given, defaults to the package moniker. These data are used to create a | ||||||||||||||||||||||||||||
| 119 | #pod new section and the section is added to the end of the sequence. If the | ||||||||||||||||||||||||||||
| 120 | #pod package argument is a reference, it is used as the literal value for the | ||||||||||||||||||||||||||||
| 121 | #pod package, and no expansion is performed. If it is a reference to undef, a | ||||||||||||||||||||||||||||
| 122 | #pod section with no package is created. | ||||||||||||||||||||||||||||
| 123 | #pod | ||||||||||||||||||||||||||||
| 124 | #pod =cut | ||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | 4 | has _between_sections => ( | |||||||||||||||||||||||||||
| 127 | is => 'rw', | ||||||||||||||||||||||||||||
| 128 | isa => 'Bool', | ||||||||||||||||||||||||||||
| 129 | default => 0, | ||||||||||||||||||||||||||||
| 130 | ); | ||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | 18 | ▶ | sub begin_section { | ||||||||||||||||||||||||||
|
Subroutine
begin_section: called CC=6 SCAR=18.6
Pod
documented
| |||||||||||||||||||||||||||||
| 133 | 18 | my ($self, $package_moniker, $name) = @_; | |||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | 18 | ▶ | Config::MVP::Error->throw("can't begin a new section while a section is open") | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 136 | if $self->current_section; | ||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | 18 | ▶ | $name = $package_moniker unless defined $name and length $name; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $name and length $name
MC/DC50%1/2
defined $name and length $name
defined $name
length $name
Truth table66%2/3
A and B
A= defined $nameB= length $name
| |||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | 18 | ▶ | my $package = ref($package_moniker) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 141 | ? $$package_moniker | ||||||||||||||||||||||||||||
| 142 | : $self->expand_package($package_moniker); | ||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | 18 | ▶ | my $section = $self->section_class->new({ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 145 | name => $name, | ||||||||||||||||||||||||||||
| 146 | (defined $package ? (package => $package) : ()), | ||||||||||||||||||||||||||||
| 147 | }); | ||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | 16 | $self->_between_sections(0); | |||||||||||||||||||||||||||
| 150 | 16 | $self->sequence->add_section($section); | |||||||||||||||||||||||||||
| 151 | } | ||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | #pod =method end_section | ||||||||||||||||||||||||||||
| 154 | #pod | ||||||||||||||||||||||||||||
| 155 | #pod $assembler->end_section; | ||||||||||||||||||||||||||||
| 156 | #pod | ||||||||||||||||||||||||||||
| 157 | #pod This ends the current section. If there is no current section, an exception is | ||||||||||||||||||||||||||||
| 158 | #pod raised. | ||||||||||||||||||||||||||||
| 159 | #pod | ||||||||||||||||||||||||||||
| 160 | #pod =cut | ||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | 16 | ▶ | sub end_section { | ||||||||||||||||||||||||||
|
Subroutine
end_section: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||||||||||
| 163 | 16 | my ($self) = @_; | |||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | 16 | ▶ | Config::MVP::Error->throw("can't end a section when no section is active") | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 166 | unless $self->current_section; | ||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||
| 168 | 16 | $self->current_section->finalize; | |||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||
| 170 | 16 | $self->_between_sections(1); | |||||||||||||||||||||||||||
| 171 | } | ||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||
| 173 | #pod =method change_section | ||||||||||||||||||||||||||||
| 174 | #pod | ||||||||||||||||||||||||||||
| 175 | #pod $assembler->change_section($package_moniker, $name); | ||||||||||||||||||||||||||||
| 176 | #pod | ||||||||||||||||||||||||||||
| 177 | #pod $assembler->change_section($package_moniker); | ||||||||||||||||||||||||||||
| 178 | #pod | ||||||||||||||||||||||||||||
| 179 | #pod This method calls C<begin_section>, first calling C<end_section> if needed. | ||||||||||||||||||||||||||||
| 180 | #pod | ||||||||||||||||||||||||||||
| 181 | #pod =cut | ||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||
| 183 | 11 | ▶ | sub change_section { | ||||||||||||||||||||||||||
|
Subroutine
change_section: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 184 | 11 | my $self = shift; | |||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||
| 186 | 11 | ▶ | $self->end_section if $self->current_section; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 187 | 11 | $self->begin_section(@_); | |||||||||||||||||||||||||||
| 188 | } | ||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||
| 190 | #pod =method add_value | ||||||||||||||||||||||||||||
| 191 | #pod | ||||||||||||||||||||||||||||
| 192 | #pod $assembler->add_value( $name => $value ); | ||||||||||||||||||||||||||||
| 193 | #pod | ||||||||||||||||||||||||||||
| 194 | #pod This method tells the assembler that it has encountered a named value and | ||||||||||||||||||||||||||||
| 195 | #pod should add it to the current section. If there is no current section, an | ||||||||||||||||||||||||||||
| 196 | #pod exception is raised. (If this is not the first time we've seen the name in the | ||||||||||||||||||||||||||||
| 197 | #pod section and it's not a multivalue property, the section class will raise an | ||||||||||||||||||||||||||||
| 198 | #pod exception on its own.) | ||||||||||||||||||||||||||||
| 199 | #pod | ||||||||||||||||||||||||||||
| 200 | #pod =cut | ||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||
| 202 | 36 | ▶ | sub add_value { | ||||||||||||||||||||||||||
|
Subroutine
add_value: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||||||||||||||||||||
| 203 | 36 | my ($self, $name, $value) = @_; | |||||||||||||||||||||||||||
| 204 | |||||||||||||||||||||||||||||
| 205 | 36 | ▶ | Config::MVP::Error->throw("can't set value when no section is active") | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 206 | unless my $section = $self->current_section; | ||||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||
| 208 | 36 | $section->add_value($name => $value); | |||||||||||||||||||||||||||
| 209 | } | ||||||||||||||||||||||||||||
| 210 | |||||||||||||||||||||||||||||
| 211 | #pod =method expand_package | ||||||||||||||||||||||||||||
| 212 | #pod | ||||||||||||||||||||||||||||
| 213 | #pod This method is passed a short identifier for a package and is expected to | ||||||||||||||||||||||||||||
| 214 | #pod return the full name of the module to load and package to interrogate. By | ||||||||||||||||||||||||||||
| 215 | #pod default it simply returns the name it was passed, meaning that package names | ||||||||||||||||||||||||||||
| 216 | #pod must be given whole to the C<change_section> method. | ||||||||||||||||||||||||||||
| 217 | #pod | ||||||||||||||||||||||||||||
| 218 | #pod =cut | ||||||||||||||||||||||||||||
| 219 | |||||||||||||||||||||||||||||
| 220 | 18 | ▶ | sub expand_package { $_[1] } | ||||||||||||||||||||||||||
|
Subroutine
expand_package: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 221 | |||||||||||||||||||||||||||||
| 222 | #pod =method current_section | ||||||||||||||||||||||||||||
| 223 | #pod | ||||||||||||||||||||||||||||
| 224 | #pod This returns the section object onto which the assembler is currently adding | ||||||||||||||||||||||||||||
| 225 | #pod values. If no section has yet been created, this method will return false. | ||||||||||||||||||||||||||||
| 226 | #pod | ||||||||||||||||||||||||||||
| 227 | #pod =cut | ||||||||||||||||||||||||||||
| 228 | |||||||||||||||||||||||||||||
| 229 | 99 | ▶ | sub current_section { | ||||||||||||||||||||||||||
|
Subroutine
current_section: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 230 | 99 | my ($self) = @_; | |||||||||||||||||||||||||||
| 231 | |||||||||||||||||||||||||||||
| 232 | 99 | ▶ | return if $self->_between_sections; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 233 | 87 | my (@sections) = $self->sequence->sections; | |||||||||||||||||||||||||||
| 234 | 87 | ▶ | return $sections[ -1 ] if @sections; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 235 | |||||||||||||||||||||||||||||
| 236 | 10 | return; | |||||||||||||||||||||||||||
| 237 | } | ||||||||||||||||||||||||||||
| 238 | |||||||||||||||||||||||||||||
| 239 | 4 | ▶ | no Moose; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 240 | 4 | 1; | |||||||||||||||||||||||||||
| 241 | |||||||||||||||||||||||||||||
| 242 | __END__ | ||||||||||||||||||||||||||||