Helper/Row/ToJSON.pm

filter: Statementstmt 100% 27 / 27 Branchbran 83.3% 5 / 6 Conditioncond 83.3% 5 / 6 MC/DCmcdc 50.0% 2 / 4 Subroutinesub 100% 6 / 6 Podpod 100% 2 / 2 totaltotal 92.1% 47 / 51 CC 6 SCAR 9.0
CC
6
Cov
95%
_is_column_serializable
4.0
serializable_columns
2.0
TO_JSON
2.0
CRAP 2.5
« Helper/ResultSet/AutoRemoveColumns.pm ↑ Summary Helper/ResultSet/Shortcut/Search/Base.pm »
1 package DBIx::Class::Helper::Row::ToJSON;
2 54 $DBIx::Class::Helper::Row::ToJSON::VERSION = '2.032002';
3 # ABSTRACT: Remove the boilerplate from your TO_JSON functions
4  
5 54 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 54 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 54 ▶use parent 'DBIx::Class::Row';
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 54 __PACKAGE__->mk_group_accessors(inherited => '_serializable_columns');
11  
12 54 my $dont_serialize = {
13 text => 1,
14 ntext => 1,
15 blob => 1,
16 };
17  
18 sub _is_column_serializable {
19 36 ▶ my ( $self, $column ) = @_;
Subroutine _is_column_serializable: called CC=4 SCAR=13.9
20  
21 36 my $info = $self->column_info($column);
22  
23 36 ▶ if (!defined $info->{is_serializable}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $info->{"is_serializable"}) 9 27 36
24 9 ▶ if (defined $info->{data_type} &&
Branch: 1 branch
BranchTrueFalseTotal
if (defined $info->{'data_type'} and $$dont_serialize{lc $info->{'data_type'}}) { } 1 8 9
Condition100%3/3
defined $info->{"data_type"} and $$dont_serialize{lc $info->{"data_type"}}
!ll&&!rl&&r
171
MC/DC100%2/2
defined $info->{"data_type"} and $$dont_serialize{lc $info->{"data_type"}}
defined $info->{"data_type"} $$dont_serialize{lc $info->{"data_type"}}
Truth table100%3/3
A and B
A= defined $info->{"data_type"}B= $$dont_serialize{lc $info->{"data_type"}}
ABresult
0X0
100
111
25 $dont_serialize->{lc $info->{data_type}}
26 ) {
27 1 $info->{is_serializable} = 0;
28 } else {
29 8 $info->{is_serializable} = 1;
30 }
31 }
32  
33 36 return $info->{is_serializable};
34 }
35  
36 sub serializable_columns {
37 11 ▶ my $self = shift;
Subroutine serializable_columns: called CC=2 SCAR=7.0
Pod documented
38 11 ▶ if (!$self->_serializable_columns) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_serializable_columns) 11 0 11
39 11 $self->_serializable_columns([
40 grep $self->_is_column_serializable($_),
41 $self->result_source->columns
42 ]);
43 }
44 11 return $self->_serializable_columns;
45 }
46  
47 sub TO_JSON {
48 11 ▶ my $self = shift;
Subroutine TO_JSON: called CC=2 SCAR=7.0
Pod documented
49  
50 11 my $columns_info = $self->columns_info($self->serializable_columns);
51  
52 return {
53 map +($_ => $self->$_),
54 11 ▶ map +($columns_info->{$_}{accessor} || $_),
Condition66%2/3
$columns_info->{$_}{'accessor'} || $_
l!l&&r!l&&!r
3300
MC/DC0%0/2
$columns_info->{$_}{'accessor'} || $_
$columns_info->{$_}{'accessor'} $_
Truth table66%2/3
A || B
A= $columns_info->{$_}{'accessor'}B= $_
ABresult
1X1
011
000
55 keys %$columns_info
56 };
57 }
58  
59 54 1;
60  
61 __END__
62  
63 =pod
64  
65 =head1 NAME
66  
67 DBIx::Class::Helper::Row::ToJSON - Remove the boilerplate from your TO_JSON functions
68  
69 =head1 SYNOPSIS
70  
71 package MyApp::Schema::Result::KittenRobot;
72  
73 use parent 'DBIx::Class::Core';
74  
75 __PACKAGE__->load_components(qw{Helper::Row::ToJSON});
76  
77 __PACKAGE__->table('KittenRobot');
78 __PACKAGE__->add_columns(
79 id => {
80 data_type => 'integer',
81 is_auto_increment => 1,
82 },
83 kitten => {
84 data_type => 'integer',
85 },
86 robot => {
87 data_type => 'text',
88 is_nullable => 1,
89 },
90 your_mom => {
91 data_type => 'blob',
92 is_nullable => 1,
93 is_serializable => 1,
94 },
95 );
96  
97 1;
98  
99 This helper adds a JSON method like the following:
100  
101 sub TO_JSON {
102 return {
103 id => $self->id,
104 kitten => $self->kitten,
105 # robot => $self->robot, # <-- doesn't serialize text columns
106 your_mom => $self->your_mom, # <-- normally wouldn't but explicitly
107 # asked for in the column spec above
108 }
109 }
110  
111 =head1 METHODS
112  
113 =head2 _is_column_serializable
114  
115 $self->_is_column_serializable('kitten')
116  
117 returns true if a column should be serializable or not. Currently this marks
118 everything as serializable unless C<is_serializable> is set to false, or
119 C<data_type> is a C<blob>, C<text>, or C<ntext> columns. If you wanted to only
120 have explicit serialization you might override this method to look like this:
121  
122 sub _is_column_serializable {
123 my ( $self, $column ) = @_;
124  
125 my $info = $self->column_info($column);
126  
127 return defined $info->{is_serializable} && $info->{is_serializable};
128 }
129  
130 =head2 serializable_columns
131  
132 $self->serializable_columns
133  
134 simply returns a list of columns that TO_JSON should serialize.
135  
136 =head2 TO_JSON
137  
138 $self->TO_JSON
139  
140 returns a hashref representing your object. Override this method to add data
141 to the returned hashref:
142  
143 sub TO_JSON {
144 my $self = shift;
145  
146 return {
147 customer_name => $self->customer->name,
148 %{ $self->next::method },
149 }
150 }
151  
152 =head1 AUTHOR
153  
154 Arthur Axel "fREW" Schmidt <frioux+cpan@gmail.com>
155  
156 =head1 COPYRIGHT AND LICENSE
157  
158 This software is copyright (c) 2016 by Arthur Axel "fREW" Schmidt.
159  
160 This is free software; you can redistribute it and/or modify it under
161 the same terms as the Perl 5 programming language system itself.
162  
163 =cut
« Helper/ResultSet/AutoRemoveColumns.pm ↑ Summary Helper/ResultSet/Shortcut/Search/Base.pm »