lib/DB/CouchDB/Schema/Edit.pm untested

filter: Statementstmt 0.0% 0 / 191 Branchbran 0.0% 0 / 46 Conditioncond 0.0% 0 / 8 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 0.0% 0 / 19 Podpod 0.0% 0 / 14 totaltotal 0.0% 0 / 282 CC 24 SCAR 31.4
CC
24
Cov
0%
edit_view_func
72.0
test_view
30.0
_select_from_list
20.0
CRAP 23.1
1 package DB::CouchDB::Schema::Edit;
2 use DB::CouchDB::Schema;
3 use Moose;
4 use Term::ReadLine;
5 use File::Temp qw/tempfile tempdir/;
6 use Pod::Usage;
7  
8 has schema => (is => 'rw', isa => 'DB::CouchDB::Schema');
9 has term => (is => 'ro', default => sub {
10 return Term::ReadLine->new('CouchDB::Schema Editor');
11 }
12 );
13 has commands => (is => 'rw', isa => 'HashRef');
14 has view => (is => 'rw', isa => 'Str');
15 has func => (is => 'rw', isa => 'Str');
16  
17 sub BUILD {
18 my $self = shift;
19 my %commands = (
20 'Select Design Doc' => { ord => 1,
21 run => sub {
22 $self->select_design();
23 }
24 },
25 'Select View Func' => {ord => 2,
26 run => sub {
27 $self->select_view_func();
28 }
29 },
30 'Edit View Func' => {ord => 3,
31 run => sub {
32 $self->edit_view_func();
33 }
34 },
35 'Quit' => { ord => 100,
36 run => sub {
37 $self->quit();
38 }
39 }
40 );
41 $self->commands(\%commands);
42 }
43  
44 sub reset {
45 my $self = shift;
46 $self->{view} = undef;
47 $self->{func} = undef;
48 };
49  
50 sub select_design {
51 my $self = shift;
52 $self->{func} = undef;
53 my @views;
54 my $designnames = $self->schema()->get_views();
55 while (my $designname = $designnames->next_key()) {
56 my $viewdoc = $self->schema()->get($designname);
57 push @views, $designname;
58 }
59 my $view = $self->_select_from_list("Select a design doc:", @views);
60 $self->view($view);
61 $self->select_view_func();
62 }
63  
64 sub select_view_func {
65 my $self = shift;
66 if ($self->view()) {
67 my @funcs;
68 my $designdoc = $self->schema()->get($self->view());
69 for my $fname (keys %{$designdoc->{views}}) {
70 push @funcs, $fname;
71 }
72 my $funcname = $self->_select_from_list(
73 "Select a view function" => @funcs
74 );
75 $self->func($funcname);
76 $self->edit_view_func();
77 } else {
78 print STDERR "You have to select a Design Doc first",$/;
79 }
80 }
81  
82 sub edit_view_func {
83 my $self = shift;
84 my $viewobj = $self->schema->get($self->view());
85 my $viewfunc = $viewobj->{views}->{$self->func};
86 my $map;
87 my $reduce;
88 $map = $viewfunc->{map} if $viewfunc->{map};
89 $reduce = $viewfunc->{reduce} if $viewfunc->{reduce};
90 my $sel = $self->_select_from_list('Select:', 'map', 'reduce');
91 my ($fh, $name) = tempfile('tmpXXXXXXXXXX', SUFFIX => '.js');
92 my $editor = $ENV{EDITOR} || 'vim';
93 LOOP: while (1) {
94 if ($sel eq 'map') {
95 print $fh $map;
96 close $fh;
97 system("$editor $name");
98 open $fh, $name;
99 {
100 local $/;
101 $viewfunc->{map} = <$fh>;
102 }
103 unlink($name);
104 } else {
105 print $fh $reduce;
106 close $fh;
107 system("$editor $name");
108 open $fh, $name;
109 {
110 local $/;
111 $viewfunc->{reduce} = <$fh>;
112 }
113 unlink($name);
114 }
115 if (defined $viewfunc->{reduce} &&
116 $viewfunc->{reduce} eq '' ||
117 not defined $viewfunc->{reduce}) {
118 delete $$viewfunc{reduce};
119 }
120 if ($self->test_view($viewfunc)) {
121 $self->save_view($viewobj);
122 last LOOP;
123 } else {
124 my $continue;
125 $self->get_response('Stop Editing this view?[Y/n]: ', sub {
126 my $response = shift;
127 $continue = 1 if (lc $response eq 'n');
128 return 1;
129 });
130 redo LOOP if $continue;
131 last LOOP;
132 }
133 }
134 }
135  
136 sub save_view {
137 my $self = shift;
138 my $viewobj = shift;
139 my $result = $self->schema()->server->update_doc(
140 $viewobj->{_id} => $viewobj
141 );
142 }
143  
144 sub test_view {
145 my $self = shift;
146 my $viewfunc = shift;
147 my $save;
148 my $server = $self->schema()->server();
149 my $test = $server->temp_view($viewfunc);
150 if ($test->err) {
151 warn $test->errstr(), $/;
152 return;
153 }
154 VIEW: while (my $doc = $test->next()) {
155 print $server->json->encode($doc);
156 my $stop;
157 $self->get_response('Continue viewing results?[Y/n]', sub {
158 my $response = shift;
159 $stop = 1 if (lc $response eq 'n');
160 return 1;
161 });
162 last VIEW if ($stop);
163 }
164
165 $self->get_response('Save this view?[y/N]: ', sub {
166 my $response = shift;
167 $save = 1 if (lc $response eq 'y');
168 return 1;
169 });
170 return $save;
171 }
172  
173 sub _select_from_list {
174 my $self = shift;
175 my $prompt = shift;
176 my @list = @_;
177 print $prompt, $/;
178 my $counter = 0;
179 for my $item (@list) {
180 print $counter++ . " - " . $item, $/;
181 };
182 my $selection = shift;
183 $self->get_response('Enter a number or name(partials will work): ', sub {
184 my $request = shift;
185 #print STDERR "the request was $request", $/;
186 if ($request =~ /^\d$/) {
187 $selection = $list[$request];
188 #print STDERR "the selection was $selection", $/;
189 return 1 if $selection;
190 } else {
191 if (my ($item) = grep {$_ =~ /$request/i } @list) {
192 $selection = $item;
193 $self->term()->addhistory($item);
194 #print STDERR "the selection was $selection", $/;
195 return 1;
196 }
197 }
198 return;
199 });
200 return $selection;
201 };
202  
203 sub run {
204 my $self = shift;
205 if (!$self->schema()) {
206 $self->connect();
207 }
208 $self->process_commands();
209 $self->quit();
210 }
211  
212 sub show_meta {
213 my $self = shift;
214 print "Editing: ".
215 $self->schema()->server()->host() . "/" .
216 $self->schema()->server()->db();
217 print $/;
218 if ($self->view) {
219 print "Selected View: ". $self->view(),$/;
220 }
221 if ($self->func) {
222 print "Selected View Func: ". $self->func(),$/;
223 }
224 print $/;
225 }
226  
227 sub process_commands {
228 my $self = shift;
229 my $commands = $self->commands();
230 my @coms = sort { $commands->{$a}->{ord} <=> $commands->{$b}->{ord} }
231 keys %{$self->commands()};
232 while (1) {
233 $self->show_meta();
234 my $command = $self->_select_from_list("please choose an action:", @coms);
235 $self->commands()->{$command}->{run}->();
236 }
237 }
238  
239 sub connect {
240 my $self = shift;
241 my $hostname;
242 $self->get_response('Enter couchdb host[localhost]: ', sub {
243 $hostname = shift;
244 if (!$hostname) {
245 $hostname = 'localhost';
246 }
247 return 1;
248 }, 1);
249 my $port;
250 $self->get_response('Enter couchdb port[5984]: ', sub {
251 $port = shift;
252 $port = '5984' if (!$port);
253 });
254 my $db;
255 my $dblist = DB::CouchDB->new(host => $hostname, port => $port)
256 ->all_dbs();
257 my $db_name = $self->_select_from_list('Select a Database', @$dblist);
258 $self->schema(new DB::CouchDB::Schema->new(host => $hostname,
259 port => $port,
260 db => $db_name
261 ));
262 }
263  
264 sub get_response {
265 my $self = shift;
266 my $prompt = shift;
267 my $validator = shift;
268 my $add_history = shift;
269 print $/;
270 my $response = $self->term()->readline($prompt);
271 if (!$validator->($response)) {
272 $self->get_response($prompt, $validator);
273 } else {
274 $self->term()->addhistory($response) if $add_history;
275 }
276 }
277  
278 sub quit {
279 my $self = shift;
280 $self->get_response('Quit?[y/N]: ', sub {
281 my $quit = shift;
282 if (lc($quit) eq 'y') {
283 exit 0;
284 } else {
285 return 1;
286 }
287 });
288 }
289  
290 1;