Skip to content

Commit

Permalink
remove unneed application of method modifiers
Browse files Browse the repository at this point in the history
'around' modifiers for _osprey_config, _osprey_options,
_osprey_subcommands need only be applied once for each command class.
this removes the need for a separate subroutine to apply the
modifiers.
  • Loading branch information
djerius committed Nov 21, 2018
1 parent b7f815a commit 0ad1e54
Showing 1 changed file with 19 additions and 21 deletions.
40 changes: 19 additions & 21 deletions lib/CLI/Osprey.pm
Original file line number Diff line number Diff line change
Expand Up @@ -54,27 +54,9 @@ sub import {
@import_options,
};

$around->(_osprey_config => sub {
my ($orig, $self) = (shift, shift);
return $self->$orig(@_), %$osprey_config;
});

my $options_data = { };
my $subcommands = { };

my $apply_modifiers = sub {
return if $target->can('new_with_options');
$with->('CLI::Osprey::Role');
$around->(_osprey_options => sub {
my ($orig, $self) = (shift, shift);
return $self->$orig(@_), %$options_data;
});
$around->(_osprey_subcommands => sub {
my ($orig, $self) = (shift, shift);
return $self->$orig(@_), %$subcommands;
});
};

my $added_order = 0;

my $option = sub {
Expand All @@ -83,7 +65,6 @@ sub import {
$has->($name => _non_option_attributes(%attributes));
$options_data->{$name} = _option_attributes($name, %attributes);
$options_data->{$name}{added_order} = ++$added_order;
$apply_modifiers->();
};

my $subcommand = sub {
Expand All @@ -101,7 +82,6 @@ sub import {
}

$subcommands->{$name} = $subobject;
$apply_modifiers->();
};

if (my $info = $Role::Tiny::INFO{$target}) {
Expand All @@ -115,7 +95,25 @@ sub import {
*{"${target}::subcommand"} = $subcommand;
}

$apply_modifiers->();
unless ( $target->does('CLI::Osprey::Role') ) {

$with->('CLI::Osprey::Role');

$around->(_osprey_config => sub {
my ($orig, $self) = (shift, shift);
return $self->$orig(@_), %$osprey_config;
});

$around->(_osprey_options => sub {
my ($orig, $self) = (shift, shift);
return $self->$orig(@_), %$options_data;
});

$around->(_osprey_subcommands => sub {
my ($orig, $self) = (shift, shift);
return $self->$orig(@_), %$subcommands;
});
}

return;
}
Expand Down

0 comments on commit 0ad1e54

Please sign in to comment.