File Coverage

blib/lib/DBIx/Class/InflateColumn/JSON2Object/Role/Storable.pm
Criterion Covered Total %
statement 45 45 100.0
branch 10 12 83.3
condition 5 9 55.5
subroutine 12 12 100.0
pod 0 6 0.0
total 72 84 85.7


line stmt bran cond sub pod time code
1             package DBIx::Class::InflateColumn::JSON2Object::Role::Storable;
2              
3             # ABSTRACT: simplified MooseX::Storage clone with enhanced JSON boolean handling
4             our $VERSION = '0.906'; # VERSION
5              
6 5     5   2502605 use 5.014;
  5         29  
7              
8 5     5   2562 use Moose::Role;
  5         25917  
  5         28  
9              
10 5     5   30083 use DBIx::Class::InflateColumn::JSON2Object::Trait::NoSerialize;
  5         17  
  5         158  
11 5     5   529 use JSON::MaybeXS;
  5         6007  
  5         339  
12 5     5   2429 use String::CamelCase qw(camelize decamelize);
  5         2982  
  5         472  
13              
14 5     5   45 use Moose::Util::TypeConstraints;
  5         11  
  5         37  
15              
16             subtype 'InflateColumnJSONBool', as 'Ref';
17             coerce 'InflateColumnJSONBool',
18             from 'Str',
19             via { $_ ? JSON->true : JSON->false };
20             coerce 'InflateColumnJSONBool',
21             from 'Int',
22             via { $_ ? JSON->true : JSON->false };
23             coerce 'InflateColumnJSONBool', from 'Undef', via { JSON->false };
24              
25             sub freeze {
26 19     19 0 62 my ($self) = @_;
27              
28 19         72 my $payload = $self->pack;
29 19         170 my $json = JSON::MaybeXS->new->utf8->convert_blessed->encode($payload);
30              
31             # stolen from MooseX::Storage
32 19 50 33     627 utf8::decode($json) if !utf8::is_utf8($json) and utf8::valid($json);
33 19         288 return $json;
34             }
35              
36             sub thaw {
37 14     14 0 41 my ( $class, $payload ) = @_;
38              
39             # stolen from MooseX::Storage
40 14 100       81 utf8::encode($payload) if utf8::is_utf8($payload);
41              
42 14 100       149 $payload = decode_json($payload) unless ref($payload);
43 14         282 return $class->new($payload);
44             }
45              
46             sub pack {
47 25     25 0 63 my ($self) = @_;
48              
49 25         59 my $payload = {};
50 25         131 foreach my $attribute ( $self->meta->get_all_attributes ) {
51             next
52 64 50       2267 if $attribute->does(
53             'DBIx::Class::InflateColumn::Trait::NoSerialize');
54 64         453272 my $val = $attribute->get_value($self);
55 64 100       9526 next unless defined $val;
56              
57 57         1980 my $type = $attribute->type_constraint;
58 57 100 100     625 if ($type eq 'Int' || $type eq 'Num') {
59 7         339 $val = 1 * $val;
60             }
61 57         3950 $payload->{ $attribute->name } = $val;
62             }
63 25         133 return $payload;
64             }
65              
66             sub moniker {
67 2     2 0 11 my ($self) = @_;
68 2   33     10 my $class = ref($self) || $self;
69 2         22 $class =~ /::([^:]+)$/;
70 2         12 return decamelize($1);
71             }
72              
73             sub package {
74 6     6 0 104 my ( $class, $moniker ) = @_;
75 6         28 return $class . '::' . camelize($moniker);
76             }
77              
78             sub TO_JSON {
79 6     6 0 108 my $self = shift;
80 6         21 return $self->pack;
81             }
82              
83             1;
84              
85             __END__
86              
87             =pod
88              
89             =encoding UTF-8
90              
91             =head1 NAME
92              
93             DBIx::Class::InflateColumn::JSON2Object::Role::Storable - simplified MooseX::Storage clone with enhanced JSON boolean handling
94              
95             =head1 VERSION
96              
97             version 0.906
98              
99             =head1 NAME
100              
101             DBIx::Class::InflateColumn::JSON2Object::Role::Storable - simplified MooseX::Storage clone with enhanced JSON boolean handling
102              
103             =head1 VERSION
104              
105             version 0.900
106              
107             =head1 AUTHOR
108              
109             Thomas Klausner <domm@cpan.org>
110              
111             =head1 COPYRIGHT AND LICENSE
112              
113             This software is copyright (c) 2017 by Thomas Klausner.
114              
115             This is free software; you can redistribute it and/or modify it under
116             the same terms as the Perl 5 programming language system itself.
117              
118             =head1 AUTHOR
119              
120             Thomas Klausner <domm@plix.at>
121              
122             =head1 COPYRIGHT AND LICENSE
123              
124             This software is copyright (c) 2017 - 2021 by Thomas Klausner.
125              
126             This is free software; you can redistribute it and/or modify it under
127             the same terms as the Perl 5 programming language system itself.
128              
129             =cut