[Jifty-commit] r5949 - in JiftyX-ModelHelpers/trunk: lib/JiftyX

Jifty commits jifty-commit at lists.jifty.org
Mon Oct 20 00:05:04 EDT 2008


Author: gugod
Date: Mon Oct 20 00:05:02 2008
New Revision: 5949

Added:
   JiftyX-ModelHelpers/trunk/t/Simapp/t/m-helper.t
Modified:
   JiftyX-ModelHelpers/trunk/lib/JiftyX/ModelHelpers.pm
   JiftyX-ModelHelpers/trunk/t/Simapp/t/helpers.t

Log:
Add a M() function that'll be a better way to retreive record or
collection objects since the auto-created functions doesn't play well
sometime.

Instead of always exporting those auto-generated functions, add a
":auto" tag that exports them for now.


Modified: JiftyX-ModelHelpers/trunk/lib/JiftyX/ModelHelpers.pm
==============================================================================
--- JiftyX-ModelHelpers/trunk/lib/JiftyX/ModelHelpers.pm	(original)
+++ JiftyX-ModelHelpers/trunk/lib/JiftyX/ModelHelpers.pm	Mon Oct 20 00:05:02 2008
@@ -3,10 +3,41 @@
 use Exporter;
 
 our @ISA = qw(Exporter);
-our @EXPORT;
-our $VERSION = "0.01";
+our @EXPORT = qw(M);
+our $VERSION = "0.20";
 
-if (@EXPORT == 0) {
+sub M {
+    my ($model, @params) = @_;;
+    my $record = Jifty->app_class(Model => $model)->new;
+    if (@params) {
+        if (index($model, "Collection") > 0) {
+            my %params = (@params);
+            while (my ($k, $v) = each %params) {
+                $record->limit(column => $k, value => $v);
+            }
+        }
+        else {
+            $record->load_by_cols(@params);
+        }
+    }
+    return $record;
+}
+
+sub import {
+    my ($self, @tags) = @_;
+
+    if ($tags[0] eq ":auto") {
+        build_model_helpers();
+        pop @tags;
+    }
+
+    # Let the Exporter.pm do the heavy-liftingjobs
+    $Exporter::ExportLevel = 1;
+    Exporter::import($self, @tags);
+    $Exporter::ExportLevel = 0;
+}
+
+sub build_model_helpers {
     require Jifty::Schema;
     my @models = map { s/.*::(.+)$/$1/;  $_; } Jifty::Schema->new->models;
 
@@ -45,7 +76,9 @@
     }
 }
 
-"true, true.";
+<<;
+One
+
 
 =head1 NAME
 
@@ -53,7 +86,7 @@
 
 =head1 VERSION
 
-Version 0.01
+Version 0.20
 
 =head1 SYNOPSIS
 

Modified: JiftyX-ModelHelpers/trunk/t/Simapp/t/helpers.t
==============================================================================
--- JiftyX-ModelHelpers/trunk/t/Simapp/t/helpers.t	(original)
+++ JiftyX-ModelHelpers/trunk/t/Simapp/t/helpers.t	Mon Oct 20 00:05:02 2008
@@ -3,7 +3,7 @@
 use strict;
 
 use Jifty::Test::Dist tests => 3;
-use JiftyX::ModelHelpers;
+use JiftyX::ModelHelpers ':auto';
 
 {
     use Simapp::Model::Book;

Added: JiftyX-ModelHelpers/trunk/t/Simapp/t/m-helper.t
==============================================================================
--- (empty file)
+++ JiftyX-ModelHelpers/trunk/t/Simapp/t/m-helper.t	Mon Oct 20 00:05:02 2008
@@ -0,0 +1,27 @@
+#!/usr/bin/env perl
+use warnings;
+use strict;
+
+use Jifty::Test::Dist tests => 3;
+use JiftyX::ModelHelpers;
+
+{
+    use Simapp::Model::Book;
+    my $b = M("Book");
+    is( ref($b), "Simapp::Model::Book" );
+}
+
+my $good_book_id;
+{
+    my $b1 = Jifty->app_class(Model => "Book")->new;
+    $good_book_id = $b1->create( name => "Good Book A");
+
+    my $b2 = M("Book", name => "Good Book A");
+    is( $b2->id, $b1->id );
+}
+
+{
+    my $b = M("Book", id => $good_book_id);
+    is( $b->name, "Good Book A" );
+}
+


More information about the Jifty-commit mailing list