File: Factory.pm

package info (click to toggle)
libcatalyst-model-adaptor-perl 0.10-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 284 kB
  • sloc: perl: 1,750; makefile: 2
file content (73 lines) | stat: -rw-r--r-- 1,594 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package Catalyst::Model::Factory;
use strict;
use warnings;
use MRO::Compat;

use base 'Catalyst::Model::Adaptor::Base';
use Catalyst::Utils ();
use Scalar::Util 'blessed';

our $VERSION = '0.10';

sub COMPONENT {
    my ($class, @args) = @_;
    my $self = $class->next::method(@args);

    $self->_load_adapted_class;
    return $self;
}

sub ACCEPT_CONTEXT {
    my ($self, $context, @args) = @_;
    my $arg = {};
    if ( scalar @args ) {
        if ( ref($args[0]) eq 'HASH' ) {
            $arg = $args[0];
        }
        else {
            $arg = { @args };
        }
    }
    my $suffix = Catalyst::Utils::class2classsuffix(blessed $self);
    return $self->_create_instance(
        $context,
        $self->merge_config_hashes($context->config->{$suffix} || {}, $arg),
    );
}

1;
__END__

=head1 NAME

Catalyst::Model::Factory - use a plain class as a Catalyst model,
instantiating it every time it is requested

=head1 SYNOPSIS

This module works just like
L<Catalyst::Model::Adaptor|Catalyst::Model::Adaptor>, except that a
fresh instance of your adapted class is created every time it is
requested via C<< $c->model >>.

=head1 CUSTOMIZING

You can customize your subclass just like
L<Catalyst::Model::Adaptor|Catalyst::Model::Adaptor>.  Instead of
C<$app>, though, you'll get C<$c>, the current request context.

=head1 METHODS

These methods are called by Catalyst, not by you:

=head2 COMPONENT

Load your class

=head2 ACCEPT_CONTEXT

Create an instance of your class and return it.

=head1 SEE ALSO

For all the critical documentation, see L<Catalyst::Model::Adaptor>.