| 1 | package DBIx::Class::Helper::Row::ToJSON; | ||||||||||||||||||||||||||||
| 2 | 56 | $DBIx::Class::Helper::Row::ToJSON::VERSION = '2.037000'; | |||||||||||||||||||||||||||
| 3 | # ABSTRACT: Remove the boilerplate from your TO_JSON functions | ||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | 56 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 56 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 56 | ▶ | use parent 'DBIx::Class::Row'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 56 | __PACKAGE__->mk_group_accessors(inherited => '_serializable_columns'); | |||||||||||||||||||||||||||
| 11 | 56 | __PACKAGE__->mk_group_accessors(inherited => '_unserializable_data_types'); | |||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 56 | my $dont_serialize = { | |||||||||||||||||||||||||||
| 14 | text => 1, | ||||||||||||||||||||||||||||
| 15 | ntext => 1, | ||||||||||||||||||||||||||||
| 16 | blob => 1, | ||||||||||||||||||||||||||||
| 17 | }; | ||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | 42 | ▶ | sub _is_column_serializable { | ||||||||||||||||||||||||||
|
Subroutine
_is_column_serializable: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||
| 20 | 42 | my ( $self, $column ) = @_; | |||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | 42 | my $info = $self->column_info($column); | |||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | 42 | ▶ | if (!defined $info->{is_serializable}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 25 | 11 | ▶ | if (defined $info->{data_type} && | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $info->{"data_type"} and $self->unserializable_data_types->{lc $info->{"data_type"}}
MC/DC50%1/2
defined $info->{"data_type"} and $self->unserializable_data_types->{lc $info->{"data_type"}}
defined $info->{"data_type"}
$self->unserializable_data_types->{lc $info->{"data_type"}}
Truth table66%2/3
A and B
A= defined $info->{"data_type"}B= $self->unserializable_data_types->{lc $info->{"data_type"}}
| |||||||||||||||||||||||||||||
| 26 | $self->unserializable_data_types->{lc $info->{data_type}} | ||||||||||||||||||||||||||||
| 27 | ) { | ||||||||||||||||||||||||||||
| 28 | 1 | $info->{is_serializable} = 0; | |||||||||||||||||||||||||||
| 29 | } else { | ||||||||||||||||||||||||||||
| 30 | 10 | $info->{is_serializable} = 1; | |||||||||||||||||||||||||||
| 31 | } | ||||||||||||||||||||||||||||
| 32 | } | ||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | 42 | return $info->{is_serializable}; | |||||||||||||||||||||||||||
| 35 | } | ||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | 14 | ▶ | sub serializable_columns { | ||||||||||||||||||||||||||
|
Subroutine
serializable_columns: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||||||||||
| 38 | 14 | my $self = shift; | |||||||||||||||||||||||||||
| 39 | 14 | ▶ | if (!$self->_serializable_columns) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 40 | 14 | $self->_serializable_columns([ | |||||||||||||||||||||||||||
| 41 | grep $self->_is_column_serializable($_), | ||||||||||||||||||||||||||||
| 42 | $self->result_source->columns | ||||||||||||||||||||||||||||
| 43 | ]); | ||||||||||||||||||||||||||||
| 44 | } | ||||||||||||||||||||||||||||
| 45 | 14 | return $self->_serializable_columns; | |||||||||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | 14 | ▶ | sub TO_JSON { | ||||||||||||||||||||||||||
|
Subroutine
TO_JSON: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||||||||||
| 49 | 14 | my $self = shift; | |||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | 14 | my $columns_info = $self->columns_info($self->serializable_columns); | |||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | return { | ||||||||||||||||||||||||||||
| 54 | map +($_ => $self->$_), | ||||||||||||||||||||||||||||
| 55 | 14 | ▶ | map +($columns_info->{$_}{accessor} || $_), | ||||||||||||||||||||||||||
|
Condition66%2/3
$columns_info->{$_}{'accessor'} || $_
MC/DC0%0/2
$columns_info->{$_}{'accessor'} || $_
$columns_info->{$_}{'accessor'}
$_
Truth table66%2/3
A || B
A= $columns_info->{$_}{'accessor'}B= $_
| |||||||||||||||||||||||||||||
| 56 | keys %$columns_info | ||||||||||||||||||||||||||||
| 57 | }; | ||||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | 9 | ▶ | sub unserializable_data_types { | ||||||||||||||||||||||||||
|
Subroutine
unserializable_data_types: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 61 | 9 | my $self = shift; | |||||||||||||||||||||||||||
| 62 | 9 | ▶ | if (!$self->_unserializable_data_types) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 63 | 3 | $self->_unserializable_data_types($dont_serialize); | |||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||
| 65 | 9 | return $self->_unserializable_data_types; | |||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | 56 | 1; | |||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | __END__ | ||||||||||||||||||||||||||||