--- "Randal L. Schwartz" <[email protected]> wrote:
> And in a recent project where I transitioned a large CDBI project
> to RDBO, I ended up creating these. Came in very handy:
Looks useful, thanks.
> sub make_clone {
> ## clone the object except for primary keys and new items
> my $self = shift;
> my %updates = @_;
>
> my $new = (ref $self)->new;
> for my $column ($new->meta->columns) {
> my $getter = $column->accessor_method_name;
> my $setter = $column->mutator_method_name;
> if (exists $updates{$getter}) {
> $new->$setter($updates{$getter});
> } elsif ($column->is_primary_key_member) {
> # do nothing
> } else {
> $new->$setter($self->$getter);
> }
> }
> return $new;
> }
Ack! A 'do nothing' elsif branch? Much cleaner:
sub make_clone {
## clone the object except for primary keys and new items
my $self = shift;
my %updates = @_;
my $new = ( ref $self )->new;
for my $column ( $new->meta->columns ) {
next if $column->is_primary_key_member;
my $getter = $column->accessor_method_name;
my $setter = $column->mutator_method_name;
if ( exists $updates{$getter} ) {
$new->$setter( $updates{$getter} );
}
else {
$new->$setter( $self->$getter );
}
}
return $new;
}
Cheers,
Ovid
--
Buy the book -- http://www.oreilly.com/catalog/perlhks/
Perl and CGI -- http://users.easystreet.com/ovid/cgi_course/
-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys-and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV
_______________________________________________
Rose-db-object mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/rose-db-object