blib/lib/Catalyst/Model/Memcached.pm

filter: Statementstmt 88.3% 68 / 77 Branchbran 50.0% 10 / 20 Conditioncond 66.6% 12 / 18 MC/DCmcdc 16.6% 2 / 12 Subroutinesub 100% 19 / 19 Podpod 62.5% 5 / 8 totaltotal 75.3% 116 / 154 CC 17 SCAR 11.0
CC
17
Cov
78%
find_or_new
7.5
BUILD
4.1
create
3.6
CRAP 3.0
↑ Summary
1 package Catalyst::Model::Memcached;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use 5.0100;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 1 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 1 ▶BEGIN { extends 'Catalyst::Model' };
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Cache::Memcached::Fast;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 1 ▶use version; our $VERSION = qv('0.02');
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 1 my $primary_key;
15 my $ttl;
16  
17 1 ▶sub BUILD {
Subroutine BUILD: called CC=4 SCAR=14.1
Pod undocumented
18 1 my $self = shift;
19 # Fix namespace
20 1 $self->{__ns_chunk} = lc ref $self;
21 1 $self->{__ns_chunk} =~ /.+::([^:]+)$/;
22 1 $self->{__ns_chunk} = $1 . '.';
23 1 ▶ $self->{__cache} //= Cache::Memcached::Fast->new( $self->{args} );
Condition33%1/3
$self->{"__cache"} //= "Cache::Memcached::Fast"->new($self->{"args"})
l!l&&r!l&&!r
010
MC/DC0%0/2
$self->{"__cache"} //= "Cache::Memcached::Fast"->new($self->{"args"})
$self->{"__cache"} "Cache::Memcached::Fast"->new($self->{"args"})
Truth table33%1/3
A //= B
A= $self->{"__cache"}B= "Cache::Memcached::Fast"->new($self->{"args"})
ABresult
1X1
011
000
24 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
25 1 $self->{primary_key} = ${(ref $self) . "::primary_key"};
26 1 ▶ die 'Primary key is a must' unless $self->{primary_key};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"primary_key"} 0 1 1
27 1 ▶ $self->{ttl} = ${(ref $self) . "::ttl"} if ${(ref $self) . "::ttl"};
Branch: 1 branch
BranchTrueFalseTotal
if ${ref($self) . '::ttl';} 1 0 1
28 1 return $self;
29 }
30  
31 #######################################################################
32 # Wrapper methods - imitating DBIx schema
33 2 ▶sub search {
Subroutine search: called CC=3 SCAR=12.0
Pod documented
34 2 my ( $self, $hash ) = @_;
35 2 ▶ if ( ref $hash ne 'HASH' || ! $hash->{ $self->{primary_key} } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $hash ne "HASH" or not $$hash{$self->{"primary_key"}}) 2 0 2
Condition66%2/3
ref $hash ne "HASH" or not $$hash{$self->{"primary_key"}}
l!l&&r!l&&!r
110
MC/DC0%0/2
ref $hash ne "HASH" or not $$hash{$self->{"primary_key"}}
ref $hash ne "HASH" not $$hash{$self->{"primary_key"}}
Truth table66%2/3
A or not(B)
A= ref $hash ne "HASH"B= not $$hash{$self->{"primary_key"}}
ABresult
1X1
011
000
36 2 die 'Search needs hash ref with primary_key';
37 }
38 0 return $self->{__cache}->get( $self->{__ns_chunk} . $hash->{ $self->{primary_key} } );
39 }
40  
41 3 ▶sub find {
Subroutine find: called CC=3 SCAR=11.0
Pod documented
42 3 my ( $self, $hash ) = @_;
43 3 ▶ if ( ref $hash ne 'HASH' || ! $hash->{ $self->{primary_key} } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $hash ne "HASH" or not $$hash{$self->{"primary_key"}}) 2 1 3
Condition100%3/3
ref $hash ne "HASH" or not $$hash{$self->{"primary_key"}}
l!l&&r!l&&!r
111
MC/DC100%2/2
ref $hash ne "HASH" or not $$hash{$self->{"primary_key"}}
ref $hash ne "HASH" not $$hash{$self->{"primary_key"}}
Truth table100%3/3
A or not(B)
A= ref $hash ne "HASH"B= not $$hash{$self->{"primary_key"}}
ABresult
1X1
011
000
44 2 die 'Find needs hash ref with primary_key';
45 }
46 1 return $self->{__cache}->get( $self->{__ns_chunk} . $hash->{ $self->{primary_key} } );
47 }
48  
49 2 ▶sub find_or_new {
Subroutine find_or_new: called CC=4 SCAR=20.1
Pod documented
50 2 my ( $self, $hash ) = @_;
51 2 ▶ if ( ref $hash ne 'HASH' || ! $hash->{ $self->{primary_key} } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $hash ne "HASH" or not $$hash{$self->{"primary_key"}}) 2 0 2
Condition66%2/3
ref $hash ne "HASH" or not $$hash{$self->{"primary_key"}}
l!l&&r!l&&!r
110
MC/DC0%0/2
ref $hash ne "HASH" or not $$hash{$self->{"primary_key"}}
ref $hash ne "HASH" not $$hash{$self->{"primary_key"}}
Truth table66%2/3
A or not(B)
A= ref $hash ne "HASH"B= not $$hash{$self->{"primary_key"}}
ABresult
1X1
011
000
52 2 die 'Find_or_new needs hash ref with primary_key';
53 }
54 0 my $res = $self->find( $hash );
55 0 ▶ unless ( $res ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($res) 0 0 0
56 0 $self->create( $hash );
57 0 $res = $hash;
58 }
59 0 return $res;
60 }
61  
62 2 ▶sub create {
Subroutine create: called CC=3 SCAR=12.7
Pod documented
63 2 my ( $self, $hash ) = @_;
64 2 ▶ if ( ref $hash ne 'HASH' || ! $hash->{ $self->{primary_key} } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $hash ne "HASH" or not $$hash{$self->{"primary_key"}}) 2 0 2
Condition66%2/3
ref $hash ne "HASH" or not $$hash{$self->{"primary_key"}}
l!l&&r!l&&!r
110
MC/DC0%0/2
ref $hash ne "HASH" or not $$hash{$self->{"primary_key"}}
ref $hash ne "HASH" not $$hash{$self->{"primary_key"}}
Truth table66%2/3
A or not(B)
A= ref $hash ne "HASH"B= not $$hash{$self->{"primary_key"}}
ABresult
1X1
011
000
65 2 die 'Create needs hash ref';
66 }
67 0 $self->{__cache}->set( $self->{__ns_chunk} . $hash->{ $self->{primary_key} }, $hash, $self->{ttl} );
68 0 return $hash;
69 }
70  
71 2 ▶sub delete {
Subroutine delete: called CC=3 SCAR=12.0
Pod documented
72 2 my ( $self, $hash ) = @_;
73 2 ▶ if ( ref $hash ne 'HASH' || ! $hash->{ $self->{primary_key} } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $hash ne "HASH" or not $$hash{$self->{"primary_key"}}) 2 0 2
Condition66%2/3
ref $hash ne "HASH" or not $$hash{$self->{"primary_key"}}
l!l&&r!l&&!r
110
MC/DC0%0/2
ref $hash ne "HASH" or not $$hash{$self->{"primary_key"}}
ref $hash ne "HASH" not $$hash{$self->{"primary_key"}}
Truth table66%2/3
A or not(B)
A= ref $hash ne "HASH"B= not $$hash{$self->{"primary_key"}}
ABresult
1X1
011
000
74 2 die 'Delete needs hash ref';
75 }
76 0 return $self->{__cache}->delete( $self->{__ns_chunk} . $hash->{ $self->{primary_key} } );
77  
78 }
79  
80 #######################################################################
81 # internals
82 1 ▶sub set_primary_key {
Subroutine set_primary_key: called CC=2 SCAR=7.0
Pod undocumented
83 1 my ( $class, $pk ) = @_;
84 1 ▶ $pk = $pk->[0] if ref $pk eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $pk eq "ARRAY" 0 1 1
85 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
86 1 ${$class."::primary_key"} = $pk;
87 1 return 1;
88 }
89 1 ▶sub set_ttl {
Subroutine set_ttl: called CC=2 SCAR=7.0
Pod undocumented
90 1 my ( $class, $pk ) = @_;
91 1 ▶ $pk = $pk->[0] if ref $pk eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $pk eq "ARRAY" 0 1 1
92 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
93 1 ${$class."::ttl"} = $pk;
94 1 return 1;
95 }
96  
97 1 ▶END { } # module clean-up code
Subroutine END: called CC=1 SCAR=0.0
98  
99 1 1;
100  
101 __END__
↑ Summary