RWDE/Interpreter.pm untested

filter: Statementstmt 0.0% 0 / 123 Branchbran 0.0% 0 / 32 Conditioncond 0.0% 0 / 29 MC/DCmcdc 0.0% 0 / 16 Subroutinesub 0.0% 0 / 10 Podpod 0.0% 0 / 5 totaltotal 0.0% 0 / 215 CC 17 SCAR 32.1
CC
17
Cov
0%
execute_loop
42.0
execute
42.0
run
20.0
CRAP 24.7
1 package RWDE::Interpreter;
2  
3 use strict;
4 use warnings;
5  
6 use Error qw(:try);
7 use RWDE::Exceptions;
8  
9 use vars qw($VERSION);
10 $VERSION = sprintf "%d", q$Revision: 507 $ =~ /(\d+)/;
11  
12 sub parser {
13 my ($self, $params) = @_;
14  
15 while () {
16 print "To start, enter the desired type of the object (i.e. LS::Account, empty string to finish): ";
17 my $type = <>;
18 chomp($type);
19 last if ($type eq '');
20  
21 print 'Now, enter desired id (empty for a static call): ';
22 my $term_id = <>;
23 chomp($term_id);
24  
25 my $term = $self->instantiate({ type => $type, term_id => $term_id });
26  
27 next if (not defined $term);
28  
29 $self->execute_loop({ term => $term });
30 }
31  
32 return ();
33 }
34  
35 sub instantiate {
36 my ($self, $params) = @_;
37  
38 my $type = $$params{type};
39 my $term_id = $$params{term_id};
40  
41 my $term;
42 if (defined $term_id && $term_id ne '' && $term_id ne '0') {
43 try {
44 $term = $type->fetch_by_id({ $type->get_id_name() => $term_id });
45 }
46  
47 catch Error with {
48 my $ex = shift;
49 print "An error occurred, info: $ex \n";
50 };
51  
52 }
53 else {
54 $term = $type;
55 }
56  
57 return $term;
58 }
59  
60 sub execute_loop {
61 my ($self, $params) = @_;
62  
63 my $term = $$params{term};
64  
65 while () {
66 my ($action_values, $action);
67 my $type = ref $term;
68  
69 if (ref $term) {
70 print "Enter desired function for $type({" . $term->get_id_name . " => " . $term->get_id . "})\n";
71 print "(blank for display, done to finish with this object): ";
72 }
73  
74 else {
75 $type = $term;
76 print "Enter desired function for $type\n";
77 print "(done to finish with this object): ";
78 }
79  
80 $action = <>;
81 chomp($action);
82  
83 last if ($action eq 'done');
84
85 if ($action eq '' && ref $term) {
86 $action = 'display';
87 }
88
89 elsif ($action eq ''){
90 print "No default action for static calls\n";
91 next;
92 }
93
94 else{
95 if (ref $term) {
96 print "Enter desired params for $type({" . $term->get_id_name() . " => " . $term->get_id() . "})->$action\n";
97 }
98
99 else {
100 print "Enter desired params for $type -> $action\n";
101 }
102
103 print "(key value pairs key => value separated by commas): ";
104  
105 $action_values = <>;
106 chomp($action_values);
107 }
108
109 $self->execute({ term => $term, action => $action, action_values => $action_values });
110 }
111  
112 return ();
113 }
114  
115 sub execute{
116 my ($self, $params) = @_;
117  
118 my $term = $$params{term};
119 my $type = ref $term || $term;
120 my $action = $$params{action};
121 my $action_values = $$params{action_values} or '';
122  
123 my $result;
124  
125 try {
126 my $action_params;
127 print "---------------------------Request--------\n";
128 if ((defined $action_values) && ($action_values =~ m/=/)){
129 #for hash style params, parse them
130 $action_values = "{$action_values}";
131 $action_params = RWDE::DB::Record->hashify({ string => "$action_values"});
132 }
133 else{
134 $action_params = $action_values;
135 }
136 print "$type->$action($action_values)\n";
137 print "---------------------------Action result--\n";
138 $result = $term->$action($action_params);
139 }
140  
141 catch Error with {
142 my $ex = shift;
143 print "An error occurred: $ex\n";
144 };
145  
146 if (defined $result){
147 print "Function returned: ($result)\n";
148 print "Boolean response would be: " . ($result ? 'true' : 'false') . "\n";
149 }
150 print "---------------------------End------------\n";
151  
152 #handle array results
153 if (ref $result eq 'ARRAY'){
154 print 'There are: ' . scalar @{$result} . " elements returned.\n";
155 }
156 elsif (ref $result){
157 $self->execute_loop({ term => $result });
158 }
159  
160 return;
161 }
162  
163 sub run{
164 my ($self,@params) = @_;
165 # Welcome to RWDE interpreter
166 # short and sweet conduit to the heart of your RWDE app
167 # this command line driven interface will allow you to run methods on specific objects
168  
169 my $type = shift @params;
170 my $action = shift @params;
171 my $term_id = shift @params;
172 my $action_values = shift @params;
173  
174 if (defined $type
175 && ($type eq '-h' || $type eq '-H' || $type eq '-?' || $type eq '-help')
176 ){
177 print "RWDE interpreter\n";
178 print "\nFor easy development or unit testing invoke the interpreter in non-interactive mode\n";
179 print "interpreter <class_type> <method_name> [<class_id|0 for static calls> [parameter,value]]\n\n";
180 print "ie. ./interpreter LS::List fetch_by_id 0 list_id,3\n\n";
181 print "ie. ./interpreter LS::List display 3\n\n";
182 print "Or start it without params to get the interactive 'shell;\n\n";
183 exit;
184 }
185 elsif (defined $type){
186  
187 my $term = RWDE::Interpreter->instantiate({ type => $type, term_id => $term_id });
188  
189 exit
190 unless defined $term;
191  
192 $action_values =~ s/,/ => /g;
193
194 RWDE::Interpreter->execute({ term => $term, action => $action, action_values => $action_values });
195  
196 exit;
197 }
198  
199 RWDE::Interpreter->parser();
200
201 return;
202 }
203
204 1;