Plugin/DBIxCustom.pm

filter: Statementstmt 25.8% 8 / 31 Branchbran 0.0% 0 / 16 Conditioncond 0.0% 0 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 40.0% 2 / 5 Podpod 100% 1 / 1 totaltotal 19.6% 11 / 56 CC 10 SCAR 43.7
CC
10
Cov
16%
register
110.0
__ANON__
2.0
__ANON__
2.0
CRAP 79.0
« DBIxCustom/Model.pm ↑ Summary DBIxCustom.pm »
1 package Mojolicious::Plugin::DBIxCustom;
2 1 ▶use Mojo::Base 'Mojolicious::Plugin';
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use Mojo::Loader qw/load_class/;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 1 our $VERSION = '1.2.1';
6  
7  
8 0 ▶sub register{
Subroutine register: not called CC=10 SCAR=47.0
Pod documented
9 0 my ($self, $app, $conf) = @_;
10 0 ▶ $conf = {%{$conf}, %{$app->config->{dbi_config}}} if($app->config->{dbi_config});
Branch: 1 branch
BranchTrueFalseTotal
if $app->config->{"dbi_config"} 0 0 0
11 0 ▶ my $dbi_class = delete $conf->{dbi_class} || 'Mojolicious::DBIxCustom';
Condition0%0/2
delete $conf->{'dbi_class'} || 'Mojolicious::DBIxCustom'
l!l
00
MC/DC0%0/1
delete $conf->{'dbi_class'} || 'Mojolicious::DBIxCustom'
delete $conf->{'dbi_class'}
Truth table0%0/2
A
A= delete $conf->{'dbi_class'}
Aresult
11
00
12 0 ▶ my $model_namespace = delete $conf->{model_namespace} if($conf->{model_namespace});
Branch: 1 branch
BranchTrueFalseTotal
if $conf->{"model_namespace"} 0 0 0
13 0 ▶ my $cb = delete $conf->{cb} if($conf->{cb});
Branch: 1 branch
BranchTrueFalseTotal
if $conf->{"cb"} 0 0 0
14 0 my $dbi;
15 0 my $e = load_class($dbi_class);
16 0 ▶ if($e){
Branch: 2 branches
BranchTrueFalseTotal
if ($e) { } 0 0 0
elsif ($dbi_class->isa('DBIx::Custom')) { } 0 0 0
17 0 ▶ ref $e ? die $e : die "can't fond the module named '$dbi_class' ,inspect your installed please";
Branch: 1 branch
BranchTrueFalseTotal
ref $e ? : 0 0 0
18 0 return undef;
19 }elsif($dbi_class->isa("DBIx::Custom")){
20 0 $dbi = $dbi_class->new($conf);
21 0 ▶ $dbi->include_model($model_namespace) if($model_namespace);
Branch: 1 branch
BranchTrueFalseTotal
if $model_namespace 0 0 0
22 0 ▶ if($cb){
Branch: 1 branch
BranchTrueFalseTotal
if ($cb) 0 0 0
23 0 $cb->($dbi);
24 }
25 0 ▶ $app->helper(dbi => sub{state $dbi_connected = $dbi->connect});
Subroutine __ANON__: not called CC=1 SCAR=6.9
26 0 ▶ $app->helper(model => sub{
Subroutine __ANON__: not called CC=1 SCAR=6.9
27 0 my ($c, $model_name) = @_;
28 0 $c->dbi->model($model_name);
29 }
30 0 );
31 }else{
32 0 $app->log->fatal("dbi_class named '$dbi_class' is not a subclass for DBIx::Custom ");
33 0 die "dbi_class named '$dbi_class' is not a subclass for DBIx::Custom";
34 }
35 }
36  
37  
38 =encoding utf8
39  
40 =head1 NAME
41  
42 Mojolicious::Plugin::DBIxCustom - 链接DBIx::Custom到Mojolicious的插件
43  
44  
45 =head1 SYNOPSIS
46  
47  
48 # Mojolicious
49 $self->plugin('DBIxCustom',{
50 dsn=>"DBI:SQLite:dbname=:memory:",
51 connector=>1,## 默认使用DBIx::Connector
52 model_namespace=>"T::Model",
53 dbi_class=>"T::MyDBIxCustom"
54 });
55
56 # Mojolicious::Lite
57 plugin 'DBIxCustom',{
58 dsn=>"DBI:SQLite:dbname=:memory:",
59 connector=>1,## 默认使用DBIx::Connector
60 model_namespace=>"T::Model",
61 dbi_class=>"T::MyDBIxCustom"
62 };
63  
64  
65 =head1 METHODS
66  
67 Mojolicious::Plugin::DBIxCustom 覆盖了Mojolicious::Plugin中的register方法。
68  
69 =head2 register
70  
71 向Mojolicious中添加了两具helper:dbi和model。
72 其中dbi默认是一个DBIx::Custom对象,你也可以通过指定dbi_class来指定一个DBIx::Custom的子类,用来实例化为dbi。
73 名为model的helper是dbi中model方法的别名。
74  
75 =head1 OPTION
76  
77 这里的OPTION介绍的是register方法的参数支持的OPTION。
78  
79 =head2 DBIx::Custom初始化参数
80  
81 所有用于DBIx::Custom初始化参数都可以通过register的option来指定。
82 如:dsn、user、password、connector、option等。
83  
84 =head2 dbi_class
85  
86 dbi_class 默认为DBIx::Custom你也可以自己指定,但必须为DBIx::Custom的子类,它会被实例化得到dbi对象。
87  
88  
89 =head2 model_namespace
90  
91 model_namespace是用于作为参数来调用dbi_class的include_model方法的。调用此方法时会自动加载对应命名空间下的所有model。
92  
93 $dbi->include_model($model_namespace);
94
95 如果没有给出此参数,则不会调用include_model方法。
96  
97  
98 =head1 AUTHOR
99  
100 wfso, C<< <461663376 at qq.com> >>
101  
102 =head1 BUGS
103  
104 Please report any bugs or feature requests to C<bug-mojolicious-plugin-dbixcustom at rt.cpan.org>, or through
105 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Mojolicious-Plugin-DBIxCustom>. I will be notified, and then you'll
106 automatically be notified of progress on your bug as I make changes.
107  
108  
109  
110  
111 =head1 SUPPORT
112  
113 You can find documentation for this module with the perldoc command.
114  
115 perldoc Mojolicious::Plugin::DBIxCustom
116  
117  
118 You can also look for information at:
119  
120 =over 4
121  
122 =item * RT: CPAN's request tracker (report bugs here)
123  
124 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Mojolicious-Plugin-DBIxCustom>
125  
126 =item * AnnoCPAN: Annotated CPAN documentation
127  
128 L<http://annocpan.org/dist/Mojolicious-Plugin-DBIxCustom>
129  
130 =item * CPAN Ratings
131  
132 L<http://cpanratings.perl.org/d/Mojolicious-Plugin-DBIxCustom>
133  
134 =item * Search CPAN
135  
136 L<http://search.cpan.org/dist/Mojolicious-Plugin-DBIxCustom/>
137  
138 =back
139  
140  
141 =head1 ACKNOWLEDGEMENTS
142  
143  
144  
145  
146 =cut
147  
148 1 1; # End of Mojolicious::Plugin::DBIxCustom
« DBIxCustom/Model.pm ↑ Summary DBIxCustom.pm »