Permalink
Browse files

Added documentation (hope it works ;) as told hehe) and use of Packag…

…e::Stash
  • Loading branch information...
Getty committed May 7, 2012
1 parent d49f4b4 commit 8e659d1b5d517afa4839011575dc7f581c94b44a
Showing with 116 additions and 74 deletions.
  1. +116 −74 lib/MooX/Cmd.pm
View
@@ -7,6 +7,7 @@ use Carp;
use Module::Pluggable::Object;
use Regexp::Common;
use Data::Record;
+use Package::Stash;
my %DEFAULT_OPTIONS = (
'creation_chain_methods' => ['new_with_options','new'],
@@ -42,91 +43,82 @@ sub import {
require => 1,
)->plugins;
- {
+ my $stash = Package::Stash->new($caller);
- no strict 'refs';
- *{"${caller}::$execute_return_method_name"} = sub { shift->{$execute_return_method_name} }
+ $stash->add_symbol('&'.$execute_return_method_name, sub { shift->{$execute_return_method_name} });
+ $stash->add_symbol('&'.$import_options{creation_method_name}, sub {
+ my ( $class, %params ) = @_;
- }
-
- {
-
- no strict 'refs';
- *{"${caller}::$import_options{creation_method_name}"} = sub {
- my ( $class, %params ) = @_;
+ my @moox_cmd_chain = defined $params{__moox_cmd_chain} ? @{$params{__moox_cmd_chain}} : ();
+
+ my %create_params;
- my @moox_cmd_chain = defined $params{__moox_cmd_chain} ? @{$params{__moox_cmd_chain}} : ();
-
- my %create_params;
+ my %cmds;
+
+ for my $cmd_plugin (@cmd_plugins) {
+ $cmds{_mkcommand($cmd_plugin,$base)} = $cmd_plugin;
+ }
+
+ my $opts_record = Data::Record->new({
+ split => qr{\s+},
+ unless => $RE{quoted},
+ });
- my %cmds;
-
- for my $cmd_plugin (@cmd_plugins) {
- $cmds{_mkcommand($cmd_plugin,$base)} = $cmd_plugin;
- }
-
- my $opts_record = Data::Record->new({
- split => qr{\s+},
- unless => $RE{quoted},
- });
+ my @args = $opts_record->records(join(' ',@ARGV));
- my @args = $opts_record->records(join(' ',@ARGV));
-
- my @used_args;
-
- my $cmd;
+ my @used_args;
- while (my $arg = shift @args) {
- if (defined $cmds{$arg}) {
- $cmd = $cmds{$arg};
- last;
- } else {
- push @used_args, $arg;
- }
- }
-
- my $creation_method;
- for (@creation_chain) {
- $creation_method = $caller->can($_);
- last if $creation_method;
+ my $cmd;
+
+ while (my $arg = shift @args) {
+ if (defined $cmds{$arg}) {
+ $cmd = $cmds{$arg};
+ last;
+ } else {
+ push @used_args, $arg;
}
-
- @ARGV = @used_args;
- my $self = $creation_method->($class, %params);
-
- my @execute_return;
-
- if ($cmd) {
- @ARGV = @args;
- push @moox_cmd_chain, $self;
- my %cmd_create_params = defined $create_params{$cmd} ? %{$create_params{$cmd}} : ();
- my $creation_method_name = $import_options{creation_method_name};
- my $creation_method = $cmd->can($creation_method_name);
- my $cmd_plugin;
- if ($creation_method) {
- $cmd_create_params{__moox_cmd_chain} = \@moox_cmd_chain;
- $cmd_plugin = $creation_method->($cmd, %cmd_create_params);
- @execute_return = @{$cmd_plugin->$execute_return_method_name};
- } else {
- for (@creation_chain) {
- if ($creation_method = $cmd->can($_)) {
- $cmd_plugin = $creation_method->($cmd, %cmd_create_params);
- last;
- }
+ }
+
+ my $creation_method;
+ for (@creation_chain) {
+ $creation_method = $caller->can($_);
+ last if $creation_method;
+ }
+
+ @ARGV = @used_args;
+ my $self = $creation_method->($class, %params);
+
+ my @execute_return;
+
+ if ($cmd) {
+ @ARGV = @args;
+ push @moox_cmd_chain, $self;
+ my %cmd_create_params = defined $create_params{$cmd} ? %{$create_params{$cmd}} : ();
+ my $creation_method_name = $import_options{creation_method_name};
+ my $creation_method = $cmd->can($creation_method_name);
+ my $cmd_plugin;
+ if ($creation_method) {
+ $cmd_create_params{__moox_cmd_chain} = \@moox_cmd_chain;
+ $cmd_plugin = $creation_method->($cmd, %cmd_create_params);
+ @execute_return = @{$cmd_plugin->$execute_return_method_name};
+ } else {
+ for (@creation_chain) {
+ if ($creation_method = $cmd->can($_)) {
+ $cmd_plugin = $creation_method->($cmd, %cmd_create_params);
+ last;
}
- croak "cant find a creation method on ".$cmd unless $creation_method;
- @execute_return = $cmd_plugin->$execute_method_name(\@ARGV,\@moox_cmd_chain);
}
- } else {
- @execute_return = $self->$execute_method_name(\@ARGV,\@moox_cmd_chain);
+ croak "cant find a creation method on ".$cmd unless $creation_method;
+ @execute_return = $cmd_plugin->$execute_method_name(\@ARGV,\@moox_cmd_chain);
}
-
- $self->{$execute_return_method_name} = \@execute_return;
-
- return $self;
+ } else {
+ @execute_return = $self->$execute_method_name(\@ARGV,\@moox_cmd_chain);
}
- }
+ $self->{$execute_return_method_name} = \@execute_return;
+
+ return $self;
+ });
}
@@ -135,10 +127,60 @@ sub import {
=encoding utf8
=head1 SYNOPSIS
+
+ package MyApp;
+
+ use Moo;
+ use MooX::Cmd;
+
+ sub execute {
+ my ( $self, $args_ref, $chain_ref ) = @_;
+ my @extra_argv = @{$args_ref};
+ my @chain = @{$chain_ref} # in this case only ( $myapp )
+ # where $myapp == $self
+ }
+
+ 1;
+ package MyApp::Cmd::Command;
+ # for "myapp command"
+
+ use Moo;
+ use MooX::Cmd;
+
+ # gets executed on "myapp command" but not on "myapp command command"
+ # there MyApp::Cmd::Command still gets instantiated and for the chain
+ sub execute {
+ my ( $self, $args_ref, $chain_ref ) = @_;
+ my @chain = @{$chain_ref} # in this case ( $myapp, $myapp_cmd_command )
+ # where $myapp_cmd_command == $self
+ }
+
+ 1;
+
+ package MyApp::Cmd::Command::Cmd::Command;
+ # for "myapp command command"
+
+ use Moo;
+ use MooX::Cmd;
+
+ # gets executed on "myapp command command" and will not get instantiated
+ # on "myapp command" cause it doesnt appear in the chain there
+ sub execute {
+ my ( $self, $args_ref, $chain_ref ) = @_;
+ my @chain = @{$chain_ref} # in this case ( $myapp, $myapp_cmd_command,
+ # $myapp_cmd_command_cmd_command )
+ # where $myapp_cmd_command_cmd_command == $self
+ }
+
+ 1;
+
=head1 DESCRIPTION
-TODO - will get refactored - using on own risk
+Works together with L<MooX::Options> for every command on its own, so options are
+parsed for the specific context and used for the instantiation:
+
+ myapp --argformyapp command --argformyappcmdcommand ...
=head1 SUPPORT

0 comments on commit 8e659d1

Please sign in to comment.