Add default i18n label, API converted over to Moose. Need to run tests, convert definition based code in the UI.

This commit is contained in:
Colin Kuskie 2010-09-24 16:10:30 -07:00
parent 068c2d5851
commit bab17655c2
2 changed files with 123 additions and 133 deletions

View file

@ -54,7 +54,7 @@ These subroutines are available from this package:
=cut =cut
define tableName => 'paymentGateway'; define tableName => 'paymentGateway';
define pluginName => 'Payment Driver'; define pluginName => ['Payment Driver', 'PayDriver'];
property label => ( property label => (
fieldType => 'text', fieldType => 'text',
@ -76,10 +76,45 @@ property groupToUse => (
); );
has [ qw/session paymentGatewayId/ ] => ( has [ qw/session paymentGatewayId/ ] => (
is => ro, is => 'ro',
required => 1, required => 1,
); );
around BUILDARGS => sub {
my $orig = shift;
my $class = shift;
if(ref $_[0] eq 'HASH') {
##Standard Moose invocation for creating a new object
return $class->$orig(@_);
}
my $session = shift;
WebGUI::Error::InvalidParam->throw(error => q{Must provide a session variable})
unless blessed $session && $session->isa('WebGUI::Session');
if (ref $_[0] eq 'HASH') {
##Create an object from a hashref of options
my $options = shift;
$options->{session} = $session;
$options->{paymentGatewayId} = $session->id->generate;
return $class->$orig($options);
}
##Must be a paymentGatewayId, look it up in the database
my $paymentGatewayId = shift;
WebGUI::Error::InvalidParam->throw(error => q{Must provide a paymentGatewayId})
unless defined $paymentGatewayId;
my $properties = $session->db->quickHashRef('select * from paymentGateway where paymentGatewayId=?', [
$paymentGatewayId,
]);
WebGUI::Error::ObjectNotFound->throw(error => q{paymentGatewayId not found in db}, id => $paymentGatewayId)
unless scalar keys %{ $properties };
croak "Somehow, the options property of this object, $paymentGatewayId, got broken in the db"
unless exists $properties->{options} and $properties->{options};
my $options = from_json($properties->{options});
$options->{session} = $session;
return $class->$orig($options);
};
#------------------------------------------------------------------- #-------------------------------------------------------------------
=head2 _buildObj ( ) =head2 _buildObj ( )
@ -88,21 +123,21 @@ Private method used to build objects, shared by new and create.
=cut =cut
sub _buildObj { #sub _buildObj {
my ($class, $session, $requestedClass, $paymentGatewayId, $options) = @_; # my ($class, $session, $requestedClass, $paymentGatewayId, $options) = @_;
my $self = {}; # my $self = {};
bless $self, $requestedClass; # bless $self, $requestedClass;
register $self; # register $self;
#
my $id = id $self; # my $id = id $self;
#
$session{ $id } = $session; # $session{ $id } = $session;
$options{ $id } = $options; # $options{ $id } = $options;
$className{ $id } = $requestedClass; # $className{ $id } = $requestedClass;
$paymentGatewayId{ $id } = $paymentGatewayId; # $paymentGatewayId{ $id } = $paymentGatewayId;
#
return $self; # return $self;
} #}
#------------------------------------------------------------------- #-------------------------------------------------------------------
@ -205,6 +240,10 @@ to do calculations.
=cut =cut
sub className {
return ref $_->[0];
}
#------------------------------------------------------------------- #-------------------------------------------------------------------
=head2 create ( $session, $options ) =head2 create ( $session, $options )
@ -222,44 +261,32 @@ A list of properties to assign to this PayDriver. See C<definition> for details
=cut =cut
sub create { #sub create {
my $class = shift; # my $class = shift;
my $session = shift; # my $session = shift;
WebGUI::Error::InvalidParam->throw(error => q{Must provide a session variable}) # my $options = shift;
unless ref $session eq 'WebGUI::Session'; # WebGUI::Error::InvalidParam->throw(error => q{Must provide a hashref of options})
my $options = shift; # unless ref $options eq 'HASH' and scalar keys %{ $options };
WebGUI::Error::InvalidParam->throw(error => q{Must provide a hashref of options}) # WebGUI::Error::InvalidParam->throw(error => q{Must provide a human readable label in the hashref of options})
unless ref $options eq 'HASH' and scalar keys %{ $options }; # unless exists $options->{label} && $options->{label};
WebGUI::Error::InvalidParam->throw(error => q{Must provide a human readable label in the hashref of options}) #
unless exists $options->{label} && $options->{label}; # # Generate a unique id for this payment
# my $paymentGatewayId = $session->id->generate;
# Generate a unique id for this payment #
my $paymentGatewayId = $session->id->generate; # # Build object
# my $self = WebGUI::Shop::PayDriver->_buildObj($session, $class, $paymentGatewayId, $options);
# Build object #
my $self = WebGUI::Shop::PayDriver->_buildObj($session, $class, $paymentGatewayId, $options); # # and persist this instance in the db
# $session->db->write('insert into paymentGateway (paymentGatewayId, className) VALUES (?,?)', [
# and persist this instance in the db # $paymentGatewayId,
$session->db->write('insert into paymentGateway (paymentGatewayId, className) VALUES (?,?)', [ # $class,
$paymentGatewayId, # ]);
$class, #
]); # # Set the options via the update method because update() will automatically serialize the options hash
# $self->update($options);
# Set the options via the update method because update() will automatically serialize the options hash #
$self->update($options); # return $self;
#}
return $self;
}
#-------------------------------------------------------------------
=head2 definition ( $session )
This subroutine returns an arrayref of hashrefs, used to validate data put into
the object by the user, and to automatically generate the edit form to show
the user.
=cut
#------------------------------------------------------------------- #-------------------------------------------------------------------
@ -300,34 +327,6 @@ sub displayPaymentError {
#------------------------------------------------------------------- #-------------------------------------------------------------------
=head2 get ( [ $param ] )
This is an enhanced accessor for the options property. By default,
it returns all the options as a hashref. If the name of a key
in the hash is passed, it will only return that value from the
options hash.
=head3 $param
An optional parameter. If it matches the key of a hash, it will
return the value from the options hash.
=cut
sub get {
my $self = shift;
my $param = shift;
my $options = $self->options;
if (defined $param) {
return $options->{ $param };
}
else {
return { %{ $options } };
}
}
#-------------------------------------------------------------------
=head2 getAddress ( addressId ) =head2 getAddress ( addressId )
Returns an instantiated WebGUI::Shop::Address object for the passed address id. Returns an instantiated WebGUI::Shop::Address object for the passed address id.
@ -468,9 +467,8 @@ sub getName {
WebGUI::Error::InvalidParam->throw(error => q{Must provide a session variable}) WebGUI::Error::InvalidParam->throw(error => q{Must provide a session variable})
unless ref $session eq 'WebGUI::Session'; unless ref $session eq 'WebGUI::Session';
my $definition = $class->definition($session); my $definition = $class->meta->pluginName;
return WebGUI::International->new($session, 'Asset')->get(@{ $class->meta->pluginName });
return $definition->[0]->{name};
} }
#------------------------------------------------------------------- #-------------------------------------------------------------------
@ -495,41 +493,31 @@ that object.
=cut =cut
sub new { #sub new {
my $class = shift; # my $class = shift;
my $session = shift; # my $session = shift;
WebGUI::Error::InvalidParam->throw(error => q{Must provide a session variable}) # WebGUI::Error::InvalidParam->throw(error => q{Must provide a session variable})
unless ref $session eq 'WebGUI::Session'; # unless ref $session eq 'WebGUI::Session';
my $paymentGatewayId = shift; # my $paymentGatewayId = shift;
WebGUI::Error::InvalidParam->throw(error => q{Must provide a paymentGatewayId}) # WebGUI::Error::InvalidParam->throw(error => q{Must provide a paymentGatewayId})
unless defined $paymentGatewayId; # unless defined $paymentGatewayId;
#
# Fetch the instance data from the db # # Fetch the instance data from the db
my $properties = $session->db->quickHashRef('select * from paymentGateway where paymentGatewayId=?', [ # my $properties = $session->db->quickHashRef('select * from paymentGateway where paymentGatewayId=?', [
$paymentGatewayId, # $paymentGatewayId,
]); # ]);
WebGUI::Error::ObjectNotFound->throw(error => q{paymentGatewayId not found in db}, id => $paymentGatewayId) # WebGUI::Error::ObjectNotFound->throw(error => q{paymentGatewayId not found in db}, id => $paymentGatewayId)
unless scalar keys %{ $properties }; # unless scalar keys %{ $properties };
#
croak "Somehow, the options property of this object, $paymentGatewayId, got broken in the db" # croak "Somehow, the options property of this object, $paymentGatewayId, got broken in the db"
unless exists $properties->{options} and $properties->{options}; # unless exists $properties->{options} and $properties->{options};
#
my $options = from_json($properties->{options}); # my $options = from_json($properties->{options});
#
my $self = WebGUI::Shop::PayDriver->_buildObj($session, $class, $paymentGatewayId, $options); # my $self = WebGUI::Shop::PayDriver->_buildObj($session, $class, $paymentGatewayId, $options);
#
return $self; # return $self;
} #}
#-------------------------------------------------------------------
=head2 options ( )
Accessor for the driver properties. This returns a hashref
any driver specific properties. To set the properties, use
the C<update> method.
=cut
#------------------------------------------------------------------- #-------------------------------------------------------------------
@ -666,7 +654,7 @@ Accessor for the session object. Returns the session object.
#------------------------------------------------------------------- #-------------------------------------------------------------------
=head2 update ( $options ) =head2 write ( $options )
Setter for user configurable options in the payment objects. Setter for user configurable options in the payment objects.
@ -677,20 +665,16 @@ flattened into JSON and stored in the database as text. There is no content che
=cut =cut
sub update { sub write {
my $self = shift; my $self = shift;
my $properties = shift;
WebGUI::Error::InvalidParam->throw(error => 'update was not sent a hashref of options to store in the database')
unless ref $properties eq 'HASH' and scalar keys %{ $properties };
my $properties = $self->get();
delete $properties->{session};
my $jsonOptions = to_json($properties); my $jsonOptions = to_json($properties);
$self->session->db->write('update paymentGateway set options=? where paymentGatewayId=?', [ $self->session->db->setRow($self->tableName, 'paymentGatewayId', {
$jsonOptions, paymentGatewayId => $self->paymentGatewayId,
$self->paymentGatewayId options => $jsonOptions,
]); });
my $storedProperties = clone $properties;
$options{ id $self } = $storedProperties;
return; return;
} }

View file

@ -118,6 +118,12 @@ our $I18N = {
context => q|Status message|, context => q|Status message|,
}, },
'Payment Driver' => {
message => q|Payment Driver|,
lastUpdated => 0,
context => q|Name of the base Payment Driver|,
},
}; };
1; 1;