| 1 | package DBIx::Class::InflateColumn::JSON2Object::Role::Storable; | ||
| 2 | |||
| 3 | # ABSTRACT: simplified MooseX::Storage clone with enhanced JSON boolean handling | ||
| 4 | |||
| 5 | use Moose::Role; | ||
| 6 | |||
| 7 | use DBIx::Class::InflateColumn::JSON2Object::Trait::NoSerialize; | ||
| 8 | use JSON::MaybeXS; | ||
| 9 | use String::CamelCase qw(camelize decamelize); | ||
| 10 | |||
| 11 | use Moose::Util::TypeConstraints; | ||
| 12 | |||
| 13 | subtype 'InflateColumnJSONBool', | ||
| 14 | as 'Ref'; | ||
| 15 | coerce 'InflateColumnJSONBool', | ||
| 16 | from 'Str', | ||
| 17 | via { $_ ? JSON->true : JSON->false }; | ||
| 18 | coerce 'InflateColumnJSONBool', | ||
| 19 | from 'Int', | ||
| 20 | via { $_ ? JSON->true : JSON->false }; | ||
| 21 | |||
| 22 | sub freeze { | ||
| 23 | my ($self) = @_; | ||
| 24 | |||
| 25 | my $payload = $self->pack; | ||
| 26 | my $json = encode_json($payload); | ||
| 27 | |||
| 28 | # stolen from MooseX::Storage | ||
| 29 | utf8::decode($json) if !utf8::is_utf8($json) and utf8::valid($json); | ||
| 30 | return $json; | ||
| 31 | } | ||
| 32 | |||
| 33 | sub thaw { | ||
| 34 | my ($class,$payload) = @_; | ||
| 35 | |||
| 36 | # stolen from MooseX::Storage | ||
| 37 | utf8::encode($payload) if utf8::is_utf8($payload); | ||
| 38 | |||
| 39 | $payload = decode_json($payload) unless ref($payload); | ||
| 40 | return $class->new($payload); | ||
| 41 | } | ||
| 42 | |||
| 43 | sub pack { | ||
| 44 | my ($self) = @_; | ||
| 45 | |||
| 46 | my $payload = {}; | ||
| 47 | foreach my $attribute ($self->meta->get_all_attributes) { | ||
| 48 | next | ||
| 49 | if $attribute->does('DBIx::Class::InflateColumn::Trait::NoSerialize'); | ||
| 50 | my $val = $attribute->get_value($self); | ||
| 51 | $payload->{$attribute->name} = $val if defined $val; | ||
| 52 | } | ||
| 53 | return $payload; | ||
| 54 | } | ||
| 55 | |||
| 56 | sub moniker { | ||
| 57 | my ($self) = @_; | ||
| 58 | my $class = ref($self) || $self; | ||
| 59 | $class =~ /::([^:]+)$/; | ||
| 60 | return decamelize($1); | ||
| 61 | } | ||
| 62 | |||
| 63 | sub package { | ||
| 64 | my ($class,$moniker) = @_; | ||
| 65 | return $class.'::'.camelize($moniker) | ||
| 66 | } | ||
| 67 | |||
| 68 | 1; | ||
| 69 | |||
| 70 | __END__ | ||
| 71 | |||
| 72 | =pod | ||
| 73 | |||
| 74 | =encoding UTF-8 | ||
| 75 | |||
| 76 | =head1 NAME | ||
| 77 | |||
| 78 | DBIx::Class::InflateColumn::JSON2Object::Role::Storable - simplified MooseX::Storage clone with enhanced JSON boolean handling | ||
| 79 | |||
| 80 | =head1 VERSION | ||
| 81 | |||
| 82 | version 0.900 | ||
| 83 | |||
| 84 | =head1 AUTHOR | ||
| 85 | |||
| 86 | Thomas Klausner <domm@cpan.org> | ||
| 87 | |||
| 88 | =head1 COPYRIGHT AND LICENSE | ||
| 89 | |||
| 90 | This software is copyright (c) 2017 by Thomas Klausner. | ||
| 91 | |||
| 92 | This is free software; you can redistribute it and/or modify it under | ||
| 93 | the same terms as the Perl 5 programming language system itself. | ||
| 94 | |||
| 95 | =cut |