Changeset 183
- Timestamp:
- 05/04/06 00:17:15 (4 years ago)
- Files:
-
- 1 modified
-
trunk/lib/Data/ObjectDriver/BaseObject.pm (modified) (6 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/lib/Data/ObjectDriver/BaseObject.pm
r182 r183 58 58 die "Please specify a valid column for $parentclass" 59 59 } 60 # TBD Is column a composite key?61 60 62 61 # create a method name based on the column 63 62 if (! defined $method) { 64 $method = $column; 65 $method =~ s/_id$//; 66 $method .= "_obj"; 67 } 68 69 # die if we can't find a way to make a valid method 70 # TBD check current list of columns to avoid clash 71 if (! defined $method || ($method eq $column)) { 63 if (!ref($column)) { 64 $method = $column; 65 $method =~ s/_id$//; 66 $method .= "_obj"; 67 } elsif (ref($column) eq 'ARRAY') { 68 foreach my $col (@{$column}) { 69 $col =~ s/_id$//; 70 $method .= $col . '_'; 71 } 72 $method .= "obj"; 73 } 74 } 75 76 # die if we have clashing methods method 77 if (! defined $method || defined(*{"${class}::$method"})) { 72 78 die "Please define a valid method for $class->$column"; 73 79 } … … 79 85 my $cachekey = "__cache_$method"; 80 86 81 *{"${class}::$method"} = sub { 82 my $obj = shift; 83 unless (exists $obj->{$cachekey}) { 84 $obj->{$cachekey} = $parentclass->lookup($obj->column($column)); 85 weaken $obj->{$cachekey}; 86 } 87 return $obj->{$cachekey}; 88 }; 87 if (ref($column)) { 88 *{"${class}::$method"} = sub { 89 my $obj = shift; 90 unless (exists $obj->{$cachekey}) { 91 if (ref($column) eq 'ARRAY') { 92 $obj->{$cachekey} = $parentclass->lookup([ map{ $obj->{column_values}->{$_} } @{$column}]); 93 } else { 94 $obj->{$cachekey} = $parentclass->lookup($obj->{column_values}->{$column}); 95 } 96 weaken $obj->{$cachekey}; 97 } 98 return $obj->{$cachekey}; 99 }; 100 } else { 101 # array version 102 } 89 103 } else { 90 *{"${class}::$method"} = sub { 91 return $parentclass->lookup(shift()->column($column)); 92 }; 104 if (ref($column)) { 105 *{"${class}::$method"} = sub { 106 my $obj = shift; 107 return $parentclass->lookup([ map{ $obj->{column_values}->{$_} } @{$column}]); 108 }; 109 } else { 110 *{"${class}::$method"} = sub { 111 return $parentclass->lookup(shift()->{column_values}->{$column}); 112 }; 113 } 93 114 } 94 115 … … 100 121 $parent_method .= '_objs'; 101 122 } 102 *{"${parentclass}::$parent_method"} = sub { 103 my $obj = shift; 104 my $terms = shift; 105 my $args = shift; 106 # TBD - allow user defined extra terms here?... 107 # TBD - use primary_key_to_terms 108 return $class->search({$column => $obj->id}, $args); 123 if (ref($column)) { 124 *{"${parentclass}::$parent_method"} = sub { 125 my $obj = shift; 126 my $terms = shift || {}; 127 my $args = shift; 128 129 my $primary_key_tuple = $obj->primary_key_tuple; 130 my $primary_key = $obj->primary_key; 131 132 # inject pk search into given terms. 133 # composite key, ugh 134 foreach my $key (@{$primary_key_tuple}) { 135 $terms->{$key} = shift(@{$primary_key}); 136 } 137 138 return $class->search($terms, $args); 139 } 140 } else { 141 *{"${parentclass}::$parent_method"} = sub { 142 my $obj = shift; 143 my $terms = shift || {}; 144 my $args = shift; 145 # TBD - use primary_key_to_terms 146 $terms->{$column} = $obj->primary_key; 147 return $class->search($terms, $args); 148 } 109 149 }; 110 150 } # end of loop over class names … … 265 305 } 266 306 307 # set some values 267 308 if (@_) { 268 309 $obj->{column_values}->{$col} = shift; … … 431 472 the column is a singular key, an array ref if this is a composite key. 432 473 474 column => 'user_id' 475 column => ['user_id', 'photo_id'] 476 433 477 =item * method [OPTIONAL] 434 478 435 Name of the method to create in this class. Defaults to the column name without479 Name of the method to create in this class. Defaults to the column name(s) without 436 480 the _id suffix and with the suffix _obj appended. 437 481 … … 443 487 =item * cached [OPTIONAL] 444 488 445 If set to 1 then we will cache in-memory the resulting object inside this class. 489 If set to 1 cache the result of the fetching the parent object in the current class. Note 490 that this is a private copy to this class only, and does not interact with other caches 491 in the system. 446 492 447 493 =back
