diff Lib/IMPL/Web/Application/RestResource.pm @ 201:0c018a247c8a

Reworked REST resource classes to be more transparent and intuitive
author sergey
date Tue, 24 Apr 2012 19:52:07 +0400
parents a9dbe534d236
children 5146e17a7b76
line wrap: on
line diff
--- a/Lib/IMPL/Web/Application/RestResource.pm	Tue Apr 24 02:34:49 2012 +0400
+++ b/Lib/IMPL/Web/Application/RestResource.pm	Tue Apr 24 19:52:07 2012 +0400
@@ -15,199 +15,106 @@
 		CustomResource => 'IMPL::Web::Application::CustomResource'
 	},
 	base => {
-		'IMPL::Web::Application::RestBaseResource' => '@_'
+		'IMPL::Web::Application::RestCustomResource' => '@_'
 	}
 };
 
 BEGIN {
 	public property target => PROP_GET | PROP_OWNERSET;
+	public property index => PROP_GET | PROP_OWNERSET;
+	public property fetch => PROP_GET | PROP_OWNERSET;
 	
 	public property methods => PROP_GET | PROP_OWNERSET;
 	
 	public property childRegex => PROP_GET | PROP_OWNERSET;
 	public property enableForms => PROP_GET | PROP_OWNERSET;
-	public property orphan => PROP_GET | PROP_OWNERSET;
 	
-	public property listChildren => PROP_GET | PROP_OWNERSET;
-	public property fetchChild => PROP_GET | PROP_OWNERSET;
-	public property createChild => PROP_GET | PROP_OWNERSET;
-	public property updateChild => PROP_GET | PROP_OWNERSET;
-	public property deleteChild => PROP_GET | PROP_OWNERSET;
 }
 
 sub CTOR {
 	my ($this) = @_;
 	
-	die ArgumentException->new("id","Identifier is required for non-root resources") if $this->id and not length $this->id;
 	die ArgumentException->new("target") unless $this->target;
-	die ArgumentException->new("A contract is required") unless $this->contract;
+	
+	$this->final($this->childRegex ? 0 : 1);
 	
 	if ($this->enableForms) {
-		
+        $this->methods->{create} = {
+        	get => \&_ParentGet,
+        	post => \&_ParentPost,
+        	final => 1 # this resource doesn't have any children
+        };
+        
+        $this->methods->{edit} = {
+        	get => \&_ParentGet,
+        	post => \&_ParentPut,
+        	final => 1 # this resource doesn't have any children
+        };
+        
+        $this->methods->{delete} {
+        	get => \&_ParentGet,
+        	post => \&_ParentDelete
+        	final => 1 # this resource doesn't have any children
+        }
 	}
 }
 
