[Jifty-commit] r4034 - in Jifty-DBI/trunk: lib/Jifty/DBI/Record

jifty-commit at lists.jifty.org jifty-commit at lists.jifty.org
Sun Sep 2 22:32:21 EDT 2007


Author: sterling
Date: Sun Sep  2 22:32:21 2007
New Revision: 4034

Modified:
   Jifty-DBI/trunk/   (props changed)
   Jifty-DBI/trunk/lib/Jifty/DBI/Record/Plugin.pm

Log:
 r11131 at dynpc145:  andrew | 2007-09-02 21:32:04 -0500
 Added documentation for using @EXPORT with mixins.


Modified: Jifty-DBI/trunk/lib/Jifty/DBI/Record/Plugin.pm
==============================================================================
--- Jifty-DBI/trunk/lib/Jifty/DBI/Record/Plugin.pm	(original)
+++ Jifty-DBI/trunk/lib/Jifty/DBI/Record/Plugin.pm	Sun Sep  2 22:32:21 2007
@@ -15,6 +15,11 @@
   package MyApp::FavoriteColor;
   use base qw/ Jifty::DBI::Record::Plugin /;
 
+  # Define which methods you want to put in the host model
+  our @EXPORT = qw(
+      favorite_complementary_color
+  );
+
   use Jifty::DBI::Schema;
   use Jifty::DBI::Record schema {
       column favorite_color =>
@@ -23,6 +28,16 @@
           valid_values are qw/ red green blue yellow /;
   };
 
+  sub favorite_complementary_color {
+      my $self = shift; # whatever host object thing we've mixed with
+      my $color = $self->favorite_color;
+      return $color eq 'red'    ? 'green'
+           : $color eq 'green'  ? 'red'
+           : $color eq 'blue'   ? 'orange'
+           : $color eq 'yellow' ? 'purple'
+           :                      undef;
+  }
+
   # Use the mixin
   package MyApp::Model::User;
 
@@ -44,6 +59,14 @@
       return "The favorite color of $name is $color.";
   }
 
+  sub name_and_complementary_color {
+      my $self  = shift;
+      my $name  = $self->name;
+      my $color = $self->favorite_complementary_color;
+
+      return "The complement of $name's favorite color is $color.";
+  }
+
 =head1 DESCRIPTION
 
 By using this package you may provide models that are built from one or more mixins. In fact, your whole table could be defined in the mixins without a single column declared within the model class itself.
@@ -63,6 +86,27 @@
           default is 'african';
   };
 
+=head3 @EXPORT
+
+A mixin may define an C<@EXPORT> variable, which works exactly as advertised in L<Exporter>. That is, given the name of any methods or variable names in the mixin, the host model will gain those methods. 
+
+  our @EXPORT = qw( autocomplete_swallow_type );
+
+  sub autocomplete_swallow_type {
+      my $self  = shift;
+      my $value = quotemeta(shift);
+
+      # You should probably find a better way than actually doing this...
+
+      my @values;
+      push @values, 'african'  if 'african'  =~ /$value/;
+      push @values, 'european' if 'european' =~ /$value/;
+
+      return @values;
+  }
+
+That way if you have any custom methods you want to throw into the host model, just define them in the mixin and add them to the C<@EXPORT> variable.
+
 =head3 register_triggers
 
 Your mixin may also want to register triggers for the records to which it will be added. You can do this by defining a method named C<register_triggers>:


More information about the Jifty-commit mailing list