| 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
MC/DC0%0/2
$row //= $self->row
$row
$self->row
Truth table66%2/3
A //= B
A= $rowB= $self->row
| |||||||||||||||||||||||||||||
| 34 | 33 | my $sparse_fieldset = $self->fields; | |||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | 33 | ▶ | if ($row->DOES('JSONAPI::Document::Role::Attributes')) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 37 | 1 | my $columns = $row->attributes($sparse_fieldset); | |||||||||||||||||||||||||||
| 38 | 1 | ▶ | if ($self->kebab_case_attrs) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 47 | 32 | delete $columns{id}; | |||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | 32 | ▶ | if (defined($sparse_fieldset) && @$sparse_fieldset) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $sparse_fieldset and @$sparse_fieldset
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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 53 | 16 | delete $columns{$field}; | |||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | 32 | ▶ | if ($self->kebab_case_attrs) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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; | |||||||||||||||||||||||||||