NAME

Badger::Factory - base class factory module

SYNOPSIS

This module is designed to be subclassed to create factory classes that automatically load modules and instantiate objects on demand.

package My::Widgets;
use base 'Badger::Factory';

# tell the base class factory what we create
our $ITEM        = 'widget';
our $ITEMS       = 'widgets';

# define module search path for widgets
our $WIDGET_PATH = ['My::Widget', 'Your::Widget'];

# lookup table for any non-standard spellings/capitalisations/paths
our $WIDGETS     = {
    url   => 'My::Widget::URL',       # non-standard capitalisation
    color => 'My::Widget::Colour',    # different spelling
    amp   => 'Nigels::Amplifier',     # different path
};

You can then use it like this:

use My::Widgets;

# class methods (note: widget() is singular)
$w = My::Widgets->widget( foo => { msg => 'Hello World' } );

# same as:
use My::Widget::Foo;
$w = My::Widget::Foo({ msg => 'Hello World' });

# add/update widgets lookup table (note: widgets() is plural)
My::Widgets->widgets(
    extra => 'Another::Widget::Module',
    super => 'Golly::Gosh',
);

# now load and instantiate new widget modules
$w = My::Widgets->widget( extra => { msg => 'Hello Badger' } );

You can also create factory objects:

my $factory = My::Widgets->new(
    widget_path => ['His::Widget', 'Her::Widget'],
    widgets     => {
        extra => 'Another::Widget::Module',
        super => 'Golly::Gosh',
    }
);

$w = $factory->widget( foo => { msg => 'Hello World' } );

The Badger::Factory::Class module can be used to simplify the process of defining factory subclasses.

package My::Widgets;

use Badger::Factory::Class
    item    => 'widget',
    path    => 'My::Widget Your::Widget';
    widgets => {
        extra => 'Another::Widget::Module',
        super => 'Golly::Gosh',
    };

DESCRIPTION

This module implements a base class factory object for loading modules and instantiating objects on demand.

TODO: the rest of the documentation

METHODS

path($path)

Mutator method to get/set the factory module path.

TODO: examples

items(%items)

TODO: Method to fetch or update the lookup table for mapping names to modules

item($name,@args)

TODO: Method to load a module and insantiate an object.

type_args(@args)

TODO: Method to perform any manipulation on the argument list before passing to object constructor

load($type)

TODO: Method to load a module for an object type

construct($name,$class,@args)

TODO: Method to instantiate a $class object using the arguments provided. In the base class this method simply calls:

$class->new(@args);

module_names($type)

TODO: Method to expand an object type into a candidate list of module names.

found_ref_ARRAY($name,$entry,@args)

TODO: Method hook to handle the case of a factory entry defined as an array reference. It is assumed to be [$module, $class]. The $module is loaded and the $class instantiated.

Subclasses can re-define this to change this behaviour.

found($name,$item)

TODO: Method hook to perform any post-processing (e.g. caching) after an item has been found and instantiated.

can($method)

TODO: Implements the magix to ensure that the item-specific accessor methods (e.g. widget()/widgets()) are generated on demand.

AUTOLOAD(@args)

TODO: Implements the AUTOLOAD magic to generate the item-specific accessor methods (e.g. widget()/widgets()) on demand.

AUTHOR

Andy Wardley http://wardley.org/

COPYRIGHT

Copyright (C) 2006-2008 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.

SEE ALSO

Badger::Factory::Class, Badger::Codecs.