Document/Builder/Role/Attributes.pm

filter: Statementstmt 97.0% 33 / 34 Branchbran 83.3% 10 / 12 Conditioncond 83.3% 5 / 6 MC/DCmcdc 50.0% 2 / 4 Subroutinesub 100% 5 / 5 Podpod 50.0% 1 / 2 totaltotal 88.8% 56 / 63 CC 11 SCAR 19.8
CC
11
Cov
92%
get_attributes
10.1
kebab_case
2.0
BEGIN
1.0
CRAP 7.2
« Document/Builder/Compound.pm ↑ Summary Document.pm »
1 package JSONAPI::Document::Builder::Role::Attributes;
2 6 $JSONAPI::Document::Builder::Role::Attributes::VERSION = '2.0';
3 =head1 NAME
4  
5 JSONAPI::Document::Builder::Role::Attributes - Utility role for JSON API attributes
6  
7 =head1 VERSION
8  
9 version 2.0
10  
11 =head1 DESCRIPTION
12  
13 Provides methods to retrieve and manipulate a resource documents attributes.
14  
15 =cut
16  
17 6 ▶use Moo::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 6 ▶use List::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 =head2 get_attributes(DBIx::Class::Row $row?) : HashRef
22  
23 Retrieves the attributes for a given row.
24  
25 Does all of the field manipulation according to the
26 relevant attributes of the consuming class, such as
27 returning a subset of fields, casing, etc.
28  
29 =cut
30  
31 sub get_attributes {
32 33 ▶ my ($self, $row) = @_;
Subroutine get_attributes: called CC=10 SCAR=23.2
Pod undocumented
33 33 ▶ $row //= $self->row;
Condition66%2/3
$row //= $self->row
l!l&&r!l&&!r
14190
MC/DC0%0/2
$row //= $self->row
$row $self->row
Truth table66%2/3
A //= B
A= $rowB= $self->row
ABresult
1X1
011
000
34 33 my $sparse_fieldset = $self->fields;
35  
36 33 ▶ if ($row->DOES('JSONAPI::Document::Role::Attributes')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($row->DOES("JSONAPI::Document::Role::Attributes")) 1 32 33
37 1 my $columns = $row->attributes($sparse_fieldset);
38 1 ▶ if ($self->kebab_case_attrs) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->kebab_case_attrs) 0 1 1
39 0 return { $self->kebab_case(%$columns) };
40 }
41 1 return $columns;
42 }
43  
44 32 my %columns = $row->get_inflated_columns();
45  
46 32 ▶ if ($columns{id}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($columns{"id"}) 32 0 32
47 32 delete $columns{id};
48 }
49  
50 32 ▶ if (defined($sparse_fieldset) && @$sparse_fieldset) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $sparse_fieldset and @$sparse_fieldset) 6 26 32
Condition100%3/3
defined $sparse_fieldset and @$sparse_fieldset
!ll&&!rl&&r
12146
MC/DC100%2/2
defined $sparse_fieldset and @$sparse_fieldset
defined $sparse_fieldset @$sparse_fieldset
Truth table100%3/3
A and B
A= defined $sparse_fieldsetB= @$sparse_fieldset
ABresult
0X0
100
111
51 6 for my $field (keys(%columns)) {
52 22 ▶ unless (List::Util::first { $_ eq $field } @$sparse_fieldset) {
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
unless (&List::Util::first(sub { $_ eq $field; } , @$sparse_fieldset)) 16 6 22
53 16 delete $columns{$field};
54 }
55 }
56 }
57  
58 32 ▶ if ($self->kebab_case_attrs) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->kebab_case_attrs) 3 29 32
59 3 return { $self->kebab_case(%columns) };
60 }
61 29 return \%columns;
62 }
63  
64 =head2 kebab_case(Hash $row) : Hash
65  
66 Takes the keys of the given row and dash cases
67 them.
68  
69 =cut
70  
71 sub kebab_case {
72 3 ▶ my ($self, %row) = @_;
Subroutine kebab_case: called CC=2 SCAR=6.9
Pod documented
73 3 my %new_row;
74 3 foreach my $column (keys(%row)) {
75 7 my $value = $row{$column};
76 7 $column =~ s/_/-/g;
77 7 $new_row{$column} = $value;
78 }
79 3 return %new_row;
80 }
81  
82 6 1;
« Document/Builder/Compound.pm ↑ Summary Document.pm »