-sub GetImpl {
-    my ($this,$action) = @_;
-    
-    return $this->target;
-}
-
-sub PutImpl {
+sub _ParentGet {
 	my ($this,$action) = @_;
-	
-	die ForbiddenException->new() if $this->orhpan;
-	
-	$this->parent->UpdateImpl($this->id,$action);
+	return $this->parent->GetImpl($action);
 }
 
-sub PostImpl {
-	my ($this,$id,$action) = @_;
-	
-	my $method;
-	
-	if (length $id == 0) {
-		$method = $this->insert or die ForbiddenException->new();
-		
-		$method = {
-			method => $method,
-			parameters => [qw(query)]
-		} unless ref $method;
-	} elsif ($this->methods and $method = $this->methods->{$id}->{post}) {
-		# we got method info 
-	} else {
-		die ForbiddenException->new();
-	}
-	
-	return $this->InvokeMemeber($method,$id,$action);
+sub _ParentPut {
+    my ($this,$action) = @_;
+    return $this->parent->PutImpl($action);
 }
 
-sub DeleteImpl {
-	my ($this,$id,$action) = @_;
-	
-	my $rx = $this->childRegex;
-	if ($rx and $id =~ m/$rx/ and my $method = $this->delete) {
-		
-		$method = {
-			method => $method,
-			parameters => [qw(id)]
-		} unless ref $method;
-		
-		return $this->InvokeMember($method,$id,$action);
-	} else {
-		die ForbiddenException->new();
-	}
+sub _ParentPost {
+    my ($this,$action) = @_;
+    return $this->parent->PostImpl($action);
 }
 
-sub HttpFallbackImpl {
-	die ForbiddenException->new();
-}
-
-sub UpdateImpl {
-	my ($this,$id,$action) = @_;
-	
-	my $method = $this->updateChild or die ForbiddenException->new();
-	$this->InvokeMember($method,$action);
+sub _ParentDelete {
+    my ($this,$action) = @_;
+    return $this->parent->DeleteImpl($action);
 }
 
 sub FetchChildResource {
 	my ($this,$id,$action) = @_;
 	
 	my $rx = $this->childRegex;
-	my $method;
-	my %params = (
-	   parent => $this,
-	   id => $id
-	);
+	
+	my $res;
 	
 	if (length $id == 0) {
 		
-		$method = $this->list;
+		my $method = $this->index;
 		die ForbiddenException->new() unless $method;
 		
-		return $this->contract->Transform( $this->InvokeMember($method,$id,$action), \%params );
+		$res = $this->InvokeMember($method,$action);
 		
-	} elsif ($method = $this->methods->{$id}) {
-		# поскольку данный объект был получен не как дочерний объект,
-		# а как выполнение метода, то для него не определены операции
-		# put и delete по умолчанию.
-		$params{orphan} = 1;
-		
-		return $this->contract->Transform( $this->InvokeMember($method,$id,$action), \%params );
+	} elsif (my $resource = $this->methods->{$id}) {
+		return CustomResource->new(
+            get => $resource->{get},
+            post => $resource->{post},
+            put => $resource->{put},
+            delete => $resource->{delete},
+            parent => $this,
+            id => $id,
+            target => $this->target
+		);
 		 
 	} elsif ($rx and $id =~ m/^$rx$/ and $method = $this->fetch) {
-		# ok
+		$res = $this->InvokeMember($method,$action, { id => $id } );
 	} else {
 		die ForbiddenException->new();
 	}
 	
-	my $res = $this->InvokeMember($method,$id,$action);        
     die NotFoundException->new() unless defined $res;
         
     return $this->contract->Transform($res, {parent => $this, id => $id} );
 }
 
-sub InvokeMember {
-	my ($this,$method,$id,$action) = @_;
-	
-	die ArgumentException->new("method","No method information provided") unless $method;
-	
-	#normalize method info
-	if (not ref $method) {
-		$method = {
-			method => $method
-		};
-	}
-	
-	if (ref $method eq 'HASH') {
-		my $member = $method->{method} or die InvalidOpException->new("A member name isn't specified");
-		my @args;
-    
-	    if (my $params = $method->{parameters}) {
-	        if (ref $params eq 'HASH') {
-	            @args = map {
-	                $_,
-	                $this->MakeParameter($params->{$_},$id,$action)
-	            } keys %$params;                
-	        } elsif (ref $params eq 'ARRAY') {
-	            @args = map $this->MakeParameter($_,$id,$action), @$params;
-	        } else {
-	            @args = ($this->MakeParameter($params,$id,$action)); 
-	        }
-	    }
-		return $this->target->$member(@args);
-	} elsif (ref $method eq TResolve) {
-		return $method->Invoke($this->target);
-	} elsif (ref $method eq 'CODE') {
-		return $method->($this,$id,$action);
-	} else {
-		die InvalidOpException->new("Unsupported type of the method information", ref $method);
-	}
-}
-
-sub MakeParameter {
-	my ($this,$param,$id,$action) = @_;
-	
-	if ($param) {
-		if (is $param, TTransform ) {
-			return $param->Transform($this,$action->query);
-		} elsif ($param and not ref $param) {
-			my %std = (
-                id => $id,
-                action => $action,
-                query => $action->query
-			);
-			
-			return $std{$param} || $action->query->param($param);
-		}
-	} else {
-		return undef;
-	}
-}
-
-
-
-
 1;
 
 __END__