comparison Lib/IMPL/Web/Application/ControllerUnit.pm @ 134:44977efed303

Significant performance optimizations Fixed recursion problems due converting objects to JSON Added cache support for the templates Added discovery feature for the web methods
author wizard
date Mon, 21 Jun 2010 02:39:53 +0400
parents a07a66fd8d5c
children c5bc900eefd3
comparison
equal deleted inserted replaced
133:a07a66fd8d5c 134:44977efed303
35 $this->action($action); 35 $this->action($action);
36 $this->application($action->application); 36 $this->application($action->application);
37 $this->query($action->query); 37 $this->query($action->query);
38 38
39 $this->$_($args->{$_}) foreach qw(formData formSchema formErrors); 39 $this->$_($args->{$_}) foreach qw(formData formSchema formErrors);
40 }
41
42 sub unitNamespace() {
43 ""
44 }
45
46 sub transactions {
47 my ($self,%methods) = @_;
48
49 while (my ($method,$info) = each %methods) {
50 if ($info and ref $info ne 'HASH') {
51 warn "Bad transaction $method description";
52 $info = {};
53 }
54
55 $info->{wrapper} = 'TransactionWrapper';
56 $self->class_data(CONTROLLER_METHODS)->{$method} = $info;
57 }
40 } 58 }
41 59
42 sub forms { 60 sub forms {
43 my ($self,%forms) = @_; 61 my ($self,%forms) = @_;
44 62
163 181
164 return IMPL::DOM::Schema->LoadSchema(File::Spec->catfile($vol,$dir,$name)); 182 return IMPL::DOM::Schema->LoadSchema(File::Spec->catfile($vol,$dir,$name));
165 } 183 }
166 } 184 }
167 185
168 sub webMethod($$;$$) { 186 sub discover {
169 my ($name,$args,$body,$options) = @_; 187 my ($this) = @_;
170 188
171 my %info = %$options; 189 my $methods = $this->class_data(CONTROLLER_METHODS);
172 $info{parameters} = $args; 190
173 $info{name} = $name; 191 my $namespace = $this->unitNamespace;
174 $info{module} = scalar caller; 192 (my $module = typeof $this) =~ s/^$namespace//;
175 193
176 194 my %smd = (
177 } 195 module => [grep $_, split /::/, $module ],
178 196 );
179 public webMethod discover => sub { 197
180 198 while (my ($method,$info) = each %$methods) {
181 }, { schema => 'some schema', returns => 'HASH' } ; 199 my %methodInfo = (
200 name => $method
201 );
202 $methodInfo{parameters} = $info->{parameters} if $info->{parameters};
203 push @{$smd{methods}},\%methodInfo;
204 }
205 return \%smd;
206 }
207
208 __PACKAGE__->transactions(
209 discover => undef
210 );
182 211
183 1; 212 1;
184 213
185 __END__ 214 __END__
186 215
314 =item C<FormWrapper($method,$action,$methodInfo)> 343 =item C<FormWrapper($method,$action,$methodInfo)>
315 344
316 Обертка для конструирования форм, может быть переопределен для конструирования контекста по 345 Обертка для конструирования форм, может быть переопределен для конструирования контекста по
317 своим правилам. 346 своим правилам.
318 347
348 =item C<discover()>
349
350 Метод, опубликованный для вызова контроллером, возвращает описание методов в формате C<Simple Module Definition>.
351
352 =begin code
353
354 # SMD structure
355 {
356 module => ['Foo','Bar'],
357 methods => [
358 {
359 name => 'search',
360 parameters => ['text','limit'] #optional
361 }
362 ]
363 }
364
365 =end code
366
319 =back 367 =back
320 368
321 =head1 EXAMPLE 369 =head1 EXAMPLE
322 370
323 =begin code 371 =begin code
326 use strict; 374 use strict;
327 use base qw(IMPL::Web::Application::ControllerUnit); 375 use base qw(IMPL::Web::Application::ControllerUnit);
328 376
329 __PACKAGE__->PassThroughArgs; 377 __PACKAGE__->PassThroughArgs;
330 378
331 __PACKAGE__->transactions(qw( 379 sub unitDataClass { 'My::Books' }
332 find 380
333 info 381 __PACKAGE__->transactions(
334 )); 382 find => {
383 parameters => [qw(author)]
384 },
385 info => {
386 parameters => [qw(id)]
387 }
388 );
335 __PACKAGE__->forms( 389 __PACKAGE__->forms(
336 create => 'books.create.xml' 390 create => 'books.create.xml'
337 ); 391 );
338 392
339 sub find { 393 sub find {
340 my ($this) = @_; 394 my ($this,$author) = @_;
341 395
342 return $this->application->dataSource->resultset('Books')->find({author => $this->query->param('author')}); 396 return $this->ds->find({author => $author});
343 } 397 }
344 398
345 sub info { 399 sub info {
346 my ($this) = @_; 400 my ($this,$id) = @_;
347 401
348 return $this->application->dataSource->resultset('Books')->find({id => $this->query->param('id')}); 402 return $this->ds->find({id => $id});
349 } 403 }
350 404
351 sub create { 405 sub create {
352 my ($this) = @_; 406 my ($this) = @_;
353 407
354 my %book = map { 408 my %book = map {
355 $_ => $this->formData->selectSingleNode($_)->nodeValue 409 $_->nodeName, $_->nodeValue
356 } qw(author_id title year ISBN); 410 } $this->formData->selectNodes([qw(author_id title year ISBN)]);
357 411
358 return $this->application->datasource->resultset('Books')->create(\%book); 412 return $this->ds->create(\%book);
359 } 413 }
360 414
361 =end code 415 =end code
362 416
363 =cut 417 =cut