| 1 | package Config::MVP::Assembler::WithBundles 2.200015; | ||||||||||||||
| 2 | # ABSTRACT: a role to make assemblers expand bundles | ||||||||||||||
| 3 | |||||||||||||||
| 4 | 2 | ▶ | use Moose::Role; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 5 | |||||||||||||||
| 6 | 2 | ▶ | use Params::SomeUtil qw(_HASHLIKE _ARRAYLIKE); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 7 | 2 | ▶ | use Class::Load 0.17 (); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 8 | |||||||||||||||
| 9 | #pod =head1 DESCRIPTION | ||||||||||||||
| 10 | #pod | ||||||||||||||
| 11 | #pod Config::MVP::Assembler::WithBundles is a role to be composed into a | ||||||||||||||
| 12 | #pod Config::MVP::Assembler subclass. It allows some sections of configuration to | ||||||||||||||
| 13 | #pod be treated as bundles. When any section is ended, if that section represented | ||||||||||||||
| 14 | #pod a bundle, its bundle contents will be unrolled and will replace it in the | ||||||||||||||
| 15 | #pod sequence. | ||||||||||||||
| 16 | #pod | ||||||||||||||
| 17 | #pod A package is considered a bundle if C<package_bundle_method> returns a | ||||||||||||||
| 18 | #pod defined value (which is the name of a method that will be called on | ||||||||||||||
| 19 | #pod that package to retrieve its bundle config). | ||||||||||||||
| 20 | #pod | ||||||||||||||
| 21 | #pod my $method = $assembler->package_bundle_method($package); | ||||||||||||||
| 22 | #pod | ||||||||||||||
| 23 | #pod The default implementation looks for a method called C<mvp_bundle_config>, but | ||||||||||||||
| 24 | #pod C<package_bundle_method> can be replaced with one that returns the name of a | ||||||||||||||
| 25 | #pod different bundle-identifying method-name. | ||||||||||||||
| 26 | #pod | ||||||||||||||
| 27 | #pod Bundles are expanded by a call to the assembler's | ||||||||||||||
| 28 | #pod C<replace_bundle_with_contents> method, like this: | ||||||||||||||
| 29 | #pod | ||||||||||||||
| 30 | #pod $assembler->replace_bundle_with_contents($section, $method); | ||||||||||||||
| 31 | #pod | ||||||||||||||
| 32 | #pod =head2 replace_bundle_with_contents | ||||||||||||||
| 33 | #pod | ||||||||||||||
| 34 | #pod The default C<replace_bundle_with_contents> method deletes the section from the | ||||||||||||||
| 35 | #pod sequence. It then gets a description of the new sections to introduce, like | ||||||||||||||
| 36 | #pod this: | ||||||||||||||
| 37 | #pod | ||||||||||||||
| 38 | #pod my @new_config = $bundle_section->package->$method({ | ||||||||||||||
| 39 | #pod name => $bundle_section->name, | ||||||||||||||
| 40 | #pod package => $bundle_section->package, | ||||||||||||||
| 41 | #pod payload => $bundle_section->payload, | ||||||||||||||
| 42 | #pod }); | ||||||||||||||
| 43 | #pod | ||||||||||||||
| 44 | #pod (We pass a hashref rather than a section so that bundles can be expanded | ||||||||||||||
| 45 | #pod synthetically without having to laboriously create a new Section.) | ||||||||||||||
| 46 | #pod | ||||||||||||||
| 47 | #pod The returned C<@new_config> is a list of arrayrefs, each of which has three | ||||||||||||||
| 48 | #pod entries: | ||||||||||||||
| 49 | #pod | ||||||||||||||
| 50 | #pod [ $name, $package, $payload ] | ||||||||||||||
| 51 | #pod | ||||||||||||||
| 52 | #pod Each arrayref is converted into a section in the sequence. The C<$payload> | ||||||||||||||
| 53 | #pod should be an arrayref of name/value pairs to be added to the created section. | ||||||||||||||
| 54 | #pod | ||||||||||||||
| 55 | #pod =cut | ||||||||||||||
| 56 | |||||||||||||||
| 57 | 25 | ▶ | sub package_bundle_method { | ||||||||||||
|
Subroutine
package_bundle_method: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||
| 58 | 25 | my ($self, $pkg) = @_; | |||||||||||||
| 59 | 25 | ▶ | return unless $pkg->can('mvp_bundle_config'); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 60 | 6 | return 'mvp_bundle_config'; | |||||||||||||
| 61 | } | ||||||||||||||
| 62 | |||||||||||||||
| 63 | 9 | ▶ | after end_section => sub { | ||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||||||
| 64 | 9 | my ($self) = @_; | |||||||||||||
| 65 | |||||||||||||||
| 66 | 9 | my $seq = $self->sequence; | |||||||||||||
| 67 | |||||||||||||||
| 68 | 9 | my ($last) = ($seq->sections)[-1]; | |||||||||||||
| 69 | 9 | ▶ | return unless $last->package; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 70 | 9 | ▶ | return unless my $method = $self->package_bundle_method($last->package); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 71 | |||||||||||||||
| 72 | 4 | $self->replace_bundle_with_contents($last, $method); | |||||||||||||
| 73 | 2 | }; | |||||||||||||
| 74 | |||||||||||||||
| 75 | 4 | ▶ | sub replace_bundle_with_contents { | ||||||||||||
|
Subroutine
replace_bundle_with_contents: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 76 | 4 | my ($self, $bundle_sec, $method) = @_; | |||||||||||||
| 77 | |||||||||||||||
| 78 | 4 | my $seq = $self->sequence; | |||||||||||||
| 79 | |||||||||||||||
| 80 | 4 | $seq->delete_section($bundle_sec->name); | |||||||||||||
| 81 | |||||||||||||||
| 82 | 4 | $self->_add_bundle_contents($method, { | |||||||||||||
| 83 | name => $bundle_sec->name, | ||||||||||||||
| 84 | package => $bundle_sec->package, | ||||||||||||||
| 85 | payload => $bundle_sec->payload, | ||||||||||||||
| 86 | }); | ||||||||||||||
| 87 | }; | ||||||||||||||
| 88 | |||||||||||||||
| 89 | 16 | ▶ | sub load_package { | ||||||||||||
|
Subroutine
load_package: called CC=2 SCAR=7.2
Pod
undocumented
| |||||||||||||||
| 90 | 16 | my ($self, $package, $section_name) = @_; | |||||||||||||
| 91 | |||||||||||||||
| 92 | 16 | ▶ | Class::Load::load_optional_class($package) | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 93 | or $self->missing_package($package, $section_name); | ||||||||||||||
| 94 | } | ||||||||||||||
| 95 | |||||||||||||||
| 96 | 0 | ▶ | sub missing_package { | ||||||||||||
|
Subroutine
missing_package: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||
| 97 | 0 | my ($self, $package, $section_name) = @_ ; | |||||||||||||
| 98 | |||||||||||||||
| 99 | 0 | my $class = Moose::Meta::Class->create_anon_class( | |||||||||||||
| 100 | superclasses => [ 'Config::MVP::Error' ], | ||||||||||||||
| 101 | cached => 1, | ||||||||||||||
| 102 | attributes => [ | ||||||||||||||
| 103 | Moose::Meta::Attribute->new(package => ( | ||||||||||||||
| 104 | is => 'ro', | ||||||||||||||
| 105 | required => 1, | ||||||||||||||
| 106 | )), | ||||||||||||||
| 107 | Moose::Meta::Attribute->new(section_name => ( | ||||||||||||||
| 108 | is => 'ro', | ||||||||||||||
| 109 | required => 1, | ||||||||||||||
| 110 | )), | ||||||||||||||
| 111 | ], | ||||||||||||||
| 112 | ); | ||||||||||||||
| 113 | |||||||||||||||
| 114 | 0 | $class->name->throw({ | |||||||||||||
| 115 | ident => 'package not installed', | ||||||||||||||
| 116 | message => "$package (for section $section_name) does not appear to be installed", | ||||||||||||||
| 117 | package => $package, | ||||||||||||||
| 118 | section_name => $section_name, | ||||||||||||||
| 119 | }); | ||||||||||||||
| 120 | } | ||||||||||||||
| 121 | |||||||||||||||
| 122 | 6 | ▶ | sub _add_bundle_contents { | ||||||||||||
|
Subroutine
_add_bundle_contents: called CC=10 SCAR=23.1
| |||||||||||||||
| 123 | 6 | my ($self, $method, $arg) = @_; | |||||||||||||
| 124 | |||||||||||||||
| 125 | 6 | my @bundle_config = $arg->{package}->$method($arg); | |||||||||||||
| 126 | |||||||||||||||
| 127 | 6 | PLUGIN: for my $plugin (@bundle_config) { | |||||||||||||
| 128 | 16 | my ($name, $package, $payload) = @$plugin; | |||||||||||||
| 129 | |||||||||||||||
| 130 | 16 | $self->load_package($package, $name); | |||||||||||||
| 131 | |||||||||||||||
| 132 | 16 | ▶ | if (my $method = $self->package_bundle_method( $package )) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 133 | 2 | $self->_add_bundle_contents($method, { | |||||||||||||
| 134 | name => $name, | ||||||||||||||
| 135 | package => $package, | ||||||||||||||
| 136 | payload => $payload, | ||||||||||||||
| 137 | }); | ||||||||||||||
| 138 | } else { | ||||||||||||||
| 139 | 14 | my $section = $self->section_class->new({ | |||||||||||||
| 140 | name => $name, | ||||||||||||||
| 141 | package => $package, | ||||||||||||||
| 142 | }); | ||||||||||||||
| 143 | |||||||||||||||
| 144 | 14 | ▶ | if (_HASHLIKE($payload)) { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 145 | # XXX: Clearly this is a hack. -- rjbs, 2009-08-24 | ||||||||||||||
| 146 | 8 | for my $name (keys %$payload) { | |||||||||||||
| 147 | my @v = ref $payload->{$name} eq ref [] | ||||||||||||||
| 148 | 2 | ? @{$payload->{$name}} | |||||||||||||
| 149 | 8 | ▶ | : $payload->{$name}; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 150 | 8 | ▶ | Carp::confess("got impossible zero-value <$name> key") | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 151 | unless @v; | ||||||||||||||
| 152 | 8 | $section->add_value($name => $_) for @v; | |||||||||||||
| 153 | } | ||||||||||||||
| 154 | } elsif (_ARRAYLIKE($payload)) { | ||||||||||||||
| 155 | 6 | ▶ | for (my $i = 0; $i < @$payload; $i += 2) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 156 | 10 | $section->add_value(@$payload[ $i, $i + 1 ]); | |||||||||||||
| 157 | } | ||||||||||||||
| 158 | } else { | ||||||||||||||
| 159 | 0 | Carp::confess("don't know how to interpret section payload $payload"); | |||||||||||||
| 160 | } | ||||||||||||||
| 161 | |||||||||||||||
| 162 | 14 | $self->sequence->add_section($section); | |||||||||||||
| 163 | 14 | $section->finalize; | |||||||||||||
| 164 | } | ||||||||||||||
| 165 | } | ||||||||||||||
| 166 | } | ||||||||||||||
| 167 | |||||||||||||||
| 168 | 2 | ▶ | no Moose; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 169 | 2 | 1; | |||||||||||||
| 170 | |||||||||||||||
| 171 | __END__ | ||||||||||||||