Handel::Storage - Abstract storage layer for cart/order/item reads/writes
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;
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.
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 constraints currency_class currency_columns currency_code currency_code_column currency_format default_values item_class iterator_class primary_columns remove_columns result_class validation_module validation_profile
Adds a list of columns to the current storage object.
$storage->add_columns('quix');
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.
Adds a new item to the specified result, returning a storage result object.
my $storage = Handel::Storage::DBIC::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.
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.
update
$storage->autoupdate(1);
The default is 1.
It is up to each custom storage class to decide if and how to implement autoupdates.
Runs the configured constraints against data and returns true if the data passes all current constraints. Otherwise, a Handel::Exception::Constraint exception is thrown.
data
$storage->constraints({ id => {'Check Id Format' => \&constraint_uuid}, name => {'Check Name/Type' => \%constraint_cart_type} }); my $data = {id => 'abc'}; $storage->check_constraints($data);
A Handel::Exception::Storage exception is thrown if the first parameter is not a HASHREF.
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.
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.
Returns a list of columns from the current storage object;
$storage->add_columns(qw/foo bar baz/); print $storage->columns; # foo bar baz
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.
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.
Returns the number of items associated with the specified result.
my $storage = Handel::Storage::DBIC::Cart->new; my $result = $storage->create({ shopper => '11111111-1111-1111-1111-111111111111' }); $result->add_item({ sku => 'ABC123' }); print $storage->count_items($result);
Creates a new result in the current storage medium.
my $result = $storage->create({ col1 => 'foo', col2 => 'bar' });
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.
Gets/sets the columns that should be inflated into currency objects.
$storage->currency_columns(qw/total price tax/);
Gets/sets the currency code used by default when formatting currency objects.
See Locale::Currency::Format and Locale::Currency for the list of available currency codes.
Gets/sets the name of the column that contains the currency code to be used for the current row. If no column is specified or it is empty, currency_code will be used instead.
currency_code
Gets/sets the currency formatting options used by default when formatting currency objects.
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.
Deletes results matching the filter in the current storage medium.
$storage->delete({ id => '11111111-1111-1111-1111-111111111111' });
Deletes items matching the filter from the specified result.
my $storage = Handel::Storage::DBIC::Cart->new; my $result = $storage->create({ shopper => '11111111-1111-1111-1111-111111111111' }); $result->add_item({ sku => 'ABC123' }); $storage->delete_items($result, { sku => 'ABC%' });
Returns true if the column exists in the current storage object.
Gets/sets the item storage class used to hold item storage configuration and/or create cart/order items.
my $storage = My::Storage::Cart->new; $storage->item_storage_class('My::Storage::Cart::Item');
Gets/sets the storage objects used to hold item storage configuration options and/or create item storage results. If no storage object is assigned, one will be created using the specified item_storage_class.
item_storage_class
$storage->item_storage_class('My::Storage::Cart::Item'); my $item_storage = $storage->item_storage; print ref $item_storage; # My::Storage::Cart:Item my $storage = My::Storage::Order->new; my $item_storage = My::Storage::Order::Item->new; $storage->item_storage($item_storage);
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
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.
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
This method accepts errors and converts them into Handel::Exception objects before throwing the error.
If message already a blessed object, it is just re thrown.
message
Removes a list of columns from the current storage object.
$storage->remove_columns(qw/description/);
Removes a named constraint for the given column from the current storage object.
$storage->remove_constraint('id', 'Check Id Format' => \&constraint_uuid);
Removes all constraints for the given column from the current storage object.
$storage->remove_constraints('id');
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');
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' });
Returns items matching the filter associated with the specified result.
my $storage = Handel::Storage::DBIC::Cart->new; my $result = $storage->search({ id => '11111111-1111-1111-1111-111111111111' }); my $iterator = $storage->search_items($result);
Sets the default values on any column that is not already defined using the values defined in default_values.
default_values
$self->default_values({ col1 => 'foo', col2 => sub {'stuff'}, col3 => 2 }); my $data = {col3 => 4}; $self->set_default_values($data); print %{$data}; # { # col1 => 'foo', # col2 => 'stuff', # col3 => 2 # }
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.
Starts a transaction on the current storage object.
Commits the current transaction on the current storage object.
Rolls back the current transaction on the current storage object.
Validates the specified data against the current <validation_profile> and returns the validation result using the specified validation_module.
validation_module
$self->validation_profile([ col1 => [ ['NOT_BLANK'] ] ]); my $data = {col1 => ''}; my $results = $self->validate_data($data); if ($results->success) { ... };
A Handel::Exception::Storage exception is thrown if validation module is set to FormValidator::Simple and the validation profile isn't a ARRAYREF, or the validation module is set to Data::FormValidator and the validation profile isn't a HASHREF.
Gets/sets the module validation class should use to do its column data validation. The default module is FormValidator::Simple.
And validation module may be used that supports the check method.
It is up to each custom storage class to decide if and how to implement data validation.
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 )] });
Gets the current data for the specified component name.
my $profile = $self->get_component_data('validation_profile');
There is no good reason to use this. Use the specific class accessors instead.
Sets the current class for the specified component name.
$self->set_component_data('validation_profile', [name => ['NOT_BLANK']]);
Gets the current class for the specified component name.
my $class = $self->get_component_class('item_class');
$self->set_component_class('item_class', 'MyItemClass');
Handel::Storage::DBIC, Handel::Storage::Result, Handel::Manual::Storage, Handel::Storage::DBIC::Cart, Handel::Storage::DBIC::Order
Christopher H. Laco CPAN ID: CLACO claco@chrislaco.com http://today.icantfocus.com/blog/
To install Handel, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Handel
CPAN shell
perl -MCPAN -e shell install Handel
For more information on module installation, please visit the detailed CPAN module installation guide.