| 1 | package Catalyst::Model::Proxy; | ||||||||||||||||||||
| 2 | |||||||||||||||||||||
| 3 | 1 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 4 | 1 | ▶ | use base 'Catalyst::Model'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 5 | 1 | ▶ | use NEXT; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 6 | |||||||||||||||||||||
| 7 | 1 | our $VERSION = '0.04'; | |||||||||||||||||||
| 8 | 1 | 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 | 0 | ▶ | sub new { | ||||||||||||||||||
|
Subroutine
new: not called CC=6 SCAR=34.7
Pod
documented
| |||||||||||||||||||||
| 83 | 0 | my ( $self, $c ) = @_; | |||||||||||||||||||
| 84 | |||||||||||||||||||||
| 85 | 0 | $self = $self->NEXT::new($c); | |||||||||||||||||||
| 86 | 0 | ▶ | $self->{namespace} ||= ref $self; | ||||||||||||||||||
|
Condition0%0/3
$self->{"namespace"} ||= ref $self
MC/DC0%0/2
$self->{"namespace"} ||= ref $self
$self->{"namespace"}
ref $self
Truth table0%0/3
A ||= B
A= $self->{"namespace"}B= ref $self
| |||||||||||||||||||||
| 87 | 0 | ▶ | $self->{additional_base_classes} ||= (); | ||||||||||||||||||
|
Condition0%0/3
$self->{"additional_base_classes"} ||= ()
MC/DC0%0/2
$self->{"additional_base_classes"} ||= ()
$self->{"additional_base_classes"}
()
Truth table0%0/3
A ||= B
A= $self->{"additional_base_classes"}B= ()
| |||||||||||||||||||||
| 88 | 0 | $self->{log} = $c->log; | |||||||||||||||||||
| 89 | 0 | $self->{debug} = $c->debug; | |||||||||||||||||||
| 90 | |||||||||||||||||||||
| 91 | 0 | for my $sub ( @{$self->{subroutines}} ) { | |||||||||||||||||||
| 92 | 0 | my $target_class = $self->{target_class}; | |||||||||||||||||||
| 93 | 0 | ▶ | unless ( $CLASS_CACHE{$target_class}{$sub} ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 94 | 0 | ▶ | $self->{log}->debug( "Installing sub:$sub from target_class:$target_class into proxy" ) if $self->{debug}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 95 | 0 | $CLASS_CACHE{$target_class}{$sub} = 1; | |||||||||||||||||||
| 96 | 1 | ▶ | no strict 'refs'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 97 | 0 | ▶ | *{__PACKAGE__ . "::$sub"} = sub { | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 98 | 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 | 1; | |||||||||||||||||||