[Jifty-commit] r7510 - in Template-Declare/trunk: . lib/Template

Jifty commits jifty-commit at lists.jifty.org
Tue Oct 6 15:16:13 EDT 2009


Author: theory
Date: Tue Oct  6 15:16:10 2009
New Revision: 7510

Added:
   Template-Declare/trunk/t/dispatch_order.t
Modified:
   Template-Declare/trunk/Changes
   Template-Declare/trunk/lib/Template/Declare.pm
   Template-Declare/trunk/t/importing.t

Log:
* Added `t/dispatch_order.t` to verify my understanding of how search ordering
  works.
* Added the `dispatch_to` parameter. The `roots` parameter is deprecated. I'll
  go through and update the docs and tests on the next commit to reflect this
  change.
* Added tests for `path_for()` regression to `t/importing.t`. There is now a
  TODO test that fails, and a test that passes right afterward. Once the issue
  is addressed, either the TODO test will start passing or the passing test
  will start failing. I think.


Modified: Template-Declare/trunk/Changes
==============================================================================
--- Template-Declare/trunk/Changes	(original)
+++ Template-Declare/trunk/Changes	Tue Oct  6 15:16:10 2009
@@ -2,6 +2,9 @@
 * Documented aliasing and template importing (mixins).
 * Reworked all the documentation, neatening things, fixing bugs in the
   examples, and adding missing docs for various functions and methods.
+* Added "dispatch_to" to replace "roots", which is now deprecated. Note that
+  "dispatch_to" resolves to template classes in the opposite order to "roots".
+  This won't be an issue if you only use a single temlate class.
 
 0.40_01 2009-08-12
 * Support for inline tagset definitions. Thanks to Olivier 'dolmen' Mengué

Modified: Template-Declare/trunk/lib/Template/Declare.pm
==============================================================================
--- Template-Declare/trunk/lib/Template/Declare.pm	(original)
+++ Template-Declare/trunk/lib/Template/Declare.pm	Tue Oct  6 15:16:10 2009
@@ -11,7 +11,7 @@
 our $VERSION = "0.40_01";
 
 use base 'Class::Data::Inheritable';
-__PACKAGE__->mk_classdata('roots');
+__PACKAGE__->mk_classdata('dispatch_to');
 __PACKAGE__->mk_classdata('postprocessor');
 __PACKAGE__->mk_classdata('aliases');
 __PACKAGE__->mk_classdata('alias_metadata');
@@ -21,7 +21,7 @@
 __PACKAGE__->mk_classdata('imported_into');
 __PACKAGE__->mk_classdata('around_template');
 
-__PACKAGE__->roots( [] );
+__PACKAGE__->dispatch_to( [] );
 __PACKAGE__->postprocessor( sub { return wantarray ? @_ : $_[0] } );
 __PACKAGE__->aliases(           {} );
 __PACKAGE__->alias_metadata(    {} );
@@ -41,6 +41,14 @@
 
 use vars qw/$TEMPLATE_VARS/;
 
+# Backwards-compatibility support.
+sub roots {
+    # warn "roots() has been deprecated; use dispatch_to() instead\n";
+    my $class = shift;
+    $class->dispatch_to( [ reverse @{ +shift } ] ) if @_;
+    return [ reverse @{ $class->dispatch_to } ];
+}
+
 =head1 NAME
 
 Template::Declare - Perlish declarative templates
@@ -258,7 +266,7 @@
     package main;
     use Template::Declare;
     Template::Declare->init(
-        roots         => ['MyApp::Templates'],
+        dispatch_to   => ['MyApp::Templates'],
         postprocessor => \&emphasize,
     );
 
@@ -377,6 +385,10 @@
         $class->roots( $args{'roots'} );
     }
 
+    if ( $args{'dispatch_to'} ) {
+        $class->dispatch_to( $args{'dispatch_to'} );
+    }
+
     if ( $args{'postprocessor'} ) {
         $class->postprocessor( $args{'postprocessor'} );
     }
@@ -520,7 +532,7 @@
     # If we're being called as a class method on T::D it means "search in any package"
     # Otherwise, it means search only in this specific package"
     if ( $self eq __PACKAGE__ ) {
-        @search_packages = reverse @{ Template::Declare->roots };
+        @search_packages = @{ Template::Declare->dispatch_to };
     } else {
         @search_packages = ($self);
     }

