Author: spadkins
Date: Thu May 25 10:43:41 2006
New Revision: 6340

Modified:
   p5ee/trunk/App-Repository/lib/App/Repository.pm
   p5ee/trunk/App-Repository/t/DBI-repobjects.t

Log:
add support for qualified classes on a single table

Modified: p5ee/trunk/App-Repository/lib/App/Repository.pm
==============================================================================
--- p5ee/trunk/App-Repository/lib/App/Repository.pm     (original)
+++ p5ee/trunk/App-Repository/lib/App/Repository.pm     Thu May 25 10:43:41 2006
@@ -1105,16 +1105,21 @@
     my ($self, $table, $params, $cols, $options) = @_;
     die "get_object(): params undefined" if (!defined $params);
     my $tabledef = $self->{table}{$table};
-    my $class = $tabledef->{class} || "App::RepositoryObject";
-    App->use($class);
+
     my ($object);
-    if (ref($cols) eq "ARRAY" && $#$cols == -1 && !ref($params)) {
-        $object = {};
-    }
-    else {
+    #if (ref($cols) eq "ARRAY" && $#$cols == -1 && !ref($params)) {
+    #    $object = {};
+    #}
+    #else {
         $object = $self->get_hash($table, $params, $cols, $options);
-    }
+    #}
+
     if ($object) {
+        my $class = $tabledef->{class} || "App::RepositoryObject";
+        # if $class is an ARRAY ref, we need to examine the qualifier(s) to 
determine the class
+        $class = $self->_get_qualified_class($class, $object) if (ref($class));
+        App->use($class);
+
         $object->{_repository} = $self;
         $object->{_table} = $table;
         bless $object, $class;
@@ -1181,15 +1186,20 @@
     &App::sub_entry if ($App::trace);
     my ($self, $table, $params, $cols, $options) = @_;
     my $tabledef = $self->{table}{$table};
-    my $class = $tabledef->{class} || "App::RepositoryObject";
-    App->use($class);
     my $objects = $self->get_hashes($table, $params, $cols, $options);
     my $primary_key = $tabledef->{primary_key};
     $primary_key = [$primary_key] if (ref($primary_key) eq "");
-    my ($key);
+    my ($key, $class, %used);
     foreach my $object (@$objects) {
         $object->{_repository} = $self;
         $object->{_table} = $table;
+        $class = $tabledef->{class} || "App::RepositoryObject";
+        # if $class is an ARRAY ref, we need to examine the qualifier(s) to 
determine the class
+        $class = $self->_get_qualified_class($class, $object) if (ref($class));
+        if (!$used{$class}) {
+            App->use($class);
+            $used{$class} = 1;
+        }
         bless $object, $class;
         if ($primary_key) {
             $key = undef;
@@ -1214,6 +1224,28 @@
     return($objects);
 }
 
+sub _get_qualified_class {
+    &App::sub_entry if ($App::trace);
+    my ($self, $class_table, $object) = @_;
+    my ($class);
+    if (ref($class_table) eq "ARRAY") {
+        my ($qual, $qual_regexp, $qual_class);
+        foreach my $qual_condition (@$class_table) {
+            ($qual, $qual_regexp, $qual_class) = @$qual_condition;
+            next if (!$qual_class);
+            if ((!$qual) ||
+                (!$qual_regexp && ! $object->{$qual}) ||
+                ($object->{$qual} && $object->{$qual} =~ /$qual_regexp/)) {
+                $class = $qual_class;
+                last;
+            }
+        }
+    }
+    $class ||= "App::RepositoryObject";
+    &App::sub_exit($class) if ($App::trace);
+    return($class);
+}
+
 #############################################################################
 # get_hash_of_values_by_key()
 #############################################################################
@@ -1711,7 +1743,6 @@
     my ($self, $table, $cols, $row, $options) = @_;
 
     my $tabledef = $self->{table}{$table};
-    my $class = $tabledef->{class} || "App::RepositoryObject";
 
     my $ref = ref($cols);
     my ($object);
@@ -1731,6 +1762,9 @@
         $object = {};
     }
 
+    my $class = $tabledef->{class} || "App::RepositoryObject";
+    # if $class is an ARRAY ref, we need to examine the qualifier(s) to 
determine the class
+    $class = $self->_get_qualified_class($class, $object) if (ref($class));
     App->use($class);
     bless $object, $class;
     $object->_init();

Modified: p5ee/trunk/App-Repository/t/DBI-repobjects.t
==============================================================================
--- p5ee/trunk/App-Repository/t/DBI-repobjects.t        (original)
+++ p5ee/trunk/App-Repository/t/DBI-repobjects.t        Thu May 25 10:43:41 2006
@@ -12,6 +12,7 @@
     },
 );
 
