File Coverage

blib/lib/Catalyst/Model/Proxy.pm
Criterion Covered Total %
statement 12 29 41.3
branch 0 4 0.0
condition 0 6 0.0
subroutine 4 6 66.6
pod 1 1 100.0
total 17 46 36.9


line stmt bran cond sub pod time code
1             package Catalyst::Model::Proxy;
2              
3 1     1   32426 use strict;
  1         3  
  1         62  
4 1     1   7 use base 'Catalyst::Model';
  1         2  
  1         887  
5 1     1   3866 use NEXT;
  1         10889  
  1         304  
6              
7             our $VERSION = '0.04';
8             our %CLASS_CACHE;
9              
10             =head1 NAME
11              
12             Catalyst::Model::Proxy - Proxy Model Class
13              
14             =head1 SYNOPSIS
15              
16             # a sample use with C<Catalyst::Model::DBI::SQL::Library>
17              
18             # lib/MyApp/Model/DBI/SQL/Library/MyDB.pm
19             package MyApp::Model::DBI::SQL::Library::MyDB;
20              
21             use base 'Catalyst::Model::DBI::SQL::Library';
22              
23             __PACKAGE__->config(
24             dsn => 'dbi:Pg:dbname=myapp',
25             password => '',
26             user => 'postgres',
27             options => { AutoCommit => 1 },
28             );
29              
30             1;
31              
32             # lib/MyApp/Model/Other.pm
33             package MyApp::Model::Other;
34              
35             use base 'Catalyst::Model::Proxy';
36              
37             __PACKAGE__->config(
38             target_class => 'DBI::SQL::Library::MyDB',
39             subroutines => [ qw ( dbh load ) ]
40             );
41              
42             # get access to shared resources via proxy mechanism
43             sub something {
44             my $self = shift;
45             my $sql = $self->load('something.sql'); #located under root/sql
46             my $query = $sql->retr ( 'query' );
47             my $dbh = $self->dbh;
48             # ... do some stuff with $dbh
49             $dbh->do ( $query );
50             }
51              
52             # back in the controller
53              
54             # lib/MyApp/Controller/Other.pm
55             package MyApp::Controller::Other;
56              
57             use base 'Catalyst::Controller';
58              
59             my $model = $c->model('Other');
60             $model->something;
61            
62             =head1 DESCRIPTION
63              
64             This is the Catalyst Model Class called C<Catalyst::Model::Proxy> that
65             implements Proxy Design Pattern. It enables you to make calls to target
66             classes/subroutines via proxy mechanism. This means reduced memory footprint
67             because any operations performed on the proxies are forwarded to the
68             original complex ( target_class ) object. The target class model is also cached
69             for increased performance. For more information on the proxy design pattern
70             please refer yourself to: http://en.wikipedia.org/wiki/Proxy_design_pattern
71              
72             =head1 METHODS
73              
74             =over 4
75              
76             =item new
77              
78             Initializes DBI connection
79              
80             =cut
81              
82             sub new {
83 0     0 1   my ( $self, $c ) = @_;
84            
85 0           $self = $self->NEXT::new($c);
86 0   0       $self->{namespace} ||= ref $self;
87 0   0       $self->{additional_base_classes} ||= ();
88 0           $self->{log} = $c->log;
89 0           $self->{debug} = $c->debug;
90            
91 0           for my $sub ( @{$self->{subroutines}} ) {
  0            
92 0           my $target_class = $self->{target_class};
93 0 0         unless ( $CLASS_CACHE{$target_class}{$sub} ) {
94 0 0         $self->{log}->debug( "Installing sub:$sub from target_class:$target_class into proxy" ) if $self->{debug};
95 0           $CLASS_CACHE{$target_class}{$sub} = 1;
96 1     1   31 no strict 'refs';
  1         3  
  1         183  
97 0           *{__PACKAGE__ . "::$sub"} = sub {
98 0     0     shift;
99 0           return $c->model($target_class)->$sub(@_);
100             }
101 0           }
102             }
103 0           return $self;
104             }
105              
106             =head1 SEE ALSO
107              
108             L<Catalyst>
109              
110             =head1 AUTHOR
111              
112             Alex Pavlovic, C<alex.pavlovic@taskforce-1.com>
113              
114             =head1 COPYRIGHT
115              
116             This program is free software, you can redistribute it and/or modify it
117             under the same terms as Perl itself.
118              
119             =cut
120              
121             1;