NAME
Mango::Provider - Base class for all Mango providers
SYNOPSIS
package MyApp::Provider::Users;
use strict;
use warnings;
BEGIN {
use base qw/Mango::Provider/;
};
__PACKAGE__->result_class('MyClass');
my $object = $provider->create(\%data);
DESCRIPTION
Mango::Provider is a base class for all providers used in Mango.
CONSTRUCTOR
new
Creates a new provider object. If options are passed to new, those are sent to setup
.
my $provider = Mango::Provider->new({
result_class => 'MyResultClass'
});
The following options are available at the class level, to new/setup and take the same data as their method counterparts:
result_class
METHODS
create
Creates a new object of type result_class
using the supplied data.
my $object = $provider->create({
id => 23,
thingy => 'value'
});
delete
Deletes objects from the provider matching the supplied filter.
$provider->delete({
col => 'value'
});
It can also delete an object passed into it:
$provider->delete($object);
This is the same as:
$provider->delete({ id => $object->id });
get_by_id
Retrieves an object from the provider matching the specified id.
my $object = $provider->get_by_id(23);
Returns undef if no matching result can be found.
result_class
Gets/sets the name of the result class results should be returned as.
$provider->result_class('MyClass');
my $object = $provider->search->first;
print ref $object; # MyClass
An exception will be thrown if the specified class can not be loaded.
search
Returns a list of objects in list context or a Mango::Iterator in scalar context matching the specified filter.
my @objects = $provider->search({
col => 'value'
});
my $iterator = $provider->search({
col => 'value'
});
The complete list of supported options are at the discretion each individual provider, but each provider should support the following options:
- order_by
-
The name/direction of of the column(s) to sort the results.
{order_by => 'col asc'}
- page
-
The page number of the results to return.
{page => 2}
- rows
-
The number of results per page to return.
{rows => 10}
When using rows/page, you can retrieve a Data::Page object from the iterator.
my $results = $provider->search(undef, {
rows => 10, page => 2
});
my $pager = $results->pager;
print "Page 2 of ", $pager->last_page;
while (my $result = $results->next) {
print $result->id;
};
setup
Calls each key as a method with the supplied value. setup
is automatically called by new
.
my $provider = Mango::Provider->new({
result_class => 'MyResultClass'
});
is the same as:
my $provider = Mango::Provider->new;
$provider->setup({
result_class => 'MyResultClass'
});
which is the same as:
my $provider = Mango::Provider->new;
$provider->result_class('MyResultClass');
update
Saves any changes made to the object back to the underlying store.
my $object = $provider->create(\%data);
$object->col('value');
$provider->update($object);
get_component_class
Gets the current class for the specified component name.
my $class = $self->get_component_class('result_class');
There is no good reason to use this. Use the specific class accessors instead.
set_component_class
Sets the current class for the specified component name.
$self->set_component_class('result_class', 'MyItemClass');
A Mango::Exception exception will be thrown if the specified class can not be loaded.
There is no good reason to use this. Use the specific class accessors instead.
SEE ALSO
AUTHOR
Christopher H. Laco
CPAN ID: CLACO
claco@chrislaco.com
http://today.icantfocus.com/blog/