Start working on the new method, with tests.

Refactor out the object building code into a private method to be shared by
new and create.
This commit is contained in:
Colin Kuskie 2008-02-23 00:30:18 +00:00
parent b174ce51fb
commit 669bb870eb
2 changed files with 73 additions and 12 deletions

View file

@ -35,6 +35,32 @@ readonly className => my %className;
readonly shipperId => my %shipperId;
readonly options => my %options;
#-------------------------------------------------------------------
=head2 _buildObj ( )
Private method used to build objects, shared by new and create.
=cut
sub _buildObj {
my ($class, $session, $shipperId, $options) = @_;
my $self = {};
bless $self, $class;
register $self;
my $shipperId = $session->id->generate;
my $id = id $self;
$session{ $id } = $session;
$shipperId{ $id } = $shipperId;
$options{ $id } = $options;
$className{ $id } = $class;
return $self;
}
#-------------------------------------------------------------------
=head2 className ( )
@ -67,19 +93,10 @@ sub create {
my $options = shift;
croak "You must pass a hashref of options to create a new ShipDriver object"
unless defined($options) and ref $options eq 'HASH' and scalar keys %{ $options };
my $self = {};
bless $self, $class;
register $self;
my $shipperId = $session->id->generate;
my $id = id $self;
my $self = WebGUI::Shop::ShipDriver->_buildObj($session, $shipperId, $options);
$session{ $id } = $session;
$shipperId{ $id } = $shipperId;
$options{ $id } = $options;
$className{ $id } = __PACKAGE__;
$session->db->write('insert into shipper (shipperId,className) VALUES (?,?)', [$shipperId, $className{$id}]);
$session->db->write('insert into shipper (shipperId,className) VALUES (?,?)', [$shipperId, $class]);
$self->set($options);
return $self;
@ -168,6 +185,33 @@ sub getName {
#-------------------------------------------------------------------
=head2 new ( $session, $shipperId )
Looks up an existing ShipperDriver in the db by shipperId and returns
that object.
=cut
sub new {
my $class = shift;
my $session = shift;
croak "new requires a session object"
unless ref $session eq 'WebGUI::Session';
my $shipperId = shift;
croak "new requires a shipperId"
unless defined $shipperId;
my $properties = $session->db->quickHashRef('select * from shipper where shipperId=?',[$shipperId]);
croak "The requested shipperId does not exist in the db"
unless scalar keys %{ $properties };
croak "Somehow, the options property of this object, $shipperId, got broken in the db"
unless exists $properties->{options} and $properties->{options};
my $options = from_json($properties->{options});
my $self = WebGUI::Shop::ShipDriver->_buildObj($session, $shipperId, $options);
return;
}
#-------------------------------------------------------------------
=head2 options ( )
Accessor for the driver properties. This returns a hashref