diff Lib/IMPL/Web/View/TTContext.pm @ 371:d5c8b955bf8d

refactoring
author cin
date Fri, 13 Dec 2013 16:49:47 +0400
parents cbf4febf0930
children 1eca08048ba9
line wrap: on
line diff
--- a/Lib/IMPL/Web/View/TTContext.pm	Tue Dec 10 03:02:01 2013 +0400
+++ b/Lib/IMPL/Web/View/TTContext.pm	Fri Dec 13 16:49:47 2013 +0400
@@ -14,7 +14,9 @@
 	   TypeKeyedCollection => 'IMPL::TypeKeyedCollection',
 	   ArgException => '-IMPL::InvalidArgumentException',
 	   Resources => 'IMPL::Resources',
-	   Loader => 'IMPL::Code::Loader'
+	   Loader => 'IMPL::Code::Loader',
+	   MetadataBase => '-IMPL::Web::View::Metadata::BaseMeta',
+	   Metadata => 'IMPL::Web::View::Metadata::ObjectMeta'
 	},
 	base => [
 		'Template::Context' => '@_'
@@ -154,14 +156,15 @@
 	my $prefix = $this->prefix;
 	
 	my $info;
-	my $meta = $this->resolve_model($path,$args);
+	my $meta = $this->resolve_model($path,$args)
+		or return "[not found '$path']";
 	
-	$info->{prefix} = $prefix ? $prefix . '.' . $path : $path;
+	$info->{prefix} = join('.', grep($_, $prefix, $path));
 	$info->{model} = $meta->model;
 	$info->{metadata} = $meta;
 	
 	$template ||= $info->{template};
-	$template = $template ? $this->find_template($template) : $this->find_template_for($info->{model});
+	$template = $template ? $this->find_template($template) : $this->find_template_for($info->{metadata});
 	
 	return $this->render(
         $template,
@@ -184,25 +187,25 @@
         $args = shift;
     }
     
-    $args ||= {};
+    #copy
+    $args = { %{$args || {}} };
     
-    my $prefix = delete $args->{prefix} || $this->prefix;
-    
-    if (my $rel = delete $args->{rel}) {
-    	$prefix = $prefix ? "${prefix}.${rel}" : $rel;
+    $args->{prefix} = join('.',grep($_,$this->prefix,$args->{path}))
+    	unless defined $args->{prefix};
+    	
+    if (is($model,MetadataBase)) {
+    	$args->{model} = $model->model;
+    	$args->{metadata} = $model;
+    } else {
+    	$args->{model} = $model;
+    	$args->{metadata} = Metadata->GetMetadataForModel($model);
     }
     
-    $template = $template ? $this->find_template($template) : $this->find_template_for($model);
+    $template = $template ? $this->find_template($template) : $this->find_template_for($args->{metadata});
     
     return $this->render(
         $template,
-        hashApply(
-            {
-                prefix => $prefix,
-                model => $model,
-            },
-            $args
-        )
+        $args
     );
 }
 
@@ -325,6 +328,11 @@
 	   unless defined $prefix;
 	
 	my $meta = $this->metadata;
+	unless($meta) {
+		$meta = Metadata->GetMetadataForModel($this->model);
+		$this->metadata($meta);
+	}
+	
 	foreach my $part (grep length($_), split(/\.|\[(\d+)\]/, $prefix)) {
 		last unless $meta;
 		if ($part =~ /^\d+$/) {
@@ -340,6 +348,9 @@
 sub find_template_for {
 	my ($this,$meta, $nothrow) = @_;
 	
+	die ArgException->new(meta => 'An invalid metadata is supplied')
+		unless is($meta,MetadataBase);
+	
 	return $this->find_template($meta->template)
 		if ($meta->template);