Model.pm

filter: Statementstmt 69.3% 43 / 62 Branchbran 25.0% 6 / 24 Conditioncond 40.0% 4 / 10 MC/DCmcdc 16.6% 1 / 6 Subroutinesub 80.0% 8 / 10 Podpod 100% 4 / 4 totaltotal 56.9% 66 / 116 CC 19 SCAR 29.1
CC
19
Cov
55%
get
39.6
AUTOLOAD
20.0
toCsv
4.1
CRAP 18.3
↑ Summary Collection/Comparator.pm »
1 package Mojo::Util::Model;
2 32 ▶use Mojo::Base -base;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 32 our $VERSION = '0.0.19';
5  
6 32 ▶use Mojo::JSON qw(encode_json);
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 8 ▶has 'exists' => sub {
Subroutine __ANON__: called CC=2 SCAR=7.6
9 8 ▶ return shift->pk ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
(shift())->pk ? : 8 0 8
10 32 };
11  
12 32 has 'id';
13  
14 8 ▶has 'keys_to_serialize' => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
15 8 my $self = shift;
16  
17 8 my @keys = keys(%$self);
18  
19 8 push(@keys, 'exists', 'pk', 'primary_key');
20  
21 8 return \@keys;
22 32 };
23  
24 19 ▶has 'pk' => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
25 19 my $self = shift;
26  
27 19 return $self->get($self->primary_key);
28 32 };
29  
30 32 has 'primary_key' => 'id';
31  
32 =head2 get
33  
34 Returns the value of a field.
35  
36 =cut
37  
38 339 ▶sub get {
Subroutine get: called CC=10 SCAR=36.8
Pod documented
39 339 my ($self, $field, $default) = @_;
40  
41 339 my $value = undef;
42 339 ▶ $default //= undef;
Condition100%2/2
$default //= undef
l!l
28311
MC/DC100%1/1
$default //= undef
$default
Truth table100%2/2
A
A= $default
Aresult
11
00
43  
44 339 my @pieces = split(/\./, $field);
45  
46 339 ▶ if (scalar(@pieces) > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @pieces > 1) 0 339 339
47 0 $field = shift @pieces;
48  
49 0 ▶ if ($self->can($field)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->can($field)) 0 0 0
50 0 ▶ if (ref($self->$field) eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self->$field eq "HASH") 0 0 0
51 0 my $tmp = $self->$field;
52  
53 0 ▶ while (ref($tmp) eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $tmp eq "HASH") 0 0 0
54 0 $field = shift @pieces;
55 0 $tmp = $tmp->{ $field };
56 }
57  
58 0 ▶ return $tmp || $default;
Condition0%0/3
$tmp || $default
l!l&&r!l&&!r
000
MC/DC0%0/2
$tmp || $default
$tmp $default
Truth table0%0/3
A || B
A= $tmpB= $default
ABresult
1X1
011
000
59 }
60  
61 0 return $self->$field->get(join('.', @pieces), $default);
62 }
63  
64 0 return $default;
65 }
66  
67 339 ▶ if ($self->can($field)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->can($field)) { } 339 0 339
68 339 $value = $self->$field;
69 } else {
70 0 ▶ $value = $self->{ $field } if (exists $self->{ $field });
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{$field} 0 0 0
71 }
72  
73 339 ▶ return $value // $default;
Condition33%1/3
$value // $default
l!l&&r!l&&!r
33900
MC/DC0%0/2
$value // $default
$value $default
Truth table33%1/3
A // B
A= $valueB= $default
ABresult
1X1
011
000
74 }
75  
76 =head2 serialize
77  
78 Returns a hashref representation of the model.
79  
80 =cut
81  
82 16 ▶sub serialize {
Subroutine serialize: called CC=3 SCAR=11.0
Pod documented
83 16 my $self = shift;
84  
85 16 my @fields = @_;
86  
87 16 ▶ if (!scalar(@fields)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (scalar @fields) 8 8 16
88 8 @fields = @{ $self->keys_to_serialize };
89 }
90  
91 16 my $result = {};
92  
93 16 $result->{ $_ } = $self->get($_) for (@fields);
94  
95 16 return $result;
96 }
97  
98 =head2 toCsv
99  
100 Returns a CSV representation of the model.
101  
102 =cut
103  
104 4 ▶sub toCsv {
Subroutine toCsv: called CC=4 SCAR=14.1
Pod documented
105 4 my ($self, $columns, $options) = @_;
106  
107 4 ▶ my $default = defined($options->{ default }) ? $options->{ default } : 'n/a';
Branch: 1 branch
BranchTrueFalseTotal
defined $options->{'default'} ? : 0 4 4
108 4 ▶ my $quote_char = $options->{ quote_char } // '"';
Condition50%1/2
$options->{"quote_char"} // "\""
l!l
04
MC/DC0%0/1
$options->{"quote_char"} // "\""
$options->{"quote_char"}
Truth table50%1/2
A
A= $options->{"quote_char"}
Aresult
11
00
109  
110 4 my @array;
111  
112 4 foreach my $column (@$columns) {
113 12 push(@array, sprintf('%s%s%s', $quote_char, $self->get($column, $default), $quote_char));
114 }
115  
116 4 return join(',', @array);
117 }
118  
119 =head2 toJson
120  
121 Returns a JSON representation of the model.
122  
123 =cut
124  
125 0 ▶sub toJson {
Subroutine toJson: not called CC=1 SCAR=6.9
Pod documented
126 0 return encode_json(shift->serialize);
127 }
128  
129 =head2 AUTOLOAD
130  
131 Try to get a field from the model or die.
132  
133 =cut
134  
135 0 ▶sub AUTOLOAD {
Subroutine AUTOLOAD: not called CC=4 SCAR=30.0
136 0 my $self = shift;
137 0 our $AUTOLOAD;
138 0 ▶ my $field = $AUTOLOAD =~ /::(\w+)$/ ? $1 : undef;
Branch: 1 branch
BranchTrueFalseTotal
$Mojo::Util::Model::AUTOLOAD =~ /::(\w+)$/u ? : 0 0 0
139  
140 0 $field =~ s/.*:://;
141 0 ▶ return unless $field =~ /[^A-Z]/; # skip DESTROY and all-cap methods
Branch: 1 branch
BranchTrueFalseTotal
unless $field =~ /[^A-Z]/u 0 0 0
142  
143 0 ▶ return $self->get($field) if (exists $self->{ $field });
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{$field} 0 0 0
144  
145 0 die "Undefined method $AUTOLOAD";
146 }
147  
148 32 1;
↑ Summary Collection/Comparator.pm »