Skip to content

Instantly share code, notes, and snippets.

@esobchenko
Created July 18, 2009 10:45
Show Gist options
  • Save esobchenko/149501 to your computer and use it in GitHub Desktop.
Save esobchenko/149501 to your computer and use it in GitHub Desktop.
simple persistent class template
package Foo;
# by Eugen Sobchenko, esobchenko@gmail.com
use base qw( Class::Accessor );
Foo->follow_best_practice;
Foo->mk_accessors( qw( id foo bar ) );
sub search_by {
# here's the code for searching objects by attribute;
# this method should return list of object ids.
}
sub retrieve {
# here's the code for loding object by id;
# this should return Foo object.
}
sub create {
# here's the code for creating objects in database;
# this should return id for newly created Foo object.
}
sub update {
# here's the code for updating objects in database.
}
sub delete {
# here's the code for deleting objects in database.
}
#
# SYNOPSIS for creating new object
#
use Foo;
my $obj = Foo->new(
foo => "baz",
bar => 1
);
$obj->create;
#
# SYNOPSIS for updating existent object
#
use Foo;
my $obj = Foo->retrieve( 123 );
$obj->set_foo("bazooka");
$obj->update;
#
# SYNOPSIS for deleting object
#
use Foo;
Foo->delete( 123 );
# or
Foo->retrieve( 1234 )->delete;
#
# SYNOPSIS for searching objects
#
use Foo;
my @ids = Foo->search_by( foo => "baz" );
foreach my $id (@ids) {
Foo->retrieve( $id )->delete;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment