The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

Mojolicious::Plugin::AnyData

VERSION

version 1.20

DESCRIPTION

Mojolicious::Plugin::AnyData uses perl data in the memory like a database source.

SYNOPSIS

    use Mojolicious::Plugin::AnyData
    
    sub startup {
        my $self = shift;
        
        $self->plugin(any_data => {
            load_data => {
                cars => [
                    ['id', 'model'],
                    [ 1, 'Honda'], ],
                },
            },
            helper => 'db',
        });
        
        # ... or
        $self->plugin(any_data => {
            func => ['cars', 'XML', 'cars.xml', 'ad_import'],
        });
        
        # ... or
        $self->plugin(any_data => {
            load_data => 'my_test_data.conf'
        });
    }

CONFIGURATION

This plugin doesn't require any options at startup, so you may load your data in your program at any time. The helper returns the default value 'db' if they haven't been specified before.

You can switch from DBD::AnyData instance to your production database handler by change development mode to production in your project:

    app->mode('production');

HELPERS

Mojolicious::Plugin::AnyData provides all methods inherited from DBD::AnyData and DBI.

db (or something else)

This helper will be created with your specified name or 'db', by default, in order to access a database handler.

any_data

This helper gives access to a plugin instance and provides the following methods:

METHODS

load_data

It loads data from perl struct (hashref) into the memory. Also, it can support several tables at the same time. You can use this method on startup, like a simple config option:

    $self->plugin(any_data => {
        load_data => {
            artists => [
                ['id_artist', 'artist_name'],
                [          1, 'Metallica'],
                [          2, 'Dire Staits'],
            ],
            releases => [
                ['id_release', 'release_name',  'id_artist'],
                [           1, 'Death Magnetic',          1],
                [           2, 'Load',                    1],
            ],
        },
    });

Or, like a real plugin method in your program:

    app->any_data->load_data({
        artists => [
            ['id_artist', 'artist_name'],
            [          1, 'Metallica'],
            [          2, 'Dire Staits'],
        ],
        releases => [
            ['id_release', 'release_name',  'id_artist'],
            [           1, 'Death Magnetic',          1],
            [           2, 'Load',                    1],
        ],
    });
    

You can also load data stuctures from a separate config, using Mojolicious::Plugin::Config:

    $self->plugin(any_data => {
        load_data => 'test_data.conf',
        helper    => 'db'
    });
    
    # or:
    
    app->any_data->load_data('test_data.conf');

The plugin automatically checks the data type (hashref or simple scalar) and in case if it's a scalar, treats it as the file name containing data. They will be loaded automagically using Mojolicious::Plugin::Config.

func

It provides a wrapper for the common DBD::AnyData::func method with one change: before loading new data, it will remove the table with the same name from the memory if it already exists.

    $self->plugin(any_data => {
        func => ['cars', 'XML', 'cars.xml', 'ad_import'],
    });
    
    # or, of course
    
    app->any_data->func('cars', 'XML', 'cars.xml', 'ad_import');

SEE ALSO

Mojolicious, DBI, DBD::AnyData

AUTHOR

Alexander Ponomarev, <shootnix@cpan.org>

BUGS/CONTRIBUTING

Please report any bugs and feature requests via the Web interface at https://github.com/shootnix/Mojolicious-Plugin-AnyData/issues. If you want to contribute, feel free to fork our Git repository https://github.com/shootnix/Mojolicious-Plugin-AnyData/.