changeset 226:b6cde007a175

Added resource contract
author sergey
date Wed, 05 Sep 2012 15:10:26 +0400
parents a1e868b0fba9
children 70ad6bc20908
files Lib/IMPL/Code/Binding.pm Lib/IMPL/Web/Application/ResourceContract.pm _test/Resources/resources.xsd _test/Resources/sample.xml
diffstat 4 files changed, 156 insertions(+), 49 deletions(-) [+]
line wrap: on
line diff
--- a/Lib/IMPL/Code/Binding.pm	Fri Aug 31 16:41:18 2012 +0400
+++ b/Lib/IMPL/Code/Binding.pm	Wed Sep 05 15:10:26 2012 +0400
@@ -25,9 +25,9 @@
         $text
     }
 CODE
-    my $body = eval $code; #$compiler_env->reval($code,'strict');
+    my $method = eval $code; #$compiler_env->reval($code,'strict');
     
-    
+    return $method;
 }
 
 1;
@@ -38,7 +38,7 @@
 
 =head1 NAME
 
-C<IMPL::Code::Binding> - превращает выражения в связыватель
+C<IMPL::Code::Binding> - превращает выражения в процедуру.
 
 =head1 SYNOPSIS
 
@@ -51,7 +51,8 @@
 my $person = DB->SearchPerson({name => 'Peter'})->First;
 
 my $bind = Binding->new(
-    [qw(obj)] => q{ $obj->addresses->[0]->country->code }
+    [qw(obj)] =>
+    q{ $obj->addresses->[0]->country->code }
 );
 
 print $bind->($person);
@@ -65,8 +66,7 @@
 
 Выражение параметризуется произвольным количеством именованных параметров,
 которые будут доступны внутри выражения как переменные. При создании связывателя
-в конструктор передается выражение связывания, ссылка насписок из параметров
-и могут быть переданы связи для копозиции.
+в конструктор передается выражение связывания, ссылка насписок из параметров.
 
 При создания связывателя будет проверен синтаксис, и если в выражении допущена
 ошибка, возникнет исключение.
@@ -76,7 +76,7 @@
 
 =head1 MEMBERS
 
-=head2 C<new($expression,\@vars,\%bindings)>
+=head2 C<new(\@vars,$expression)>
 
 Возвращает ссылку на процедуру.
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Lib/IMPL/Web/Application/ResourceContract.pm	Wed Sep 05 15:10:26 2012 +0400
@@ -0,0 +1,65 @@
+package IMPL::Web::Application::ResourceContract;
+use strict;
+use IMPL::lang qw(:declare);
+use IMPL::declare {
+	base => [
+	   'IMPL::Object' => undef
+	]
+};
+
+BEGIN {
+	public property operations => PROP_ALL;
+	private property _namedResources => PROP_ALL;
+	private property _regexpResources => PROP_ALL; 
+}
+
+1;
+
+__END__
+
+=pod
+
+=head1 NAME
+
+C<IMPL::Web::Application::ResourceContract> - описание ресурса
+
+=head1 SYNIOSIS
+
+=begin code
+
+use IMPL::require {
+	ResourceContract => 'IMPL::Web::Application::ResourceContract',
+	OperationContract => 'IMPL::Web::Application::OperationContract'
+};
+
+my $contract = ResourceContract->new(
+    operations => {
+    	get => OperationContract->new(
+            bind => sub {
+                return "Hello!";
+            }
+        )
+    },
+    resources => [
+        {
+        	name => 'info',
+        	bind => sub {
+        		return $_[0]->model->info;
+        	},
+        	contract => ResourceContract->new(
+        	   get => OperationContract->new(
+        	       bind => sub {
+        	       	   my ($resource,$model) = @_;
+        	       	   return $model; # or the same: $resource->model;
+        	       }
+        	   )
+        	)
+        }
+    ]
+)
+
+=end code 
+
+=head1 DESCRIPTION
+
+=cut
\ No newline at end of file
--- a/_test/Resources/resources.xsd	Fri Aug 31 16:41:18 2012 +0400
+++ b/_test/Resources/resources.xsd	Wed Sep 05 15:10:26 2012 +0400
@@ -14,7 +14,7 @@
 									<complexContent>
 										<extension base="tns:ResourceContract">
 											<attribute name="id" type="string" />
-											<attribute name="modelType" type="tns:ClassName"/>
+											<attribute name="modelType" type="tns:ClassName" />
 										</extension>
 									</complexContent>
 								</complexType>
@@ -43,9 +43,18 @@
 		<sequence>
 			<choice>
 				<element name="name" type="string" />
-				<element name="match" type="string" />
+				<element name="match">
+				    <complexType>
+				        <simpleContent>
+				            <extension base="string">
+				                <attribute name="vars" type="tns:VarList"/>
+				            </extension>
+				        </simpleContent>
+				    </complexType>
+				    
+				</element>
 			</choice>
-			<element name="model" minOccurs="0" type="tns:MethodBinding" />
+			<group ref="tns:BindingExpressions"/>
 			<choice>
 				<element name="contract">
 					<complexType>
@@ -71,25 +80,32 @@
 				minOccurs="0" />
 		</sequence>
 	</group>
