diff 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
line wrap: on
line diff
--- a/Lib/IMPL/Web/Application/ControllerUnit.pm	Fri Jun 18 16:27:28 2010 +0400
+++ b/Lib/IMPL/Web/Application/ControllerUnit.pm	Mon Jun 21 02:39:53 2010 +0400
@@ -39,6 +39,24 @@
 	$this->$_($args->{$_}) foreach qw(formData formSchema formErrors);
 }
 
+sub unitNamespace() {
+	""
+}
+
+sub transactions {
+	my ($self,%methods) = @_;
+	
+	while (my ($method,$info) = each %methods) {
+		if ($info and ref $info ne 'HASH') {
+			warn "Bad transaction $method description";
+			$info = {};
+		}
+		
+		$info->{wrapper} = 'TransactionWrapper';
+		$self->class_data(CONTROLLER_METHODS)->{$method} = $info;
+	}
+}
+
 sub forms {
 	my ($self,%forms) = @_;
 	
@@ -165,20 +183,31 @@
 	}
 }
 
-sub webMethod($$;$$) {
-	my ($name,$args,$body,$options) = @_;
+sub discover {
+	my ($this) = @_;
+	
+	my $methods = $this->class_data(CONTROLLER_METHODS);
+	
+	my $namespace = $this->unitNamespace;
+	(my $module = typeof $this) =~ s/^$namespace//;
 	
-	my %info = %$options;
-	$info{parameters} = $args;
-	$info{name} = $name;
-	$info{module} = scalar caller;
+	my %smd = (
+		module => [grep $_, split /::/, $module ],
+	);
 	
-	
+	while (my ($method,$info) = each %$methods) {
+		my %methodInfo = (
+			name => $method
+		);
+		$methodInfo{parameters} = $info->{parameters} if $info->{parameters};
+		push @{$smd{methods}},\%methodInfo; 
+	}
+	return \%smd;
 }
 
-public webMethod discover => sub {
-	
-}, { schema => 'some schema', returns => 'HASH' } ;
+__PACKAGE__->transactions(
+	discover => undef
+);
 
 1;
 
@@ -316,6 +345,25 @@
 Обертка для конструирования форм, может быть переопределен для конструирования контекста по
 своим правилам.
 
+=item C<discover()>
+
+Метод, опубликованный для вызова контроллером, возвращает описание методов в формате C<Simple Module Definition>.
+
+=begin code
+
+# SMD structure
+{
+	module => ['Foo','Bar'],
+	methods => [
+		{
+			name => 'search',
+			parameters => ['text','limit'] #optional
+		}
+	]
+}
+
+=end code
+
 =back
 
 =head1 EXAMPLE
@@ -328,34 +376,40 @@
 
 __PACKAGE__->PassThroughArgs;
 
-__PACKAGE__->transactions(qw(
-	find 
-	info
-));
+sub unitDataClass { 'My::Books' }
+
+__PACKAGE__->transactions(
+	find => {
+		parameters => [qw(author)]
+	},
+	info => {
+		parameters => [qw(id)]
+	}
+);
 __PACKAGE__->forms(
 	create => 'books.create.xml'
 );
 
 sub find {
-	my ($this) = @_;
+	my ($this,$author) = @_;
 	
-	return $this->application->dataSource->resultset('Books')->find({author => $this->query->param('author')});
+	return $this->ds->find({author => $author});
 }
 
 sub info {
-	my ($this) = @_;
+	my ($this,$id) = @_;
 	
-	return $this->application->dataSource->resultset('Books')->find({id => $this->query->param('id')});
+	return $this->ds->find({id => $id});
 }
 
 sub create {
 	my ($this) = @_;
 	
 	my %book = map {
-		$_ => $this->formData->selectSingleNode($_)->nodeValue
-	} qw(author_id title year ISBN);
+		$_->nodeName, $_->nodeValue
+	} $this->formData->selectNodes([qw(author_id title year ISBN)]);
 	
-	return $this->application->datasource->resultset('Books')->create(\%book);
+	return $this->ds->create(\%book);
 }
 
 =end code