blib/lib/App/GitFind/Runner.pm

filter: Statementstmt 100% 11 / 11 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 4 / 4 Podpod n/a totaltotal 100% 15 / 15 CC 1 SCAR 0.0
CC
1
Cov
100%
BEGIN
1.0
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« blib/lib/App/GitFind/Base.pm ↑ Summary blib/lib/App/GitFind/cmdline.pm »
1 package App::GitFind::Runner;
2  
3 1 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 our $VERSION = '0.000001';
8  
9 1 ▶use parent 'App::GitFind::Class';
Subroutine BEGIN: called CC=1 SCAR=0.0
10 use Class::Tiny qw(expr);
11  
12 # Imports
13 use App::GitFind::Base;
14 use App::GitFind::Actions qw(argdetails);
15 use App::GitFind::FileStatLs ();
16 use Getargs::Mixed;
17 use Git::Raw;
18  
19 # === Documentation === {{{1
20  
21 =head1 NAME
22  
23 App::GitFind::Runner - Test a file against a set of criteria
24  
25 =head1 SYNOPSIS
26  
27 my $hrArgs = App::GitFind::cmdline::Parse(\@ARGV);
28 my $runner = App::GitFind::Runner->new(-expr => $hrArgs->{expr});
29 $runner->process($some_entry_or_other);
30  
31 =cut
32  
33 # }}}1
34 # === Main interpreter === {{{1
35  
36 =head2 process
37  
38 Process a single file, represented as an L<App::GitFind::Entry> instance.
39 Returns the Boolean result of the expression. Note that the specific
40 exit codes from C<-exec> and similar actions are lost. Usage:
41  
42 $runner->process([-entry=>]$entry);
43  
44 =cut
45  
46 sub process {
47 my ($self, %args) = parameters('self',[qw(entry)], @_);
48 @_ = ($self, $args{entry}, $self->expr);
49 goto &_process;
50 } #process()
51  
52 # Internal: $self->_process($entry, $expr)
53 sub _process {
54 my ($self, $entry, $expr) = @_;
55 my $func; # What will handle the expression
56 my @arg; # Args to $func
57  
58 print "Processing ", ddc($expr) if $TRACE;
59  
60 if(ref $expr eq 'HASH') { # SEQ, AND, OR, NOT
61 die "Expression has more than one key! " . ddc($expr)
62 if scalar keys %{$expr} > 1;
63 my $operation = (keys %{$expr})[0];
64 $func = $self->can("process_$operation");
65 @arg = ($entry, $expr->{$operation});
66  
67 } else { # Basic elements
68 $func = $self->can("do_$expr");
69 @arg = $entry;
70 }
71  
72 die "I don't know how to process the expression: " . ddc($expr)
73 unless $func;
74  
75 return $self->$func(@arg);
76 } #_process()
77  
78 # }}}1
79 # === Logical operators === {{{1
80  
81 =head2 process_NOT
82  
83 Process a negation of a single expression. Usage:
84  
85 $runner->process_NOT($entry, $expr);
86  
87 Even though the name of the parameter is C<exprs> for consistency with
88 AND, OR, and SEQ, only a single expression is allowed.
89  
90 =cut
91  
92 sub process_NOT {
93 my ($self, $entry, $expr) = @_;
94 croak "I can't take an array of expressions"
95 if ref $expr eq 'ARRAY';
96  
97 return !$self->_process($entry, $expr);
98 } #process_NOT()
99  
100 =head2 process_AND
101  
102 Process a conjunction of expressions. Usage:
103  
104 $runner->process_AND($entry, [$expr1, ...]);
105  
106 =cut
107  
108 sub process_AND {
109 my ($self, $entry, $lrExprs) = @_;
110 my $retval;
111  
112 for(@$lrExprs) {
113 $retval = $self->_process($entry, $_);
114 last unless $retval; # Short-circuit
115 }
116 return $retval;
117 } #process_AND()
118  
119 =head2 process_OR
120  
121 Process a disjunction of expressions. Usage:
122  
123 $runner->process_OR($entry, [$expr1, ...]);
124  
125 =cut
126  
127 sub process_OR {
128 my ($self, $entry, $lrExprs) = @_;
129 my $retval;
130  
131 for(@$lrExprs) {
132 $retval = $self->_process($entry, $_);
133 last if $retval; # Short-circuit
134 }
135 return $retval;
136 } #process_OR()
137  
138 =head2 process_SEQ
139  
140 Process a sequence of expressions. Usage:
141  
142 $runner->process_SEQ($entry, [$expr1, ...]);
143  
144 =cut
145  
146 sub process_SEQ {
147 my ($self, $entry, $lrExprs) = @_;
148 my $retval;
149  
150 $retval = $self->_process($entry, $_) foreach @{$lrExprs};
151 return $retval;
152 } #process_SEQ()
153  
154 # }}}1
155 # === Tests/actions === {{{1
156  
157 sub do_true { true }
158  
159 sub do_false { false }
160  
161 sub do_print { say $_[1]; true } # $_[0] = self
162  
163 sub do_ls { print App::GitFind::FileStatLs::ls_stat($_[1]); true } # $_[0] = self
164  
165 # }}}1
166  
167 1; # End of App::GitFind::Runner
168 __END__
« blib/lib/App/GitFind/Base.pm ↑ Summary blib/lib/App/GitFind/cmdline.pm »