+	
+	<group name="BindingExpressions">
+	   <choice>
+	       <element name="bind" type="tns:Binding"/>
+	       <element name="transform" type="tns:Transformation"/>
+	       <element name="value" type="string" />
+	   </choice>
+	</group>
 
 	<complexType name="OperationContract">
-		<complexContent>
-			<extension base="tns:MethodBinding">
-				<sequence>
-					<element name="response" minOccurs="0">
-					   <complexType>
-					       <choice>
-					           <element name="redirect" type="tns:RedirectResponse" />
-					           <element name="transform" type="tns:Transformation"/>
-					       </choice>
-					   </complexType>
-					</element>
-				</sequence>
-			</extension>
-		</complexContent>
+
+		<sequence>
+			<element name="bind" type="tns:Binding" />
+			<element name="response" minOccurs="0">
+				<complexType>
+					<choice>
+						<element name="redirect" type="tns:RedirectResponse" />
+						<element name="transform" type="tns:Transformation" />
+					</choice>
+				</complexType>
+			</element>
+		</sequence>
+
 	</complexType>
 
-	<complexType name="MethodBinding">
+	<complexType name="Binding">
 		<sequence>
 			<element name="var" type="tns:BindingVariable" minOccurs="0"
 				maxOccurs="unbounded" />
@@ -100,18 +116,20 @@
 
 	<complexType name="BindingVariable">
 		<choice>
-			<element name="bind" type="tns:MethodBinding" />
+			<element name="bind" type="tns:Binding" />
 			<element name="transform" type="tns:Transformation" />
+			<element name="value" type="string" />
 		</choice>
 		<attribute name="name" type="tns:IdentifierName" use="required" />
 	</complexType>
 
 
 	<complexType name="Transformation">
-	   <sequence>
-	       <element name="target" type="tns:MethodBinding" minOccurs="0"/>
-	       <element name="transform" type="tns:Transformation" minOccurs="0"/>
-	   </sequence>
+		<sequence>
+			<element name="target" type="tns:Binding" minOccurs="0" />
+			<element name="transform" type="tns:Transformation"
+				minOccurs="0" />
+		</sequence>
 		<attribute name="class" type="tns:ClassName" use="required" />
 	</complexType>
 
@@ -149,5 +167,11 @@
 			<pattern value="\w+" />
 		</restriction>
 	</simpleType>
+	
+	<simpleType name="VarList">
+	   <restriction base="string">
+	       <pattern value="(\w+)(\s*,\s*(\w+))*" />
+	   </restriction>
+	</simpleType>
 
 </schema>
\ No newline at end of file
--- a/_test/Resources/sample.xml	Fri Aug 31 16:41:18 2012 +0400
+++ b/_test/Resources/sample.xml	Wed Sep 05 15:10:26 2012 +0400
@@ -3,17 +3,24 @@
 	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 	xsi:schemaLocation="http://implab.org/schemas/resources resources.xsd ">
 	<root>
-		<get expr="$model" />
+		<get>
+			<bind expr="$model" />
+		</get>
 
 		<resource>
 			<name>projects</name>
-			<model expr="$model" />
-			<get expr="$model->projects" />
+			<bind expr="$model" />
+
+			<get>
+				<bind expr="$model" />
+			</get>
 
-			<post expr="$model->CreateProject($project)">
-				<var name="project">
-					<transform class="IMPL::Web::Transform::BindModel" />
-				</var>
+			<post>
+				<bind expr="$model->CreateProject($project)">
+					<var name="project">
+						<transform class="IMPL::Web::Transform::BindModel" />
+					</var>
+				</bind>
 				<response>
 					<redirect>
 						<location locator="projects-locator" />
@@ -22,32 +29,43 @@
 			</post>
 
 			<resource>
-				<match>(\w+)</match>
-				<model expr="$model->GetProject($resourceId)" />
+				<match vars="id">(\w+)</match>
+				<bind expr="$model->GetProject($id)" />
+
+				<get>
+					<bind expr="$model" />
+				</get>
 
-				<get expr="$model" />
-				<put expr="$model->update(data)">
-				    <var name="data">
-				        <transform class="IMPL::Web::Transform::BindModel" />
-				    </var>
+				<put>
+					<bind expr="$model->update(data)">
+						<var name="data">
+							<transform class="IMPL::Web::Transform::BindModel"/>
+						</var>
+					</bind>
 				</put>
 
 				<resource>
 					<name>library</name>
-					<model expr="$model" />
-					<get expr="$model" />
+					<bind expr="$model" />
+					<get>
+						<bind expr="$model" />
+					</get>
 				</resource>
 				<resource>
 					<name>sources</name>
-					<model expr="$model" />
-					<get expr="$model" />
+					<bind expr="$model" />
+					<get>
+						<bind expr="$model" />
+					</get>
 				</resource>
 			</resource>
 		</resource>
 	</root>
 	<contracts>
 		<contract id="project-contract" modelType="IMPL::Web::Model::Project">
-			<get expr="$model"/>
+			<get>
+				<bind expr="$model" />
+			</get>
 		</contract>
 	</contracts>
 </resources>
\ No newline at end of file