Scrappy/Action.pm untested

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