Permalink
Browse files

Simplify DynamicConfig by handling separation of arguments in a class…

… method

That way implementing classes don't need to mess with BUILDARGS.

The DynamicConfig Role requires 'separate_local_config' method.

Role::Stash::Plugins defines an apporpriate one.
  • Loading branch information...
1 parent 91d4850 commit 58ec84d163c2fbe7569b9602729588c3f256243b @rwstauner committed Dec 15, 2010
Showing with 47 additions and 36 deletions.
  1. +22 −1 lib/Dist/Zilla/Role/DynamicConfig.pm
  2. +25 −35 lib/Dist/Zilla/Role/Stash/Plugins.pm
@@ -28,7 +28,8 @@ This overwrites the L<Class::MOP::Instance> method
called to prepare arguments before instantiation.
It separates the expected arguments
-and places the remaining unknown arguments into I<_config>.
+(including anything caught by L</separate_local_config>)
+and places the remaining unknown/dynamic arguments into L</_config>.
=cut
@@ -42,13 +43,31 @@ sub BUILDARGS {
confess 'do not try to pass _config as a build arg!'
if $copy{_config};
+ my $other = $class->separate_local_config(\%copy);
+
return {
zilla => $zilla,
plugin_name => $name,
_config => \%copy,
+ %$other
}
}
+=method separate_local_config
+
+Separate any arguments that should be stored directly on the object
+rather than in the dynamic L</_config> attribute.
+
+Remove those arguments from the passed in hashref,
+make any necessary modifications (like renaming the keys if desired),
+and return a hashref with the result.
+
+Required.
+
+=cut
+
+requires 'separate_local_config';
+
no Moose::Role;
1;
@@ -60,4 +79,6 @@ This is a role for a L<Plugin|Dist::Zilla::Role::Plugin>
(or possibly other classes)
that accepts a dynamic configuration.
+Plugins performing this role must define L</separate_local_config>.
+
=cut
@@ -60,40 +60,6 @@ consider L</get_stashed_config> or L</merge_stashed_config>.
# _config inherited
-=method BUILDARGS
-
-This overwrites the L<Class::MOP::Instance> method
-called to prepare arguments before instantiation.
-
-It uses L<Dist::Zilla::Role::DynamicConfig/BUILDARGS>
-to process the arguments initially,
-then separates any local arguments from the keys in _config
-(L</argument_separator>, for instance).
-
-=cut
-
-around 'BUILDARGS' => sub {
- my ($orig, $class, @arg) = @_;
-
- # Prepare arguments including zilla, plugin_name, and _config.
- my $built = $orig->($class, @arg);
- my $config = $built->{_config};
-
- # keys for other plugins should include non-word characters
- # (like "-Plugin::Name:variable"), so any keys that are only
- # word characters (valid identifiers) are for this object.
- # TODO: make this configurable
- my @local = grep { /^\w+$/ } keys %$config;
- my %other;
- @other{@local} = delete @$config{@local}
- if @local;
-
- return {
- %$built,
- %other,
- }
-};
-
=method get_stashed_config
Return a hashref of the config arguments for the plugin
@@ -218,6 +184,30 @@ sub merge_stashed_config {
}
}
+=method separate_local_config
+
+Removes any hash keys that are only word characters
+(valid perl identifiers (including L</argument_separator>))
+because the dynamic keys intended for other plugins will all
+contain non-word characters.
+
+Overwrite this if necessary.
+
+=cut
+
+sub separate_local_config {
+ my ($self, $config) = @_;
+ # keys for other plugins should include non-word characters
+ # (like "-Plugin::Name:variable"), so any keys that are only
+ # word characters (valid identifiers) are for this object.
+ my @local = grep { /^\w+$/ } keys %$config;
+ my %other;
+ @other{@local} = delete @$config{@local}
+ if @local;
+
+ return \%other;
+}
+
=method stash_name
Returns the stash name (including the '%').
@@ -240,7 +230,7 @@ sub stash_name {
no Moose::Role;
1;
-=for :stopwords BUILDARGS dist-zilla zilla
+=for :stopwords dist-zilla zilla
=head1 DESCRIPTION

0 comments on commit 58ec84d

Please sign in to comment.