Action.pm 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. package Scrappy::Action;
  2. BEGIN {
  3. $Scrappy::Action::VERSION = '0.94112090';
  4. }
  5. use Moose;
  6. use File::Find::Rule;
  7. # return a list of installed actions
  8. #has actions => (
  9. # is => 'ro',
  10. # isa => 'ArrayRef',
  11. # default => sub {
  12. # []
  13. # }
  14. #);
  15. # a hash list of installed actions
  16. has registry => (
  17. is => 'ro',
  18. isa => 'HashRef',
  19. default => sub {
  20. my $actions = {};
  21. foreach my $action (@{shift->actions}) {
  22. $actions->{$action} = $action;
  23. $actions->{lc($action)} = $action;
  24. }
  25. return $actions;
  26. }
  27. );
  28. sub actions {
  29. my @actions = ();
  30. my @files =
  31. File::Find::Rule->file()->name('*.pm')
  32. ->in(map {"$_/Scrappy/Action"} @INC);
  33. my %actions =
  34. map { $_ => 1 }
  35. map { s/.*(Scrappy[\\\/]Action[\\\/].*\.pm)/$1/; $_ } @files; #uniquenes
  36. for my $action (keys %actions) {
  37. my ($plug) = $action =~ /(Scrappy[\\\/]Action[\\\/].*)\.pm/;
  38. if ($plug) {
  39. $plug =~ s/\//::/g;
  40. push @actions, $plug;
  41. }
  42. }
  43. return [@actions];
  44. }
  45. sub load_action {
  46. my $self = shift;
  47. my $action = shift;
  48. unless ($action =~ /^Scrappy::Action::/) {
  49. # make fully-quaified action name
  50. $action = ucfirst $action;
  51. $action = join("::", map(ucfirst, split '-', $action))
  52. if $action =~ /\-/;
  53. $action = join("", map(ucfirst, split '_', $action))
  54. if $action =~ /\_/;
  55. $action = "Scrappy::Action::$action";
  56. }
  57. # check for a direct match
  58. if ($self->registry->{$action}) {
  59. return $self->registry->{$action};
  60. }
  61. # last resort seek
  62. elsif ($self->registry->{lc($action)}) {
  63. return $self->registry->{lc($action)};
  64. }
  65. return 0;
  66. }
  67. # execute an action from the cli
  68. sub execute {
  69. my ($class, $action_class, $action, @options) = @_;
  70. my $self = ref $class ? $class : $class->new;
  71. # show help on syntax error
  72. if (!$action_class || $action_class eq 'help') {
  73. with 'Scrappy::Action::Help';
  74. print $self->menu;
  75. print "\n";
  76. exit;
  77. }
  78. else {
  79. if ($action) {
  80. if ( $action eq 'meta'
  81. || $action eq 'registry'
  82. || $action eq 'actions'
  83. || $action eq 'load_action'
  84. || $action eq 'execute')
  85. {
  86. with 'Scrappy::Action::Help';
  87. print $self->menu;
  88. print "\n";
  89. exit;
  90. }
  91. }
  92. }
  93. # locate the action if installed
  94. my $requested_action = $self->load_action($action_class);
  95. if ($requested_action) {
  96. # load the desired action class
  97. with $requested_action;
  98. # is actoin available
  99. unless ($action) {
  100. print $self->help($requested_action);
  101. print "\n";
  102. exit;
  103. }
  104. # run the requested action
  105. print $self->meta->has_method($action)
  106. ? $self->$action(@options)
  107. : $self->help($requested_action);
  108. print "\n";
  109. }
  110. else {
  111. # ... or display the help menu
  112. with 'Scrappy::Action::Help';
  113. print $self->menu;
  114. print "\n";
  115. }
  116. }
  117. 1;