changeset 204:d63f9a92d6d4

+IMPL::Config::Include - simple way to include external config *IMPL::Web::Handler::TTView - finished template selecting mechanism (not tested)
author sergey
date Wed, 02 May 2012 17:42:47 +0400
parents 68a59c3358ff
children 891c04080658
files Lib/IMPL/Config.pm Lib/IMPL/Config/Include.pm Lib/IMPL/Web/Handler/TTView.pm
diffstat 3 files changed, 62 insertions(+), 38 deletions(-) [+]
line wrap: on
line diff
--- a/Lib/IMPL/Config.pm	Wed Apr 25 18:06:11 2012 +0400
+++ b/Lib/IMPL/Config.pm	Wed May 02 17:42:47 2012 +0400
@@ -16,6 +16,7 @@
 use IMPL::Serialization::XmlFormatter;
 
 our $ConfigBase ||= '';
+our $AppBase;
 
 sub LoadXMLFile {
     my ($self,$file) = @_;
@@ -120,6 +121,14 @@
     $_[0]->SUPER::get($_[1]) ? 1 : 0;
 }
 
+sub AppBase {
+	$AppBase
+}
+
+sub ConfigBase {
+	$ConfigBase
+}
+
 1;
 __END__
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Lib/IMPL/Config/Include.pm	Wed May 02 17:42:47 2012 +0400
@@ -0,0 +1,18 @@
+package IMPL::Config::Include;
+use strict;
+use warnings;
+use IMPL::require {
+    Conf => 'IMPL::Config',
+    Exception => 'IMPL::Exception'
+};
+
+
+sub restore {
+	my ($self,$data) = @_;
+	
+	die Exception->new("A file name is required") if ref $data || not $data;
+	
+	return Conf->spawn($data);
+}
+
+1;
\ No newline at end of file
--- a/Lib/IMPL/Web/Handler/TTView.pm	Wed Apr 25 18:06:11 2012 +0400
+++ b/Lib/IMPL/Web/Handler/TTView.pm	Wed May 02 17:42:47 2012 +0400
@@ -1,6 +1,7 @@
 package IMPL::Web::Handler::TTView;
 use strict;
 
+use List::Util qw(first);
 use IMPL::lang qw(:declare :constants);
 use IMPL::declare {
 	base => {
@@ -17,6 +18,7 @@
 	public property defaultDocument => PROP_ALL;
 	public property indexResource => PROP_ALL;
 	private property _selectorsCache => PROP_ALL;
+	private property _classTemplates => PROP_ALL;
 }
 
 sub CTOR {
@@ -31,7 +33,7 @@
 	my $result = $next ? $next->($action) : undef;
 	
 	my $doc = $this->loader->document(
-        'default',
+        $this->SelectView($action,ref $result),
         {
         	data => $result,
         	action => $action,
@@ -54,18 +56,21 @@
 	$last = $this->indexResource;
 	push @path,$last;
 	
+	$this->BuildCache unless $this->_selectorsCache;
 	my $cache = $this->_selectorsCache;
+	
+	foreach my $subclass ( $class ? (_GetHierarchy($class), '-default') : '-plain') {
+		my @results;
+		push @results, { data => $this->_classTemplates->{$subclass} } if $this->_classTemplates->{$subclass}; 
+		my $alternatives = [ { selectors => $cache->{$subclass}, immediate => 1 } ];
+		$alternatives = $this->MatchAlternatives($_,$alternatives,\@results) foreach @path;
 		
-	my $alternatives = [ map {
-            {selectors => $cache->{$_}, immediate => 1}
-        } grep $cache->{$_}, ( $class ? (_GetHierarchy($class), '-default') : '-plain' )
-    ];
-	
-	my @results;
-	
-	$alternatives = $this->MatchAlternatives($_,$alternatives,\@results) foreach @path;
-	
-	@results = sort { $b->{level} <=> $a->{level} } @results;	
+		if (@results) {
+			return shift sort { $b->{level} <=> $a->{level} } @results;
+		}
+	}
+		
+	return $this->defaultDocument;	
 }
 
 sub _GetHierarchy {
@@ -82,6 +87,9 @@
 	
 	my @selectors;
 	
+	my $cache = $this->_selectorsCache({});
+	$this->_classTemplates({});
+	
 	foreach my $selector ($this->selectors) {
 		if (not ref $selector) {
 			
@@ -89,8 +97,9 @@
 			
 			my @path = split(/\s+/,$path);
 			
-			my $class; 
+			my $class;
 			
+			# if this selector has a class part
 			if ($path[$#path-1] =~ m/^\@(.*)/) {
 				$class = $1;
 				shift @path;
@@ -98,39 +107,27 @@
 				$class = '-default';
 			}
 			
+			#if this selector has a path
 			if (@path) {
-			
 				@path = reverse @path;
 				my $last = pop @path;
+				my $t = ( $cache->{$class} ||= {} );
+				my $level = 1;
+		        foreach my $prim (@path ) {
+		            $t = ($t->{$prim}->{next} ||= {});
+		            $level ++;
+                }
+                $t->{$last}->{level} = $level;
+                $t->{$last}->{data} = $data;
 			
 			} else {
-				# todo
+				# we dont have a selector, only class
+				
+				$this->_classTemplates->{$class} = $data;
 			}
 			
 		}
 	}
-	
-	foreach my $selector(
-	    { path => [qw( foo bar )], data => 'teo' },
-	    { path => [qw( {x:.*} zoo bar )], data => 'view/{x}'},
-	    { path => [qw( foo >zoo >bar )], data => 'ilo' },
-	    { path => [qw( bar )], data => 'duo' },
-	    { path => [qw( wee )], data => 'iwy'},
-	    { path => [qw( foo wee )], data => 'fwy'},
-	    { path => [qw( {x:\w+} )], data => 'x:{x}'},
-	    { path => [qw( boo {x:\w+} )], data => 'boo/{x}'},
-	) {
-	    my $t = $tree;
-	    my @path = reverse @{$selector->{path}};
-	    my $last = pop @path;
-	    my $level = 1;
-	    foreach my $prim (@path ) {
-	        $t = ($t->{$prim}->{next} ||= {});
-	        $level ++;
-	    }
-	    $t->{$last}->{level} = $level;
-	    $t->{$last}->{data} = $selector->{data};
-	}
 }
 
 sub MatchAlternatives {
@@ -247,8 +244,8 @@
 shoes *      => product/list
 {action:*.} @My::Data::Product => product/{action}
 
-stuff/list => product/list
-/123/details => product/details
+stuff list => product/list
+details => product/details
 
 =end text