Permalink
Browse files

add new set_serialize_column for DBIx::Class::Schema::Loader

  • Loading branch information...
1 parent adced79 commit d539f2992195113821a565393bfeda6ba3e4f04b @fayland committed Dec 15, 2013
Showing with 26 additions and 5 deletions.
  1. +25 −4 lib/DBIx/Class/InflateColumn/Serializer.pm
  2. +1 −1 t/lib/DBICTest/Schema/SerializeJSON.pm
@@ -5,11 +5,25 @@ use warnings;
sub register_column {
my $self = shift;
- my ($column, $info, $args) = @_;
$self->next::method(@_);
+ $self->__serializer_inflate_column(@_);
+};
- return unless defined $info->{'serializer_class'};
+sub set_serialize_column {
+ my $self = shift;
+ my ($column, $serializer_class, $args) = @_;
+
+ my $info = $self->column_info($column);
+ $info->{serializer_class} = $serializer_class;
+
+ $self->__serializer_inflate_column($column, $info, $args);
+}
+
+sub __serializer_inflate_column {
+ my $self = shift;
+ my ($column, $info, $args) = @_;
+ return unless defined $info->{'serializer_class'};
my $class = "DBIx::Class::InflateColumn::Serializer::$info->{'serializer_class'}";
eval "require ${class};";
@@ -26,7 +40,7 @@ sub register_column {
deflate => $freezer,
}
);
-};
+}
=head1 NAME
@@ -42,7 +56,7 @@ DBIx::Class::InflateColumn::Serializer - Inflators to serialize data structures
'data_column' => {
'data_type' => 'VARCHAR',
'size' => 255,
- 'serializer_class' => 'JSON'
+ 'serializer_class' => 'JSON'
}
);
@@ -81,6 +95,13 @@ Right now there are three serializers:
3. add 'serializer_class' => SERIALIZER to the properties of the column that you want to (de/i)nflate
with the SERIALIZER class.
+=head1 Working with DBIx::Class::Schema::Loader
+
+Sometimes you use L<DBIx::Class::Schema::Loader> that you can't change the add_columns part. in that case, you can add below code after md5sum
+
+__PACKAGE__->load_components('InflateColumn::Serializer');
+__PACKAGE__->set_serialize_column('data_column', 'JSON'); # $column_name, $serializer_class
+
=head1 NOTES
As stated in the DBIC FAQ: "Be careful not to overuse this capability, however. If you find yourself depending more and more on some data within the inflated column, then it may be time to factor that data out."
@@ -18,12 +18,12 @@ __PACKAGE__->add_columns(
},
'serial2' => {
data_type => 'varchar',
- serializer_class => 'JSON',
is_nullable => 1,
}
);
__PACKAGE__->set_primary_key('testtable_id');
+__PACKAGE__->set_serialize_column('serial2', 'JSON');
1;

0 comments on commit d539f29

Please sign in to comment.