NAME
MooX::Failover - Instantiate Moo classes with failover
VERSION
v0.2.0_01
SYNOPSIS
# In your class:
package MyClass;
use Moo;
use MooX::Failover;
has 'attr' => ( ... );
# after attributes are defined:
failover_to 'OtherClass';
...
# When using the class
my $obj = MyClass->new( %args );
# If %args contains missing or invalid values or new otherwise
# fails, then $obj will be of type "OtherClass".
INSTALLATION
See How to install CPAN modules.
Required Modules
This distribution requires Perl v5.10.0.
This distribution requires the following modules:
Class::Load (version 0.20)
This distribution recommends the following modules:
RECENT CHANGES
Enhancements
Significant optimizations at the expense of incompatability.
Added the ability to use an alternative name of the constructor for the failover class.
Arguments for the constructor can be a hash reference, array reference or scalar.
Added the ability to save the name of the class that triggered to error.
Incompatability
Rewrote the interface entirely, so that the failover is specified as part of the class definition. See the POD for details.
Other Changes
Added missing MANIFEST.SKIP and removed dist.ini from distribution.
Added an example using Web::Machine.
See the Changes file for a longer revision history.
DESCRIPTION
This role provides constructor failover for Moo classes.
If a class cannot be instantiated because of invalid arguments (perhaps from an untrusted source), then instead it returns the failover class (passing the same arguments to that class).
It is roughly equivalent to using
my $obj = eval { MyClass->new(%args) //
OtherClass->new( %args, error => $@ );
This allows for cleaner design, by not forcing you to duplicate type checking for class parameters.
A use case for this module is for instantiating Web::Machine::Resource objects, where a resource class's attributes correspond to URL arguments. A type failure would normally cause an internal serror error (HTTP 500). Using MooX::Failover, we can return a different resource object that examines the error, and returns a more appropriate error code, e.g. bad request (HTTP 400).
SEE ALSO
AUTHOR
Robert Rothenberg <rrwo@thermeon.com
>
Acknowledgements
COPYRIGHT
Copyright 2014 Thermeon Europe.
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
This program is distributed in the hope that it will be useful, but without any warranty; without even the implied warranty of merchantability or fitness for a particular purpose.