From a7cc3696c6a61d53ceee57af241ec6db4d2a989d Mon Sep 17 00:00:00 2001 From: Colin Kuskie Date: Fri, 16 Jul 2010 09:01:58 -0700 Subject: [PATCH] Update Tax.pm for Moose. --- lib/WebGUI/Shop/Tax.pm | 49 ++++++++++++++++++++---------------------- t/Shop/Tax.t | 9 +++----- 2 files changed, 26 insertions(+), 32 deletions(-) diff --git a/lib/WebGUI/Shop/Tax.pm b/lib/WebGUI/Shop/Tax.pm index 165f3fc4b..8c90421c5 100644 --- a/lib/WebGUI/Shop/Tax.pm +++ b/lib/WebGUI/Shop/Tax.pm @@ -16,7 +16,7 @@ package WebGUI::Shop::Tax; use strict; -use Class::InsideOut qw{ :std }; +use Moose; use WebGUI::Exception::Shop; use WebGUI::Shop::Admin; use WebGUI::Pluggable; @@ -44,7 +44,23 @@ These subroutines are available from this package: =cut -readonly session => my %session; +has session => ( + is => 'ro', + required => 1, +); + +around BUILDARGS => sub { + my $orig = shift; + my $className = shift; + + ##Original arguments start here. + my $protoSession = $_[0]; + if (blessed $protoSession && $protoSession->isa('WebGUI::Session')) { + return $className->$orig(session => $protoSession); + } + return $className->$orig(@_); +}; + ##------------------------------------------------------------------- #sub appendSkuForm { @@ -127,8 +143,11 @@ A WebGUI::Session object. Required in class context, optional in instance contex sub getDriver { my $self = shift; - my $session = shift || $self->session; - + my $session = shift; + if (!$session && blessed $self) { + $session = $self->session; + } + WebGUI::Error::InvalidObject->throw( expected => "WebGUI::Session", got => (ref $session), error => "Need a session." ) unless $session && $session->isa( 'WebGUI::Session' ); @@ -144,28 +163,6 @@ sub getDriver { return $driver; } -#------------------------------------------------------------------- - -=head2 new ( $session ) - -Constructor for the WebGUI::Shop::Tax. Returns a WebGUI::Shop::Tax object. - -=cut - -sub new { - my $class = shift; - my $session = shift; - - WebGUI::Error::InvalidObject->throw( expected => "WebGUI::Session", got => (ref $session), error => "Need a session." ) - unless $session && $session->isa( 'WebGUI::Session' ); - - my $self = {}; - bless $self, $class; - register $self; - $session{ id $self } = $session; - return $self; -} - #------------------------------------------------------------------- diff --git a/t/Shop/Tax.t b/t/Shop/Tax.t index e5f1494b0..9af7e4b80 100644 --- a/t/Shop/Tax.t +++ b/t/Shop/Tax.t @@ -17,6 +17,7 @@ use FindBin; use strict; use lib "$FindBin::Bin/../lib"; use Test::More; +use Test::Exception; use WebGUI::Test; # Must use this before any other WebGUI modules use WebGUI::Session; @@ -28,7 +29,7 @@ my $session = WebGUI::Test->session; #---------------------------------------------------------------------------- # Tests -my $tests = 10; +my $tests = 9; plan tests => $tests + 1; # Add initial use_ok test @@ -45,11 +46,7 @@ SKIP: { # ####################################################################### - eval { my $tax = WebGUI::Shop::Tax->new( ) }; - - my $e = Exception::Class->caught(); - isa_ok( $e, 'WebGUI::Error::InvalidParam', 'new: throws error when no session object is passed' ); - is( $e->error, 'Need a session.', 'add: correct message for ommitted session object' ); + dies_ok { my $tax = WebGUI::Shop::Tax->new( ) } 'new: throws error when no session object is passed'; my $tax = WebGUI::Shop::Tax->new( $session );