Added: Template-Declare/trunk/t/dispatch_order.t
==============================================================================
--- (empty file)
+++ Template-Declare/trunk/t/dispatch_order.t	Tue Oct  6 15:16:10 2009
@@ -0,0 +1,136 @@
+use warnings;
+use strict;
+
+##############################################################################
+package Wifty::Foo;
+use base qw/Template::Declare/;
+use Template::Declare::Tags;
+template hello => sub { outs 'hello from Foo' };
+
+##############################################################################
+package Wifty::Bar;
+use base qw/Template::Declare/;
+use Template::Declare::Tags;
+template hello => sub { outs 'hello from Bar' };
+
+##############################################################################
+package Wifty::Baz;
+use base qw/Template::Declare/;
+use Template::Declare::Tags;
+template hello => sub { outs 'hello from Baz' };
+
+##############################################################################
+package Wifty::Bip;
+use base qw/Template::Declare/;
+use Template::Declare::Tags;
+template hello => sub { outs 'hello from Bip' };
+
+##############################################################################
+package main;
+use Test::More tests => 16;
+
+# Check template resolution with the old `roots` attribute.
+ok !Template::Declare->init( roots => ['Wifty::Foo', 'Wifty::Bar'] ),
+    'init with Foo and Bar as roots';
+
+is +Template::Declare->show('hello'), 'hello from Bar',
+    'Bar should have precedence';
+
+# Check template resolution with the new `dispatch_to` attribute.
+ok !Template::Declare->init( dispatch_to => ['Wifty::Foo', 'Wifty::Bar'] ),
+    'init to dispatch to Foo and Bar';
+
+is +Template::Declare->show('hello'), 'hello from Foo',
+    'Foo should have precedence';
+
+##############################################################################
+# Import the Baz templates into Bar.
+package Wifty::Bar;
+import_templates Wifty::Baz under '/';
+
+##############################################################################
+package main;
+ok !Template::Declare->init( dispatch_to => ['Wifty::Foo', 'Wifty::Bar'] ),
+    'init to dispatch to Foo and Bar again';
+
+is +Template::Declare->show('hello'), 'hello from Foo',
+    'Foo should still have precedence';
+
+##############################################################################
+# Import the Baz templates into Foo.
+package Wifty::Foo;
+import_templates Wifty::Baz under '/';
+
+##############################################################################
+package main;
+ok !Template::Declare->init( dispatch_to => ['Wifty::Foo', 'Wifty::Bar'] ),
+    'init to dispatch to Foo and Bar one more time';
+
+is +Template::Declare->show('hello'), 'hello from Baz',
+    'Baz::hello should have replaced Foo::hello';
+
+# Now dispatch only to Bip and Foo.
+ok !Template::Declare->init( dispatch_to => ['Wifty::Bip', 'Wifty::Foo'] ),
+    'init to dispatch to Bip and Foo';
+
+is +Template::Declare->show('hello'), 'hello from Bip',
+    'Bip should now have precedence';
+
+##############################################################################
+# Now try the dame stuff with aliases.
+##############################################################################
+package Mifty::Foo;
+use base qw/Template::Declare/;
+use Template::Declare::Tags;
+template hello => sub { outs 'hello from Foo' };
+
+##############################################################################
+package Mifty::Bar;
+use base qw/Template::Declare/;
+use Template::Declare::Tags;
+template hello => sub { outs 'hello from Bar' };
+
+##############################################################################
+package Mifty::Baz;
+use base qw/Template::Declare/;
+use Template::Declare::Tags;
+template hello => sub { outs 'hello from Baz' };
+
+##############################################################################
+package Mifty::Bip;
+use base qw/Template::Declare/;
+use Template::Declare::Tags;
+template hello => sub { outs 'hello from Bip' };
+
+##############################################################################
+# Import the Baz templates into Bar.
+package Mifty::Bar;
+alias Mifty::Baz under '/';
+
+##############################################################################
+package main;
+ok !Template::Declare->init( dispatch_to => ['Mifty::Foo', 'Mifty::Bar'] ),
+    'init to dispatch to Mifty::Foo and Mifty::Bar';
+
+is +Template::Declare->show('hello'), 'hello from Foo',
+    'Mifty::Foo should have precedence';
+
+##############################################################################
+# Import the Baz templates into Foo.
+package Mifty::Foo;
+import_templates Mifty::Baz under '/';
+
+##############################################################################
+package main;
+ok !Template::Declare->init( dispatch_to => ['Mifty::Foo', 'Mifty::Bar'] ),
+    'init to dispatch to Mifty::Foo and Mifty::Bar again';
+
+is +Template::Declare->show('hello'), 'hello from Baz',
+    'Mifty::Baz::hello should have replaced Mifty::Foo::hello';
+
+# Now dispatch only to Bip and Foo.
+ok !Template::Declare->init( dispatch_to => ['Mifty::Bip', 'Mifty::Foo'] ),
+    'init to dispatch to Mifty::Bip and Mifty::Foo';
+
+is +Template::Declare->show('hello'), 'hello from Bip',
+    'Mifty::Bip should now have precedence';

