123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150 |
- package Scrappy::Action;
- BEGIN {
- $Scrappy::Action::VERSION = '0.94112090';
- }
- use Moose;
- use File::Find::Rule;
- # return a list of installed actions
- #has actions => (
- # is => 'ro',
- # isa => 'ArrayRef',
- # default => sub {
- # []
- # }
- #);
- # a hash list of installed actions
- has registry => (
- is => 'ro',
- isa => 'HashRef',
- default => sub {
- my $actions = {};
- foreach my $action (@{shift->actions}) {
- $actions->{$action} = $action;
- $actions->{lc($action)} = $action;
- }
- return $actions;
- }
- );
- sub actions {
- my @actions = ();
- my @files =
- File::Find::Rule->file()->name('*.pm')
- ->in(map {"$_/Scrappy/Action"} @INC);
- my %actions =
- map { $_ => 1 }
- map { s/.*(Scrappy[\\\/]Action[\\\/].*\.pm)/$1/; $_ } @files; #uniquenes
- for my $action (keys %actions) {
- my ($plug) = $action =~ /(Scrappy[\\\/]Action[\\\/].*)\.pm/;
- if ($plug) {
- $plug =~ s/\//::/g;
- push @actions, $plug;
- }
- }
- return [@actions];
- }
- sub load_action {
- my $self = shift;
- my $action = shift;
- unless ($action =~ /^Scrappy::Action::/) {
- # make fully-quaified action name
- $action = ucfirst $action;
- $action = join("::", map(ucfirst, split '-', $action))
- if $action =~ /\-/;
- $action = join("", map(ucfirst, split '_', $action))
- if $action =~ /\_/;
- $action = "Scrappy::Action::$action";
- }
- # check for a direct match
- if ($self->registry->{$action}) {
- return $self->registry->{$action};
- }
- # last resort seek
- elsif ($self->registry->{lc($action)}) {
- return $self->registry->{lc($action)};
- }
- return 0;
- }
- # execute an action from the cli
- sub execute {
- my ($class, $action_class, $action, @options) = @_;
- my $self = ref $class ? $class : $class->new;
- # show help on syntax error
- if (!$action_class || $action_class eq 'help') {
- with 'Scrappy::Action::Help';
- print $self->menu;
- print "\n";
- exit;
- }
- else {
- if ($action) {
- if ( $action eq 'meta'
- || $action eq 'registry'
- || $action eq 'actions'
- || $action eq 'load_action'
- || $action eq 'execute')
- {
- with 'Scrappy::Action::Help';
- print $self->menu;
- print "\n";
- exit;
- }
- }
- }
- # locate the action if installed
- my $requested_action = $self->load_action($action_class);
- if ($requested_action) {
- # load the desired action class
- with $requested_action;
- # is actoin available
- unless ($action) {
- print $self->help($requested_action);
- print "\n";
- exit;
- }
- # run the requested action
- print $self->meta->has_method($action)
- ? $self->$action(@options)
- : $self->help($requested_action);
- print "\n";
- }
- else {
- # ... or display the help menu
- with 'Scrappy::Action::Help';
- print $self->menu;
- print "\n";
- }
- }
- 1;
|