+
 use Test::More qw(no_plan);
 use lib "../App-Context/lib";
 use lib "../../App-Context/lib";
@@ -20,6 +21,18 @@
 
 use App;
 use App::Repository;
+use App::RepositoryObject;
+
+package App::RepositoryObject::Man;
[EMAIL PROTECTED] = ("App::RepositoryObject");
+$VERSION = 0.01;
+
+package App::RepositoryObject::Woman;
[EMAIL PROTECTED] = ("App::RepositoryObject");
+$VERSION = 0.01;
+
+package main;
+
 use strict;
 
 if (!$App::options{dbuser}) {
@@ -40,6 +53,10 @@
                 dbpass => $App::options{dbpass},
                 table => {
                     test_person => {
+                        class => [
+                            [ "gender", "F", "App::RepositoryObject::Woman" ],
+                            # [ undef,  undef, "App::RepositoryObject::Man" ], 
 # otherwise Man
+                        ],
                         primary_key => ["person_id"],
                     },
                 },
@@ -108,6 +125,7 @@
 #####################################################################
 {
     my $obj = $rep->get_object("test_person", 1);
+    isa_ok($obj, "App::RepositoryObject", "stephen");
     $first_name = $obj->get("first_name");
     is($first_name, "stephen", "get() first_name [$first_name]");
     is($obj->set("first_name", "steve"),1,"set() first name [steve]");
@@ -129,6 +147,7 @@
 
 {
     my $obj = $rep->get_object("test_person", 2, []);
+    isa_ok($obj, "App::RepositoryObject", "susan");
     ok($obj->set(["first_name","age"], ["sue",38]), "set() 2 values");
     ($first_name, $age) = $obj->get(["first_name","age"]);
     is($first_name, "sue", "get() 2 values (checking 1 of 2)");
@@ -136,8 +155,15 @@
 }
 
 {
+    my $obj = $rep->get_object("test_person", 2);
+    isa_ok($obj, "App::RepositoryObject::Woman", "susan");
+}
+
+{
     my $objs = $rep->get_objects("test_person", {}, undef, {order_by => 
"person_id"});
     is($objs->[0]{_key}, 1, "get_objects() automatically set the _key");
+    isa_ok($objs->[0], "App::RepositoryObject", "by get_objects(), stephen");
+    isa_ok($objs->[1], "App::RepositoryObject::Woman", "by get_objects(), 
susan");
 }
 
 {
@@ -169,10 +195,11 @@
     is($obj3->{first_name},$obj->{first_name}, "new.first_name seems ok");
     is($obj3->{age},$obj->{age}, "new.age seems ok");
     is($obj3->{_key},$obj->{_key}, "new._key seems ok");
-    my $obj4 = $rep->new_object("test_person",{first_name => "christine"});
+    my $obj4 = $rep->new_object("test_person",{first_name => "christine", 
gender => "F"});
     is($obj4->{first_name},"christine", "new.first_name (2) seems ok");
     is($obj4->{_key},8, "new._key is ok");
     is($obj4->{person_id},8, "new.person_id is ok");
+    isa_ok($obj4, "App::RepositoryObject::Woman", "by new_object(), 
christine");
 }
 
 {

Reply via email to