NAME
Handel::Storage - Abstract storage layer for cart/order/item reads/writes
SYNOPSIS
package MyStorage;
use strict;
use warnings;
use base qw/Handel::Storage/;
sub create {
my ($self, $data) = @_;
return $self->result_class->create_instance(
$ldap->magic($data), $self
);
};
package MyCart;
use strict;
use warnings;
use base qw/Handel::Base/;
__PACKAGE__->storage_class('MyStorage');
__PACKAGE__->storage({
columns => [qw/id foo bar baz/],
primary_columns => [qw/id/]
});
1;
DESCRIPTION
Handel::Storage is a base class used to create custom storage classes used by cart/order/item classes. It provides some generic functionality as well as methods that must be implemented by custom storage subclasses like Handel::Storage::DBIC.
CONSTRUCTOR
new
Creates a new instance of Handel::Storage, and passes the options to "setup" on the new instance. The three examples below are the same:
my $storage = Handel::Storage-new({
item_class => 'Handel::Item'
});
my $storage = Handel::Storage-new;
$storage->setup({
item_class => 'Handel::Item'
});
my $storage = Handel::Storage->new;
$storage->item_class('Handel::Item')
The following options are available to new/setup, and take the same data as their method counterparts:
add_columns
autoupdate
cart_class
checkout_class
constraints
currency_class
currency_columns
default_values
item_class
iterator_class
primary_columns
remove_columns
result_class
validation_module
validation_profile
METHODS
add_columns
Adds a list of columns to the current storage object.
$storage->add_columns('quix');
add_constraint
Adds a named constraint for the given column to the current storage object. You can have any number of constraints for each column as long as they all have different names. The constraints may or may not be called in the order in which they are added.
$storage->add_constraint('id', 'Check Id Format' => \&constraint_uuid);
It is up to each custom storage class to decide if and how to implement column constraints.
add_item
Adds a new item to the specified result, returning a storage result object.
my $storage = Handel::Storage::Cart->new;
my $result = $storage->create({
shopper => '11111111-1111-1111-1111-111111111111'
});
my $item = $storage->add_item($result, {
sku => 'ABC123'
});
print $item->sku;
This method must be implemented in custom subclasses.
autoupdate
Gets/sets the autoupdate flag for the current storage object. When set to 1, an update request will be made to storage for every column change. When set to 0, no updated data will be sent to storage until update
is called.
$storage->autoupdate(1);
The default is 1.
It is up to each custom storage class to decide if and how to implement autoupdates.
cart_class
Gets/sets the cart class to be used when creating orders from carts.
$storage->cart_class('CustomCart');
A Handel::Exception::Storage exception will be thrown if the specified class can not be loaded.
checkout_class
Gets/sets the checkout class to be used to process the order through the CHECKOUT_PHASE_INITIALIZE
phase when creating a new order and the process options is set. The default checkout class is Handel::Checkout.
$storage->checkout_class('CustomCheckout');
A Handel::Exception::Storage exception will be thrown if the specified class can not be loaded.
clone
Returns a clone of the current storage instance.
$storage->item_class('Item');
$storage->cart_class('Cart');
my $clone = $storage->clone;
$clone->item_class('Bar');
print $storage->item_class; # Item
print $clone->item_class; # Item
print $clone->cart_class; $ Cart
This is used mostly between sub/super classes to inherit a copy of the storage settings without having to specify options from scratch.
column_accessors
Returns a hashref containing all of the columns and their accessor names for the current storage object.
$storage->add_columns(qw/foo bar/);
print %{$self->column_accessors});
# foo foo bar bar
The column accessors are used by cart/order/item classes to map public accessors to their columns.
columns
Returns a list of columns from the current storage object;
$storage->add_columns(qw/foo bar baz/);
print $storage->columns; # foo bar baz
constraints
Gets/sets the constraints configuration for the current storage instance.
$storage->constraints({
id => {'Check Id Format' => \&constraint_uuid},
name => {'Check Name/Type' => \%constraint_cart_type}
});
The constraints are stored in a hash where each key is the name of the column and each value is another hash reference containing the constraint name and the constraint subroutine reference.
It is up to each custom storage class to decide if and how to implement column constraints.
copyable_item_columns
Returns a list of columns in the current item class that can be copied freely. This list is usually all columns in the item class except for the primary key columns and the foreign key columns that participate in the specified item relationship.
count_items
Returns the number of items associated with the specified result.
my $storage = Handel::Storage::Cart->new;
my $result = $storage->create({
shopper => '11111111-1111-1111-1111-111111111111'
});
$result->add_item({
sku => 'ABC123'
});
print $storage->count_items($result);
This method must be implemented in custom subclasses.
create
Creates a new result in the current storage medium.
my $result = $storage->create({
col1 => 'foo',
col2 => 'bar'
});
This method must be implemented in custom subclasses.
currency_class
Gets/sets the currency class to be used when inflating currency columns. The default currency class is Handel::Currency. The currency class used should be subclass of Handel::Currency.
$storage->currency_class('CustomCurrency');
A Handel::Exception::Storage exception will be thrown if the specified class can not be loaded.
It is up to each custom storage class to decide if and how to implement currency columns.
currency_columns
Gets/sets the columns that should be inflated into currency objects.
$storage->currency_columns(qw/total price tax/);
It is up to each custom storage class to decide if and how to implement currency columns.
default_values
Gets/sets the hash containing the default values to be applied to empty columns during create/update actions.
$storage->default_values({
id => \&newid,
name => 'My New Cart'
});
The default values are stored in a hash where the key is the name of the column and the value is either a reference to a subroutine to get the value from, or an actual default value itself.
It is up to each custom storage class to decide if and how to implement default values.
delete
Deletes results matching the filter in the current storage medium.
$storage->delete({
id => '11111111-1111-1111-1111-111111111111'
});
This method must be implemented in custom subclasses.
delete_items
Deletes items matching the filter from the specified result.
my $storage = Handel::Storage::Cart->new;
my $result = $storage->create({
shopper => '11111111-1111-1111-1111-111111111111'
});
$result->add_item({
sku => 'ABC123'
});
$storage->delete_items($result, {
sku => 'ABC%'
});
This method must be implemented in custom subclasses.
item_class
Gets/sets the item class to be used when returning cart/order items.
$storage->item_class('CustomCartItem');
The class specified should be a subclass of Handel::Base, or at least provide its create_instance
and result
methods.
A Handel::Exception::Storage exception will be thrown if the specified class can not be loaded.
iterator_class
Gets/sets the class used for iterative result operations. The default iterator is Handel::Iterator::List.
$storage->iterator_class('MyIterator');
my $results = $storage->search;
print ref $results # Handel::Iterator::List
A Handel::Exception::Storage exception will be thrown if the specified class can not be loaded.
new_uuid
Returns a new uuid/guid string in the form of
xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
See DBIx::Class::UUIDColumns for more information on how uuids are generated.
primary_columns
Returns a list of primary columns from the current storage object;
$storage->add_columns(qw/foo bar baz/);
$storage->primary_columns('foo');
print $storage->primary_columns; # foo
remove_columns
Removes a list of columns from the current storage object.
$storage->remove_columns(qw/description/);
remove_constraint
Removes a named constraint for the given column from the current storage object.
$storage->remove_constraint('id', 'Check Id Format' => \&constraint_uuid);
remove_constraints
Removes all constraints for the given column from the current storage object.
$storage->remove_constraints('id');
result_class
Gets/sets the result class to be used when returning results from create/search storage operations. The default result class is Handel::Storage::Result.
$storage->result_class('CustomStorageResult');
A Handel::Exception::Storage exception will be thrown if the specified class can not be loaded.
search
Returns results in list context, or an iterator in scalar context from the current source in the current schema matching the search filter.
my $iterator = $storage->search({
col1 => 'foo'
});
my @results = $storage->search({
col1 => 'foo'
});
This method must be implemented in custom subclasses.
search_items
Returns items matching the filter associated with the specified result.
my $storage = Handel::Storage::Cart->new;
my $result = $storage->search({
id => '11111111-1111-1111-1111-111111111111'
});
my $iterator = $storage->search_items($result);
Returns results in list context, or an iterator in scalar context from the current source in the current schema matching the search filter.
This method must be implemented in custom subclasses.
setup
Configures a storage instance with the options specified. Setup accepts the exact same options that "new" does.
package MyStorageClass;
use strict;
use warnings;
use base qw/Handel::Storage/;
__PACKAGE__->setup({
item_class => 'Foo'
});
# or
my $storage = Handel::Storage-new;
$storage->setup({
item_class => 'Items',
cart_class => 'CustomerCart'
});
This is the same as doing:
my $storage = Handel::Storage-new({
item_class => 'Items',
cart_class => 'CustomerCart'
});
If you call setup on a storage instance or class that has already been configured, its configuration will be updated with the new options. No attempt will be made to clear or reset the unspecified settings back to their defaults.
validation_module
Gets/sets the module validation class should use to do its column data validation. The default module is FormValidator::Simple.
It is up to each custom storage class to decide if and how to implement data validation.
validation_profile
Gets/sets the validation profile to be used when validating column values.
$storage->validation_profile([
param1 => ['NOT_BLANK', 'ASCII', ['LENGTH', 2, 5]],
param2 => ['NOT_BLANK', 'INT' ],
mail1 => ['NOT_BLANK', 'EMAIL_LOOSE']
]);
* The default validation module is FormValidator::Simple, which expects a profile in an array reference. If you use Data::FormValidator, make sure you pass in the profile as a hash reference instead:
$storage->validation_profile({
optional => [qw( company
fax
country )],
required => [qw( fullname
phone
email
address )]
});
It is up to each custom storage class to decide if and how to implement data validation.
SEE ALSO
Handel::Storage::DBIC, Handel::Storage::Result, Handel::Manual::Storage, Handel::Storage::Cart, Handel::Storage::Order
AUTHOR
Christopher H. Laco
CPAN ID: CLACO
claco@chrislaco.com
http://today.icantfocus.com/blog/