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

Arguments: \%options

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

Arguments: \%data

Creates a new object of type result_class using the supplied data.

my $object = $provider->create({
    id => 23,
    thingy => 'value'
});

delete

Arguments: \%filter or $object

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

Arguments: $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

Arguments: $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.

Arguments: \%filter, \%options

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

Arguments: \%options

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

Arguments: $object

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

Arguments: $name

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

Arguments: $name, $value

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

Mango::Iterator

AUTHOR

Christopher H. Laco
CPAN ID: CLACO
claco@chrislaco.com
http://today.icantfocus.com/blog/