Model/KiokuDB.pm

filter: Statementstmt 98.0% 98 / 100 Branchbran 50.0% 10 / 20 Conditioncond 50.0% 3 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 100% 18 / 18 Podpod 20.0% 1 / 5 totaltotal 84.9% 130 / 153 CC 16 SCAR 10.8
CC
16
Cov
88%
BUILD
5.2
__ANON__
5.1
scope_guard_needed
3.7
CRAP 2.9
« Authentication/Store/Model/KiokuDB/UserWrapper.pm ↑ Summary Authentication/Store/Model/KiokuDB.pm »
1 package Catalyst::Model::KiokuDB;
2 1 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 1 ▶use KiokuX::Model;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use Scope::Guard;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Scalar::Util qw(weaken);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use overload ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Hash::Util::FieldHash::Compat qw(fieldhash);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 sub format_table;
12  
13 1 ▶use namespace::clean -except => 'meta';
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 1 our $VERSION = "0.12";
16  
17 1 extends qw(Catalyst::Model);
18  
19 1 fieldhash my %scopes;
20  
21 8 ▶sub ACCEPT_CONTEXT {
Subroutine ACCEPT_CONTEXT: called CC=2 SCAR=7.1
Pod documented
22 8 my ($self, $c, @args) = @_;
23  
24 8 ▶ $self->save_scope($c) if $self->manage_scope;
Branch: 1 branch
BranchTrueFalseTotal
if $self->manage_scope 8 0 8
25  
26 8 return $self->model;
27 }
28  
29 1 has manage_scope => (
30 isa => "Bool",
31 is => "ro",
32 default => 1,
33 );
34  
35 1 has clear_leaks => (
36 isa => "Bool",
37 is => "ro",
38 default => 1,
39 );
40  
41 1 has report_leaks => (
42 isa => "Bool",
43 is => "ro",
44 default => 1,
45 );
46  
47 1 has report_loads => (
48 isa => "Bool",
49 is => "ro",
50 default => 1,
51 );
52  
53 6 ▶sub scope_guard_needed {
Subroutine scope_guard_needed: called CC=3 SCAR=13.1
Pod undocumented
54 6 my $self = shift;
55  
56 6 ▶ $self->clear_leaks || $self->report_leaks || $self->report_loads;
Branch: 1 branch
BranchTrueFalseTotal
$self->clear_leaks or $self->report_leaks or $self->report_loads 6 0 6
Condition33%1/3
$self->clear_leaks or $self->report_leaks
l!l&&r!l&&!r
600
MC/DC0%0/2
$self->clear_leaks or $self->report_leaks
$self->clear_leaks $self->report_leaks
Truth table33%1/3
A or B
A= $self->clear_leaksB= $self->report_leaks
ABresult
1X1
011
000
57 }
58  
59 1 has model => (
60 isa => "KiokuX::Model",
61 is => "ro",
62 predicate => "has_model",
63 writer => "_model",
64 handles => "KiokuDB::Role::API",
65 );
66  
67 1 has model_class => (
68 isa => "ClassName",
69 is => "ro",
70 default => "KiokuX::Model",
71 );
72  
73 has model_args => (
74 isa => "HashRef",
75 is => "ro",
76 1 ▶ default => sub { +{} },
Subroutine __ANON__: called CC=1 SCAR=0.0
77 1 );
78  
79 1 has dsn => (
80 is => "ro",
81 predicate => "has_dsn",
82 );
83  
84 1 ▶sub BUILD {
Subroutine BUILD: called CC=5 SCAR=16.4
Pod undocumented
85 1 my ( $self, $params ) = @_;
86  
87 1 ▶ unless ( $self->has_model ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->has_model) 1 0 1
88 # Don't pass Catalyst specific parameters into the model, as this will
89 # break things using MX::StrictConstructor
90 1 my %params = %$params;
91 1 delete $params{$_} for (grep { /^_?catalyst/ } keys %params);
92  
93 # don't pass parameters to our constructor
94 1 delete @params{grep { defined } map { $_->init_arg } $self->meta->get_all_attributes};
95  
96 1 ▶ if ( scalar keys %params ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar keys %params) 0 1 1
97 0 carp("Passing extra parameters to the constructor is deprecated, please use model_args");
98 }
99  
100 my $model = $self->_new_model(
101 $self->has_dsn ? ( dsn => $self->dsn ) : (),
102 %params,
103 1 ▶ %{ $self->model_args },
Branch: 1 branch
BranchTrueFalseTotal
$self->has_dsn ? : 1 0 1
104 );
105  
106 1 my $l = $model->directory->live_objects;
107  
108 1 $l->clear_leaks($self->clear_leaks);
109  
110 1 $self->_model($model);
111 }
112 }
113  
114 1 ▶sub _new_model {
Subroutine _new_model: called CC=1 SCAR=0.0
115 1 my ( $self, @args ) = @_;
116  
117 1 $self->model_class->new(@args);
118 }
119  
120 8 ▶sub save_scope {
Subroutine save_scope: called CC=3 SCAR=11.2
Pod undocumented
121 8 my ( $self, $c ) = @_;
122  
123 8 my $dir = $self->directory;
124  
125 # make sure a live object scope for this kiokudb directory exists
126 8 ▶ $scopes{$c}{overload::StrVal($dir)} ||= do {
Condition66%2/3
$scopes{$c}{overload::StrVal($dir)} ||= do { my $scope = $dir->new_scope; $self->scope_guard_needed ? $self->setup_scope_guard($c, $scope) : $scope }
l!l&&r!l&&!r
260
MC/DC0%0/2
$scopes{$c}{overload::StrVal($dir)} ||= do { my $scope = $dir->new_scope; $self->scope_guard_needed ? $self->setup_scope_guard($c, $scope) : $scope }
$scopes{$c}{overload::StrVal($dir)} do { my $scope = $dir->new_scope; $self->scope_guard_needed ? $self->setup_scope_guard($c, $scope) : $scope }
Truth table66%2/3
A ||= B
A= $scopes{$c}{overload::StrVal($dir)}B= do { my $scope = $dir->new_scope; $self->scope_guard_needed ? $self->setup_scope_guard($c, $scope) : $scope }
ABresult
1X1
011
000
127 6 my $scope = $dir->new_scope;
128  
129 6 ▶ $self->scope_guard_needed ? $self->setup_scope_guard($c, $scope) : $scope;
Branch: 1 branch
BranchTrueFalseTotal
$self->scope_guard_needed ? : 6 0 6
130 };
131 }
132  
133 7 ▶sub format_table {
Subroutine format_table: called CC=3 SCAR=11.0
134 7 my @objects = @_;
135  
136 7 require Text::SimpleTable;
137 7 my $t = Text::SimpleTable->new( [ 60, 'Class' ], [ 8, 'Count' ] );
138  
139 7 my %counts;
140 7 $counts{ref($_)}++ for @objects;
141  
142 7 foreach my $class ( sort { $counts{$b} <=> $counts{$a} } keys %counts ) {
143 8 $t->row( $class, $counts{$class} );
144 }
145  
146 7 return $t->draw;
147 }
148  
149 6 ▶sub setup_scope_guard {
Subroutine setup_scope_guard: called CC=1 SCAR=0.0
Pod undocumented
150 6 my ( $self, $c, $scope ) = @_;
151  
152 # gotta capture this early to avoid leaking $c
153 6 my $log = $c->log;
154 6 my $debug = $c->debug;
155 6 my $stash = $c->stash;
156  
157 6 ▶ return Scope::Guard->new(sub {
Subroutine __ANON__: called CC=5 SCAR=16.3
158 # we need to be sure all real references to the objects are cleared
159 # if the stash clearing is problematic clear_leaks, report_leaks and
160 # report_loads should be disabled
161 6 %$stash = ();
162  
163 6 my $l = $scope->live_objects;
164  
165 6 ▶ if ( $debug ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($debug) 6 0 6
166 6 my @live_objects = $l->live_objects;
167  
168 6 my $msg = "Loaded " . scalar(@live_objects) . " objects:\n" . format_table(@live_objects);
169  
170 6 $log->debug($msg);
171  
172 6 @live_objects = ();
173 }
174  
175 6 my ( $prev_tracker, $prev_clear );
176 6 ▶ if ( $self->report_leaks ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->report_leaks) 6 0 6
177 6 $prev_tracker = $l->leak_tracker;
178 1 ▶ $l->leak_tracker(sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
179 1 my @leaked_objects = @_;
180 1 $log->warn("leaked objects:\n" . format_table(@leaked_objects));
181 6 });
182 }
183  
184 6 $prev_clear = $l->clear_leaks;
185 6 $l->clear_leaks($self->clear_leaks);
186  
187 6 $scope->remove;
188  
189 6 ▶ if ( $self->report_leaks ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->report_leaks) 6 0 6
190 6 ▶ if ( $prev_tracker ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($prev_tracker) { } 0 6 6
191 0 $l->leak_tracker($prev_tracker);
192 } else {
193 6 $l->clear_leak_tracker;
194 }
195 }
196  
197 6 $l->clear_leaks($prev_clear);
198 6 });
199 }
200  
201 1 __PACKAGE__->meta->make_immutable;
202  
203 1 __PACKAGE__
204  
205 __END__
« Authentication/Store/Model/KiokuDB/UserWrapper.pm ↑ Summary Authentication/Store/Model/KiokuDB.pm »