Navigation Menu

Skip to content

Commit

Permalink
Finish C::IO cleanup, strip out create guts.
Browse files Browse the repository at this point in the history
  • Loading branch information
perlDreamer committed Jul 8, 2010
1 parent 4cbbed3 commit 9ebcc0a
Show file tree
Hide file tree
Showing 2 changed files with 116 additions and 61 deletions.
165 changes: 115 additions & 50 deletions lib/WebGUI/Shop/AddressBook.pm
Expand Up @@ -3,6 +3,25 @@ package WebGUI::Shop::AddressBook;
use strict;

use Class::InsideOut qw{ :std };
use Moose;
use WebGUI::Definition;

property 'userId' => (
noFormPost => 1,
default => '',
);

property 'defaultAddressId' => (
noFormPost => 1,
default => '',
);

has [ qw/addressBookId session/] => (
is => 'ro',
required => 1,
);


use JSON;
require WebGUI::Asset::Template;
use WebGUI::Exception::Shop;
Expand Down Expand Up @@ -31,9 +50,95 @@ These subroutines are available from this package:
=cut

readonly session => my %session;
private properties => my %properties;
private addressCache => my %addressCache;
#-------------------------------------------------------------------

=head2 new ( $session, $cartId )
Constructor. Instanciates an address book based upon a cartId.
=head2 new ( $session )
Constructor. Builds a new, default address book object.
=head2 new ( $properties )
Constructor. Builds a new, default address book object in Moose style with default properties set by $properties. This does not
persist them to the database automatically. This needs to be done via $self->write.
=head3 $session
A reference to the current session.
=head3 $addressBookId
The unique id of a cart to instanciate.
=head3 $properties
A hash reference that contains one or more of the following:
=head4 defaultAddressId
The unique id for a address attached to this cart.
=head4 userId
The unique id for the user who owns this cart.
=cut


around BUILDARGS => sub {
my $orig = shift;
my $class = shift;
if (ref $_[0] eq 'HASH') {
my $properties = $_[0];
my $session = $properties->{session};
if (! (blessed $session && $session->isa('WebGUI::Session')) ) {
WebGUI::Error::InvalidObject->throw(expected=>"WebGUI::Session", got=>(ref $session), error=>"Need a session.");
}
my ($addressBookId) = $class->_init($session);
$properties->{addressBookId} = $addressBookId;
return $class->$orig($properties);
}
my $session = shift;
if (! (blessed $session && $session->isa('WebGUI::Session'))) {
WebGUI::Error::InvalidObject->throw(expected=>"WebGUI::Session", got=>(ref $session), error=>"Need a session.");
}
my $argument2 = shift;
if (!defined $argument2) {
my ($addressBookId) = $class->_init($session);
my $properties = {};
$properties->{session} = $session;
$properties->{addressBookId} = $addressBookId;
return $class->$orig($properties);
}
##Look up one in the db
my $book = $session->db->quickHashRef("select * from addressBook where addressBookId=?", [$argument2]);
if ($book->{addressBookId} eq "") {
WebGUI::Error::ObjectNotFound->throw(error=>"No such address book.", id=>$argument2);
}
$book->{session} = $session;
return $class->$orig($book);
};

#-------------------------------------------------------------------

=head2 _init ( session )
Builds a stub of object information in the database, and returns the newly created
cartId, and the creationDate fields so the object can be initialized correctly.
=cut

sub _init {
my $class = shift;
my $session = shift;
my $addressBookId = $session->id->generate;
$session->db->write('insert into addressBook (addressBookId) values (?)', [$addressBookId]);
return ($addressBookId);
}


#-------------------------------------------------------------------

Expand Down Expand Up @@ -154,7 +259,6 @@ sub delete {
my ($self) = @_;
my $myId = id $self;
foreach my $address (@{$self->getAddresses}) {
delete $addressCache{$myId}{$address->getId};
$address->delete;
}
$self->session->db->write("delete from addressBook where addressBookId=?",[$self->getId]);
Expand All @@ -181,28 +285,6 @@ sub formatCallbackForm {

#-------------------------------------------------------------------

=head2 get ( [ property ] )
Returns a duplicated hash reference of this object’s data.
=head3 property
Any field − returns the value of a field rather than the hash reference. See the
C<update> method.
=cut

sub get {
my ($self, $name) = @_;
if (defined $name) {
return $properties{id $self}{$name};
}
my %copyOfHashRef = %{$properties{id $self}};
return \%copyOfHashRef;
}

#-------------------------------------------------------------------

=head2 getAddress ( id )
Returns an address object.
Expand All @@ -215,11 +297,10 @@ An address object's unique id.

sub getAddress {
my ($self, $addressId) = @_;
my $id = id $self;
unless (exists $addressCache{$id}{$addressId}) {
$addressCache{$id}{$addressId} = WebGUI::Shop::Address->new($self, $addressId);
unless (exists $self->{_addressCache}->{$addressId}) {
$self->{_addressCache}->{$addressId} = WebGUI::Shop::Address->new($self, $addressId);
}
return $addressCache{$id}{$addressId};
return $self->{_addressCache}->{$addressId};
}

#-------------------------------------------------------------------
Expand Down Expand Up @@ -465,31 +546,15 @@ sub processAddressForm {

#-------------------------------------------------------------------

=head2 update ( properties )
Sets properties in the addressBook
=head2 write ( )
=head3 properties
A hash reference that contains one of the following:
=head4 userId
Assign the user that owns this address book.
=head4 defaultAddressId
The id of the address to be made the default for this address book.
Writes the object properties to the database.
=cut

sub update {
sub write {
my ($self, $newProperties) = @_;
my $id = id $self;
foreach my $field (qw(userId defaultAddressId)) {
$properties{$id}{$field} = (exists $newProperties->{$field}) ? $newProperties->{$field} : $properties{$id}{$field};
}
$self->session->db->setRow("addressBook","addressBookId",$properties{$id});
$self->session->db->setRow("addressBook","addressBookId",$self->get());
}

#-------------------------------------------------------------------
Expand Down
12 changes: 1 addition & 11 deletions lib/WebGUI/Shop/Cart.pm
Expand Up @@ -89,11 +89,6 @@ These subroutines are available from this package:
=cut

#readonly session => my %session;
#private properties => my %properties;
#public error => my %error;
#private addressBookCache => my %addressBookCache;

#-------------------------------------------------------------------

=head2 new ( $session, $cartId )
Expand Down Expand Up @@ -326,12 +321,7 @@ A reference to the current session.

sub create {
my ($class, $session) = @_;
unless (defined $session && $session->isa("WebGUI::Session")) {
WebGUI::Error::InvalidObject->throw(expected=>"WebGUI::Session", got=>(ref $session), error=>"Need a session.");
}
my $cartId = $session->id->generate;
$session->db->write('insert into cart (cartId, sessionId, creationDate) values (?,?,UNIX_TIMESTAMP())', [$cartId, $session->getId]);
return $class->new($session, $cartId);
return $class->new($session);
}

#-------------------------------------------------------------------
Expand Down

0 comments on commit 9ebcc0a

Please sign in to comment.