Mongol/Roles/Relations.pm

filter: Statementstmt 30.9% 17 / 55 Branchbran 0.0% 0 / 14 Conditioncond 0.0% 0 / 4 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 35.7% 5 / 14 Podpod 100% 2 / 2 totaltotal 26.3% 24 / 91 CC 10 SCAR 22.1
CC
10
Cov
23%
has_many
20.0
has_one
20.0
__ANON__
6.0
CRAP 9.1
« Mongol/Roles/Pagination.pm ↑ Summary Mongol/Roles/Core.pm »
1 package Mongol::Roles::Relations;
2  
3 1 ▶use Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use Moose::Util qw( does_role find_meta );
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 ▶use Class::Load qw( load_class );
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 ▶use Lingua::EN::Inflect qw( PL );
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 1 requires( qw( id find find_one retrieve delete ) );
11  
12 0 ▶sub has_many {
Subroutine has_many: not called CC=4 SCAR=30.0
Pod documented
13 0 my ( $class, $type, $foreign_key, $moniker ) = @_;
14  
15 0 ▶ die( 'No type defined!' )
Branch: 1 branch
BranchTrueFalseTotal
unless defined $type 0 0 0
16 unless( defined( $type ) );
17  
18 0 ▶ die( 'No foreign key defined!' )
Branch: 1 branch
BranchTrueFalseTotal
unless $foreign_key 0 0 0
19 unless( $foreign_key );
20  
21 0 ▶ $moniker = _get_moniker( $type )
Branch: 1 branch
BranchTrueFalseTotal
unless $moniker 0 0 0
22 unless( $moniker );
23  
24 0 _load_class( $type );
25  
26 0 my $meta = find_meta( $class );
27  
28 0 ▶ $meta->add_method( sprintf( 'add_%s', $moniker ) => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
29 0 my ( $self, $data, $options ) = @_;
30  
31 0 $data->{ $foreign_key } = $self->id();
32  
33 0 return $type->new( $data )
34 ->save();
35 }
36 0 );
37  
38 0 ▶ $meta->add_method( sprintf( 'get_%s', PL( $moniker ) ) => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
39 0 my ( $self, $query, $options ) = @_;
40  
41 0 ▶ $query ||= {};
Condition0%0/2
$query ||= {}
l!l
00
MC/DC0%0/1
$query ||= {}
$query
Truth table0%0/2
A
A= $query
Aresult
11
00
42 0 $query->{ $foreign_key } = $self->id();
43  
44 0 return $type->find( $query, $options );
45 }
46 0 );
47  
48 0 ▶ $meta->add_method( sprintf( 'get_%s', $moniker ) => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
49 0 my ( $self, $id ) = @_;
50  
51 0 return $type->find_one(
52 {
53 _id => $id,
54 $foreign_key => $self->id(),
55 }
56 );
57 }
58 0 );
59  
60 0 ▶ $meta->add_method( sprintf( 'remove_%s', PL( $moniker ) ) => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
61 0 my ( $self, $query ) = @_;
62  
63 0 ▶ $query ||= {};
Condition0%0/2
$query ||= {}
l!l
00
MC/DC0%0/1
$query ||= {}
$query
Truth table0%0/2
A
A= $query
Aresult
11
00
64 0 $query->{ $foreign_key } = $self->id();
65  
66 0 return $type->delete( $query );
67 }
68 0 );
69 }
70  
71 0 ▶sub has_one {
Subroutine has_one: not called CC=4 SCAR=30.0
Pod documented
72 0 my ( $class, $type, $foreign_key, $moniker ) = @_;
73  
74 0 ▶ die( 'No type defined!' )
Branch: 1 branch
BranchTrueFalseTotal
unless defined $type 0 0 0
75 unless( defined( $type ) );
76  
77 0 ▶ die( 'No foreign key defined!' )
Branch: 1 branch
BranchTrueFalseTotal
unless $foreign_key 0 0 0
78 unless( $foreign_key );
79  
80 0 ▶ $moniker = _get_moniker( $type )
Branch: 1 branch
BranchTrueFalseTotal
unless $moniker 0 0 0
81 unless( $moniker );
82  
83 0 _load_class( $type );
84  
85 0 my $meta = find_meta( $class );
86  
87 0 ▶ $meta->add_method( sprintf( 'get_%s', $moniker ) => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
88 0 my $self = shift();
89  
90 0 return $type->retrieve( $self->$foreign_key() );
91 }
92 0 );
93 }
94  
95 0 ▶sub _load_class {
Subroutine _load_class: not called CC=2 SCAR=17.9
96 0 my $type = shift();
97  
98 0 load_class( $type );
99 0 ▶ die( sprintf( '%s cannot do basic operations!', $type ) )
Branch: 1 branch
BranchTrueFalseTotal
unless does_role($type, "Mongol::Roles::Core") 0 0 0
100 unless( does_role( $type, 'Mongol::Roles::Core' ) );
101 }
102  
103 0 ▶sub _get_moniker {
Subroutine _get_moniker: not called CC=1 SCAR=6.9
104 0 my $type = shift();
105  
106 0 ( my $name = $type ) =~ s/.+:://;
107 0 return lc( $name );
108 }
109  
110 1 ▶no Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
111  
112 1 1;
113  
114 __END__
« Mongol/Roles/Pagination.pm ↑ Summary Mongol/Roles/Core.pm »