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:
parent
b174ce51fb
commit
669bb870eb
2 changed files with 73 additions and 12 deletions
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -29,7 +29,7 @@ my $session = WebGUI::Test->session;
|
|||
#----------------------------------------------------------------------------
|
||||
# Tests
|
||||
|
||||
my $tests = 15;
|
||||
my $tests = 18;
|
||||
plan tests => 1 + $tests;
|
||||
|
||||
#----------------------------------------------------------------------------
|
||||
|
|
@ -167,6 +167,23 @@ is($count, 0, 'delete deleted the object');
|
|||
|
||||
undef $driver;
|
||||
|
||||
#######################################################################
|
||||
#
|
||||
# new
|
||||
#
|
||||
#######################################################################
|
||||
|
||||
my $oldDriver;
|
||||
|
||||
eval { $oldDriver = WebGUI::Shop::ShipDriver->new(); };
|
||||
like ($@, qr/^new requires a session object/, 'new croaks without a session object');
|
||||
|
||||
eval { $oldDriver = WebGUI::Shop::ShipDriver->new($session); };
|
||||
like ($@, qr/^new requires a shipperId/, 'new croaks without a shipperId');
|
||||
|
||||
eval { $oldDriver = WebGUI::Shop::ShipDriver->new($session, 'notEverAnId'); };
|
||||
like ($@, qr/^The requested shipperId does not exist in the db/, 'new croaks unless the requested shipperId object exists in the db');
|
||||
|
||||
#######################################################################
|
||||
#
|
||||
# calculate
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue