[Jifty-commit] r7524 - in Template-Declare/branches/mixmaster: t

Jifty commits jifty-commit at lists.jifty.org
Wed Oct 7 17:14:07 EDT 2009


Author: theory
Date: Wed Oct  7 17:14:06 2009
New Revision: 7524

Modified:
   Template-Declare/branches/mixmaster/lib/Template/Declare.pm
   Template-Declare/branches/mixmaster/t/mixing.t

Log:
Updated `mix()` to handle the situation where the `into` and `under` arguments
are reversed.



Modified: Template-Declare/branches/mixmaster/lib/Template/Declare.pm
==============================================================================
--- Template-Declare/branches/mixmaster/lib/Template/Declare.pm	(original)
+++ Template-Declare/branches/mixmaster/lib/Template/Declare.pm	Wed Oct  7 17:14:06 2009
@@ -598,9 +598,9 @@
     mix Some::Other::Mixin       under '/otmix', set { name => 'Larry' };
     mix My::Mixin into My::View, under '/mymix';
 
-Sometimes you want to mix templates from one class into another class; C<mix>
-is your key to doing so. In the first example, if Some::Clever::Mixin creates
-templates named C<foo> and C<bar>, they will be mixed into the calling
+Sometimes you want to mix templates from one class into another class;
+C<mix()> is your key to doing so. In the first example, if Some::Clever::Mixin
+creates templates named C<foo> and C<bar>, they will be mixed into the calling
 template class as C<mixin/foo> and C<mixin/bar>.
 
 The second example mixes in the templates defined in Some::Other::Mixin into
@@ -623,7 +623,7 @@
 from Some::Other::Mixin, for example, will be able to access both
 C<mymixin/howdy> and C<howdy>.
 
-By default, C<mix> will mix templates into the class from which it's called.
+By default, C<mix()> will mix templates into the class from which it's called.
 But sometimes you might want to mix templates into some other template class.
 Such might be useful for end users to compose template structures from
 collections of template classes. In such a case, use the C<into> keyword to
@@ -631,7 +631,10 @@
 demonstrates this, where My::Mixin templates are mixed into My::View. Of
 course, you can still specify variables to set for those mixins.
 
-For those who prefer a direct OO syntax for mixins, just call C<mix> as a
+If you should happen to forget to pass the C<into> argument before C<under>,
+worry not, C<mix()> will figure it out and do the right thing.
+
+For those who prefer a direct OO syntax for mixins, just call C<mix()> as a
 method on the class to be mixed in. To replicate the above three exmaples
 without the use of the sugar:
 
@@ -643,16 +646,24 @@
 
 sub mix {
     my $mixin = shift;
-    my $into  = eval { $_[0]->isa(__PACKAGE__) } ? shift : caller(0);
-    $mixin->_import($into, @_);
+    my ($into, $under);
+    if ( eval { $_[0]->isa(__PACKAGE__) } ) {
+        ($into, $under) = (shift, shift);
+    } elsif ( eval { $_[1]->isa(__PACKAGE__) } ) {
+        ($under, $into) = (shift, shift);
+    } else {
+        $into  = caller(0);
+        $under = shift;
+    }
+    $mixin->_import($into, $under, @_);
 }
 
 =head2 into
 
   $class = into $class;
 
-C<into> is a helper method providing semantic sugar for the C<mix> method. All
-it does is return the name of the class on which it was called.
+C<into> is a helper method providing semantic sugar for the C<mix()> method.
+All it does is return the name of the class on which it was called.
 
 =cut
 
@@ -663,8 +674,8 @@
     alias Some::Clever::Mixin under '/mixin';
     alias Some::Other::Mixin  under '/mymix', { name => 'Larry' };
 
-Like C<mix>, but without support for the C<into> keyword. That is, it mixes
-templates into the calling template class. Deprecated in favor of C<mix>.
+Like C<mix()>, but without support for the C<into> keyword. That is, it mixes
+templates into the calling template class. Deprecated in favor of C<mix()>.
 
 =cut
 
@@ -674,9 +685,9 @@
 
     import_templates MyApp::Templates under '/something';
 
-Like C<mix>, but without support for the C<into> or C<set> keywords. That is,
-it mixes templates into the calling template class and does not support
-package variables for those mixins. Deprecated in favor of C<mix>.
+Like C<mix()>, but without support for the C<into> or C<set> keywords. That
+is, it mixes templates into the calling template class and does not support
+package variables for those mixins. Deprecated in favor of C<mix()>.
 
 =cut
 

Modified: Template-Declare/branches/mixmaster/t/mixing.t
==============================================================================
--- Template-Declare/branches/mixmaster/t/mixing.t	(original)
+++ Template-Declare/branches/mixmaster/t/mixing.t	Wed Oct  7 17:14:06 2009
@@ -48,11 +48,13 @@
 
 # Mix from outside the class.
 mix Wifty::UI::mixed_pkg into Wifty::UI, under '/mixed_pkg3';
+# And reverse.
+mix Wifty::UI::mixed_pkg under '/mixed_pkg4', into Wifty::UI
 
 # Fire it up.
 Template::Declare->init( dispatch_to => ['Wifty::UI'] );
 
-use Test::More tests => 24;
+use Test::More tests => 29;
 require "t/utils.pl";
 
 ok( Wifty::UI::mixed_pkg->has_template('mixed'), 'Mixed package should have template' );
@@ -95,6 +97,16 @@
 }
 
 {
+    # Try the fourth with `into` and `under` reversed.
+    ok my $simple = ( show('mixed_pkg4/mixed') ), 'Should get output from fourth mix';
+    like( $simple, qr'Invocant:', 'Its output should be right' );
+    unlike( $simple, qr'Varialble SET' , 'But the variable should not be set');
+    like( $simple, qr{'Wifty::UI::mixed_pkg'},
+        '$self is correct in template block' );
+    ok_lint($simple);
+}
+
+{
     ok my $simple = ( show('mixed_subclass_pkg/mixed') ),
         'Should get output from superclass template';
     like(


More information about the Jifty-commit mailing list