Model/EmailStore.pm

filter: Statementstmt 75.7% 25 / 33 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 75.0% 9 / 12 Podpod n/a totaltotal 75.5% 34 / 45 CC 1 SCAR 1.8
CC
1
Cov
76%
call_plugins
2.0
_cataylst_model_email_store_import_hook
2.0
BEGIN
1.3
CRAP 1.2
« Helper/Model/EmailStore.pm ↑ Summary
1 package Catalyst::Model::EmailStore;
2  
3 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use base qw/Catalyst::Base/;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use NEXT;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use Scalar::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use Clone qw();
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 our $VERSION = '0.03';
12  
13 =head1 NAME
14  
15 Catalyst::Model::EmailStore - Email::Store Model Class
16  
17 =head1 SYNOPSIS
18  
19 # use the helper
20 create model EmailStore EmailStore dsn user password
21  
22 # lib/MyApp/Model/EmailStore.pm
23 package MyApp::Model::EmailStore;
24  
25 use base 'Catalyst::Model::EmailStore';
26  
27 __PACKAGE__->config(
28 dsn => 'dbi:Pg:dbname=myapp',
29 password => '',
30 user => 'postgres',
31 options => { AutoCommit => 1 },
32 cdbi_plugins => [ qw/AbstractCount Pager/ ],
33 upgrade_relationships => 1
34 );
35  
36 1;
37  
38 # As object method
39 $c->model('EmailStore::Address')->search(...);
40  
41 # As class method
42 MyApp::Model::EmailStore::Adress->search(...);
43  
44 =head1 DESCRIPTION
45  
46 This is the C<Email::Store> model class. It will automatically
47 subclass all model classes from the C<Email::Store> namespace
48 and import them into your application. For this purpose a class
49 is considered to be a model if it descends from C<Email::Store::DBI>.
50  
51 This class is also doing its best to upgrade the relationships of
52 all Email::Store models (i.e. MyApp::Model::EmailStore::Entity::Name
53 will have many MyApp::Model::EmailStore::Addressing objects instead
54 of Email::Store::Addressing ones). But be warned that this will not
55 affect event handlers in any way. If you want to extend or modify
56 these you are still stuck in the Email::Store namespace
57 unfortunatly.
58  
59 =head1 CAVEATS
60  
61 Due to limitations in the design of Email::Store the main model class
62 (e.g. MyApp::Model::EmailStore) is not part of the inheritance chain
63 that leads up to Class::DBI so you can't use any CDBI plugins there.
64 To alleviate this problem a config option named I<cdbi_plugins> is
65 provided. All classes named therein (without the mandatory
66 C<Class::DBI::Plugin> prefix) will be required and imported
67 into C<Email::Store::DBI>.
68  
69 Also I've take the liberty to remove the overloading of 'bool' that is
70 done automatically by CDBI and would cause $c->model( 'XXX' ) to fail.
71  
72 Upgrading relationships is dependent on an internal Class::DBI structure
73 called '__meta_info'. This structure might change in the future or might
74 already have changed in the course of CDBI development, so be prepared
75 to experience breakage if this model is used with any version of
76 Class::DBI except 3.0.1.
77  
78 I also suggest that you keep your Email::Store tables in
79 their own database and separate from your other tables
80  
81 =head1 METHODS
82  
83 =head2 new
84  
85 Initializes Email::Store::DBI and loads model classes according to
86 Email::Store->plugins. Actually it reimplements the plugin
87 mechanism of Email::Store so you are on your own if you rely on
88 modifications to this class itself. Also attempts to borg
89 all the classes.
90  
91 =cut
92  
93 2 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=2.6
94  
95 2 require Email::Store::DBI;
96 0 require Module::Pluggable::Ordered;
97  
98 {
99  
100 #BUG: this doesn't allow to refine plugins in the Catalyst model
101 #BUG: but I really don't know how to fix this
102  
103 0 ▶ sub Email::Store::call_plugins {
Subroutine call_plugins: not called CC=1 SCAR=6.9
104 0 shift; Catalyst::Model::EmailStore->call_plugins( @_ );
105 }
106  
107 package Email::Store::DBI;
108 0 ▶ sub _cataylst_model_email_store_import_hook {
Subroutine _cataylst_model_email_store_import_hook: not called CC=1 SCAR=6.9
109 0 my $class = shift;
110 0 my $to_import = shift;
111 0 $to_import->import(@_);
112 };
113  
114 # remove overloading of bool done by CDBI which will cause
115 # problems with $c->model and maybe other stuff as well
116 0 ▶ use overload bool => sub { $_[0] };
Subroutine BEGIN: called CC=1 SCAR=0.2
Subroutine __ANON__: not called CC=1 SCAR=0.2
117 }
118  
119 0 }
120  
121 sub new {
122 my $class = shift;
123 my $self = $class->NEXT::new( @_ );
124 my $c = shift;
125 my $prefix = $c . '::Model::EmailStore';
126  
127 return bless {%$self}, $class if $class =~ /^$prefix\::/;
128  
129 my %p = %{ $self };
130  
131 my $caller = caller();
132  
133 Module::Pluggable::Ordered->import
134 ( inner => 1, search_path => [ "Email::Store" ] );
135 Email::Store::DBI->import( @p{ qw/dsn user password options/ } );
136  
137  
138 # exclude Email::Store::DBI which is wrongly detected as plugin of
139 # Email::Store and import only model classes (i.e. E::S::DBI descendants)
140 # from this namespace
141  
142 my @models = grep{
143 $_ ne qw/Email::Store::DBI/ and $_->isa( qw/Email::Store::DBI/ );
144 } __PACKAGE__->plugins;
145  
146 for my $plugin ( @models ) {
147 no strict 'refs';
148 $plugin->require;
149 my $model = $plugin;
150 $model =~ s/^Email::Store/$prefix/;
151 @{"$model\::ISA"} = ( $plugin, ref( $self ) );
152 *{"$model\::new"} = sub { bless {%$self}, $model };
153 $c->components->{$model} ||= $model->new();
154 }
155  
156 $c->log->info( "Loaded Email::Store models: @models" ) if $c->debug;
157  
158 if ( $self->{upgrade_relationships} ) {
159 # upgrade table relationships
160 require Clone;
161 for my $plugin ( @models ) {
162 my $model = $plugin;
163 $model =~ s/^Email::Store/$prefix/;
164  
165 $c->log->debug( "Upgradin relationships of $model" ) if $c->debug;
166  
167 # create a clean copy of table meta information
168 my $meta = $model->__meta_info( Clone::clone( $plugin->__meta_info ) );
169  
170 # use a recursive anonymous sub to change all keys that describe
171 # class relationships from Email::Store to the newly created
172 # Catalyst models
173 my $recursive_upgrade;
174 $recursive_upgrade = sub {
175 my $href = shift;
176 my $changes = 0;
177 for ( keys %{ $href } ) {
178 if ( ref($href->{$_}) eq 'HASH' ) {
179 $changes += &$recursive_upgrade($href->{$_});
180 next;
181 }
182 if ( Scalar::Util::blessed($href->{$_}) ) {
183 $changes += &$recursive_upgrade($href->{$_});
184 # if we encountered a relationship we have to
185 # reinitialize it so that the correct accessors will
186 # be created
187 next unless $changes;
188 if ( $href->{$_}->isa( qw/Class::DBI::Relationship/ ) ) {
189 $href->{$_}->_set_up_class_data;
190 $href->{$_}->_add_triggers;
191 $href->{$_}->_add_methods;
192 }
193 # all changes to this relationship have been done
194 # so we can reset our counter here ( relationships
195 # can't be nested as far as I know )
196 $changes = 0;
197 }
198 if ( $_ =~ /class$/ ) {
199 $changes += ( $href->{$_} =~ s/Email::Store/$prefix/ );
200 }
201 }
202 return $changes;
203 };
204 &$recursive_upgrade( $meta );
205 }
206 }
207  
208 # pull in the requested cdbi plugins
209 if ( exists( $self->{cdbi_plugins} ) ) {
210 my @cdbi_plugins = map{"Class::DBI::Plugin::$_"} @{$self->{cdbi_plugins}};
211 for my $plugin ( @cdbi_plugins ) {
212 $plugin->require;
213 Email::Store::DBI->_cataylst_model_email_store_import_hook( $plugin );
214 }
215 $c->log->info( "Loaded CDBI plugins for Email::Store: @cdbi_plugins" )
216 if $c->debug;
217 }
218  
219 return $self;
220 }
221  
222 1;
223  
224 __END__
« Helper/Model/EmailStore.pm ↑ Summary