Modified: Template-Declare/trunk/t/importing.t
==============================================================================
--- Template-Declare/trunk/t/importing.t	(original)
+++ Template-Declare/trunk/t/importing.t	Tue Oct  6 15:16:10 2009
@@ -1,6 +1,7 @@
 use warnings;
 use strict;
 
+##############################################################################
 package Wifty::UI::imported_pkg;
 use base qw/Template::Declare/;
 use Template::Declare::Tags;
@@ -10,10 +11,12 @@
     div { outs( 'This is imported from ' . $self ) };
 };
 
+##############################################################################
 package Wifty::UI::imported_subclass_pkg;
 use base qw/Wifty::UI::imported_pkg/;
 use Template::Declare::Tags;
 
+##############################################################################
 package Wifty::UI;
 use base qw/Template::Declare/;
 use Template::Declare::Tags;
@@ -37,29 +40,55 @@
 import_templates Wifty::UI::imported_pkg under '/imported_pkg';
 import_templates Wifty::UI::imported_subclass_pkg under '/imported_subclass_pkg';
 
+##############################################################################
+package Wifty::OtherUI;
+use base qw/Template::Declare/;
+use Template::Declare::Tags;
+import_templates Wifty::UI::imported_pkg under '/other_pkg';
+import_templates Wifty::UI::imported_subclass_pkg under '/other_subclass';
+
+##############################################################################
 package main;
 use Template::Declare::Tags;
 Template::Declare->init( roots => ['Wifty::UI'] );
 
-use Test::More tests => 14;
+use Test::More tests => 18;
+#use Test::More 'no_plan';
 require "t/utils.pl";
 
+# Visibility.
 ok( Wifty::UI::imported_pkg->has_template('imported'),
-    'Original template should be visible' );
+    'Original template should be visible in its own class' );
 ok( Wifty::UI::imported_subclass_pkg->has_template('imported'),
     'And be visible in a subclass');
+ok( !Template::Declare->has_template('imported'),
+    'But it should not be visible in Template::Declare');
+ok( !Wifty::UI->has_template('imported'),
+    'Nor in the packge it was imported into' );
 
 ok( Template::Declare->has_template('imported_pkg/imported'),
-    'Template should have been imported' );
+    'But it should be visible in its imported path' );
 ok( Template::Declare->has_template('imported_subclass_pkg/imported'),
-    'Superclass imports should be visible to subclasses');
+    'And it should be visible when imported from a subclass' );
 
+ok( !Template::Declare->has_template('other_pkg/imported'),
+    'The imported template should not be visible when imported into non-root package' );
+
+# Translate the path to where it was imported.
+TODO: {
+    local $TODO = 'path_for is confused', 1;
+    is(
+        Wifty::UI::imported_subclass_pkg->path_for('imported'),
+        '/imported_subclass_pkg/imported',
+        'The path for the imported template should be correct'
+    );
+}
 is(
     Wifty::UI::imported_subclass_pkg->path_for('imported'),
-    '/imported_subclass_pkg/imported',
-    'The path for the imported template should be correct'
+    '/other_subclass/imported',
+    'The imported template path should be correct for the last package it was imported into'
 );
-is( Wifty::UI->path_for('simple'), '/simple', 'Simple template shoudl be unimported' );
+is( Wifty::UI->path_for('simple'), '/simple', 'Simple template should be in the root path' );
 
 {
     ok my $simple = ( show('imported_pkg/imported') ), 'Should get output for imported template';


More information about the Jifty-commit mailing list