| 1 | package DBIx::Class::InflateColumn::Serializer::YAML; | ||||||||||
| 2 | 3 | $DBIx::Class::InflateColumn::Serializer::YAML::VERSION = '0.08'; | |||||||||
| 3 | =head1 NAME | ||||||||||
| 4 | |||||||||||
| 5 | DBIx::Class::InflateColumn::Serializer::YAML - YAML Inflator | ||||||||||
| 6 | |||||||||||
| 7 | =head1 SYNOPSIS | ||||||||||
| 8 | |||||||||||
| 9 | package MySchema::Table; | ||||||||||
| 10 | use base 'DBIx::Class'; | ||||||||||
| 11 | |||||||||||
| 12 | __PACKAGE__->load_components('InflateColumn::Serializer', 'Core'); | ||||||||||
| 13 | __PACKAGE__->add_columns( | ||||||||||
| 14 | 'data_column' => { | ||||||||||
| 15 | 'data_type' => 'VARCHAR', | ||||||||||
| 16 | 'size' => 255, | ||||||||||
| 17 | 'serializer_class' => 'YAML' | ||||||||||
| 18 | } | ||||||||||
| 19 | ); | ||||||||||
| 20 | |||||||||||
| 21 | Then in your code... | ||||||||||
| 22 | |||||||||||
| 23 | my $struct = { 'I' => { 'am' => 'a struct' }; | ||||||||||
| 24 | $obj->data_column($struct); | ||||||||||
| 25 | $obj->update; | ||||||||||
| 26 | |||||||||||
| 27 | And you can recover your data structure with: | ||||||||||
| 28 | |||||||||||
| 29 | my $obj = ...->find(...); | ||||||||||
| 30 | my $struct = $obj->data_column; | ||||||||||
| 31 | |||||||||||
| 32 | The data structures you assign to "data_column" will be saved in the database in YAML format. | ||||||||||
| 33 | |||||||||||
| 34 | =cut | ||||||||||
| 35 | |||||||||||
| 36 | 3 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 37 | 3 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 38 | 3 | ▶ | use YAML; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 39 | 3 | ▶ | use Carp; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 40 | |||||||||||
| 41 | =over 4 | ||||||||||
| 42 | |||||||||||
| 43 | =item get_freezer | ||||||||||
| 44 | |||||||||||
| 45 | Called by DBIx::Class::InflateColumn::Serializer to get the routine that serializes | ||||||||||
| 46 | the data passed to it. Returns a coderef. | ||||||||||
| 47 | |||||||||||
| 48 | =cut | ||||||||||
| 49 | |||||||||||
| 50 | sub get_freezer{ | ||||||||||
| 51 | 6 | ▶ | my ($class, $column, $info, $args) = @_; | ||||||||
|
Subroutine
get_freezer: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 52 | |||||||||||
| 53 | 6 | ▶ | if (defined $info->{'size'}){ | ||||||||
|
Branch: 1 branch
| |||||||||||
| 54 | 3 | my $size = $info->{'size'}; | |||||||||
| 55 | return sub { | ||||||||||
| 56 | 2 | ▶ | my $s = YAML::Dump(shift); | ||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||
| 57 | 2 | ▶ | croak "serialization too big" if (length($s) > $size); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 58 | 1 | return $s; | |||||||||
| 59 | 3 | }; | |||||||||
| 60 | } else { | ||||||||||
| 61 | return sub { | ||||||||||
| 62 | 2 | ▶ | return YAML::Dump(shift); | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 63 | 3 | }; | |||||||||
| 64 | } | ||||||||||
| 65 | } | ||||||||||
| 66 | |||||||||||
| 67 | =item get_unfreezer | ||||||||||
| 68 | |||||||||||
| 69 | Called by DBIx::Class::InflateColumn::Serializer to get the routine that deserializes | ||||||||||
| 70 | the data stored in the column. Returns a coderef. | ||||||||||
| 71 | |||||||||||
| 72 | =back | ||||||||||
| 73 | |||||||||||
| 74 | =cut | ||||||||||
| 75 | |||||||||||
| 76 | sub get_unfreezer { | ||||||||||
| 77 | return sub { | ||||||||||
| 78 | 3 | ▶ | return YAML::Load(shift); | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 79 | 6 | ▶ | }; | ||||||||
|
Subroutine
get_unfreezer: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 80 | } | ||||||||||
| 81 | |||||||||||
| 82 | |||||||||||
| 83 | 3 | 1; | |||||||||