Badger::Storages

NAME

Top Close Open

Badger::Storages - factory module for Badger::Storage objects

SYNOPSIS

Top Close Open
# Storage() is a shortcut to Badger::Storages->storage() which
# loads and instantiates instances of Badger::Storage subclasses
use Badger::Storages 'Storage';

# the following are all equivalent
my $storage = Storage('file:/path/to/directory');
my $storage = Storage( file => '/path/to/directory' );
my $storage = Storage({
    type => file,
    path => '/path/to/directory',
});

# specifying storage option (codec), the following are equivalent
my $storage = Storage(
    file => {
        path  => '/path/to/directory',
        codec => 'json',
    }
);
my $storage = Storage({
    type  => file,
    path  => '/path/to/directory',
    codec => 'json',
});

# see Badger::Storage for further examples

DESCRIPTION

Top Close Open

This clumsily named module implements a subclass of Badger::Factory for loading and instantiating Badger::Storage object (or strictly speaking, subclasses of Badger::Storage such as Badger::Storage::Filesystem).

You can call the storage() method as a class method to load a storage module and instantiate an instance of it.

my $storage = Badger::Storages->storage( file => '/path/to/dir' );

You can also create a Badger::Storages object and call the storage() method as an object method. Creating a Badger::Storages object allows you to specify your own storage modules, a custom lookup path, or any other valid configuration options for the storage factory.

my $factory = Badger::Storages->new(
    storage_path => ['My::Storage', 'Badger::Storage', 'BadgerX::Storage'],
    storages     => {
        mumcached => 'My::MumCache::Storage',   # ask mum where it is
    },
};

You can now load the entirely fictional My::MumCache::Storage module as follows:

my $storage = $factory->storage(
    mumcached => { 
        # any options for the My::Mum::Storage module
    }
);

Or any module under the My::Storage, Badger::Storage or BadgerX::Storage namespaces:

my $storage = $factory->storage(
    example => { 
        # any options for the My::Storage::Example module
    }
);

EXPORTABLE SUBROUTINES

Top Close Open

Storage()

Top Close Open

This function of convenience is a shortcut for fetching a storage object.

use Badger::Storages 'Storage';
my $storage = Storage('file:/path/to/dir');

It is equivalent to:

use Badger::Storages;
my $storage = Badger::Storages->storage('file:/path/to/dir');

METHODS

Top Close Open

storage($type,@args)

Top Close Open

This is the main method used to fetch a Badger::Storage object. The first argument is the storage type, corresponding to the name of a Badger::Storage subclass module.

my $storage = Badger::Storages->storage( filesystem => '/path/to/dir' );

In this example the filesystem type is mapped to Badger::Storage::Filesystem. You can also use the shorthand form of file.

my $storage = Badger::Storages->storage( file => '/path/to/dir' );

You can even go one step further and specify the entire thing as a URL

my $storage = Badger::Storages->storage('file:/path/to/dir');

These are all shorthand for the following:

my $storage = Badger::Storages->storage( 
    filesystem => {
        path => '/path/to/dir',
    }
);

If you prefer you can pass a single reference to a hash array with the storage type defined as the type item.

my $storage = Badger::Storages->storage( 
    {
        type => 'filesystem',
        path => '/path/to/dir',
    }
);

Any other configuration arguments destined for the particular storage module can be added to this hash array. For example, to create a Badger::Storage::Filesystem object that stores YAML encoded files in /tmp/badger_data, you can write this:

my $storage = Badger::Storages->storage( 
    file => {
        path  => '/path/to/dir',
        codec => 'yaml',
    }
);

See the documentation for Badger::Storage for information about what you can do with the storage object returned.

INTERNAL METHODS

Top Close Open

type_args(@args)

Top Close Open

This internal method is used to massage the arguments passed to the storage() method into a canonical format.

AUTHOR

Top Close Open

Andy Wardley http://wardley.org

COPYRIGHT

Top Close Open

Copyright (C) 2007-2009 Andy Wardley. All Rights Reserved.

This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

Fork Me on Github